Skip to content

Use isAssignableFrom instead of relying on ClassCastException#18350

Merged
jdconrad merged 2 commits intoelastic:masterfrom
uschindler:painless_isAssignableFrom
May 16, 2016
Merged

Use isAssignableFrom instead of relying on ClassCastException#18350
jdconrad merged 2 commits intoelastic:masterfrom
uschindler:painless_isAssignableFrom

Conversation

@uschindler
Copy link
Copy Markdown
Contributor

Title says it all :-)

@uschindler
Copy link
Copy Markdown
Contributor Author

I found more of those, updating PR in a minute...

@uschindler
Copy link
Copy Markdown
Contributor Author

uschindler commented May 14, 2016

I added more changes around the static casting. It would be good if somebody cross-checks, because some of checks were really crazy (AnalyzerCaster was completely strange to me - I think the fix is right and much simpler)!

@uschindler uschindler force-pushed the painless_isAssignableFrom branch from f8572d5 to a5b0e43 Compare May 14, 2016 21:39
@uschindler uschindler force-pushed the painless_isAssignableFrom branch from a5b0e43 to 8195ef9 Compare May 14, 2016 23:06
@clintongormley clintongormley changed the title painless: Use isAssignableFrom instead of relying on ClassCastException Use isAssignableFrom instead of relying on ClassCastException May 15, 2016
@rmuir
Copy link
Copy Markdown
Contributor

rmuir commented May 16, 2016

I agree catching the cast makes the logic hard to understand. personally i find asSubclass much easier to reason about than isAssignableFrom (i think the verb from in the method throws me for a loop every time), but given that we want to return our own exceptions anyway and that there is some "or" logic in here, I think this is better here. Thanks for cleaning it up! cc: @jdconrad

@uschindler
Copy link
Copy Markdown
Contributor Author

personally i find asSubclass much easier to reason about than isAssignableFrom (i think the verb from in the method throws me for a loop every time)

I know those arguments - especially the "From" confuses. If you once understood how this method should be used, it is quite easy to understand:

If you have left.isAssignableFrom(right) then this only returns true if you could do the following assignment leftInstance = rightInstance - you see left stays left and right stays right. It returns false if you would need a cast and you would get a ClassCastException.

So don't think about super- or subclass, just think in terms of this assignment possible or not! :-)

The problem is similar to some people to understand like compareTo. It is the same: left.compareTo(right) is the same like Comparator.compare(left, right), so its just the order. It also brought me into the same mental loop until you realize this "order of arguments".

For the code in painless, the huge trycatches confuse more than the mental loop :-)

P.S.: If you look at Class.isSubclass()'s source, it calls isAssignableFrom internally, because it is the one that the JVM implements. It just wraps the ClassCastException afterwards. So In our current code, it does the same thing 2 times and each unwrapping exception. And its unreadable, too.

@uschindler
Copy link
Copy Markdown
Contributor Author

and that there is some "or" logic in here

Thats the real reason why I did it!

@jdconrad
Copy link
Copy Markdown
Contributor

Thanks @uschindler! LGTM. If I'm being completely honest, asSubclass was the one I found in examples, so I used that, but this is much cleaner.

@jdconrad jdconrad merged commit f664fa5 into elastic:master May 16, 2016
@uschindler uschindler deleted the painless_isAssignableFrom branch May 16, 2016 20:09
@jdconrad jdconrad mentioned this pull request May 17, 2016
18 tasks
@clintongormley clintongormley added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache and removed :Plugin Lang Painless labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v5.0.0-alpha3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants