-
Notifications
You must be signed in to change notification settings - Fork 875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch IAE when Gradle error getLocation cannot be called. #5270
Catch IAE when Gradle error getLocation cannot be called. #5270
Conversation
return null; | ||
} catch (IllegalArgumentException iae) { | ||
LOG.log(Level.FINE,"Trying to get location from an exception, that has no location property: " + locationAwareEx.getClass().getName()); | ||
} | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem might be deeper than this. The method accepts throwable but expects it to be LocationAwareException
. If someone would call it with the wrong exception type
if (locationAccessor == null) {
locationAccessor = locationAwareEx.getClass().getMethod("getLocation"); // NOI18N
}
would intitialize the field wrong if it happens to have that method, and all subsequent calls will fail or do really weird things.
I personally am not a fan of Throwable
usage, since it is in 99% of all cases not something you should be catching.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I think it's a tradeoff we need to pay when we try to cover multiple versions at once. My mind could trick me, but some exceptions had getLocation
methods even before LocationAwareException
was introduced.
This is just some heuristic, trying to figure out what's behind a Gradle build failure. If we can get a location out of that we display it as an editor hint on the source code.
Well, if the heuristic does not work we just shall let it go...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but doesn't the Method
field has to fit to the right class? If this method is supposed to be as generic as possible and work for all exceptions which have a getLocation
method, I don't think it should be cached as field. It should be checked if that exception has getLocation
on every invocation:
Method locationAccessor = locationAwareEx.getClass().getMethod("getLocation"); // NOI18N
return (String)locationAccessor.invoke(locationAwareEx);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this illustrates the issue:
Integer five = 5;
Double pi = Math.PI;
Method stringAccessor = five.getClass().getMethod("toString");
System.out.println(stringAccessor.invoke(pi));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Point taken. Haven't really checked what's in the try block. Thanks!
10a2bff
to
b367560
Compare
b367560
to
00d5d23
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me
Let me try a different fix ... I've cached the accessor so that the reflective access is not that slow; but it should be easy to check for |
added do not merge label so that nobody merges by accident |
@sdedic Take your time! Just would like to add, that it's fine not to cache the Method accessor here. This is on a critical path. |
Ouch ! No caching is possible, in fact. During debugging, I've realized (only now) that the Tooling API seems to load classes for the deserialized data using a throwaway classloader , so Classes from 2 project loads are not assignable at all, even though they represent the same type. |
@sdedic thanks a lot for checking! Reflection is super fast anyway these days - I don't think much is lost there. |
I've bumped into this one today:
Couldn't really know how to reproduce. I was fiddling with old Gradle versions with the new tooling.
The fix is trivial though...