-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Refactor getMethodIfAvailable to not cause NoSuchMethodExceptions #1628
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
Conversation
Method[] methods = clazz.getMethods(); | ||
for (Method method : methods) { | ||
if (method.getName().equals(methodName) | ||
&& Arrays.equals(method.getParameterTypes(), paramTypes)) { |
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.
May I suggest a pre-check for getParameterCount() == paramTypes.length
in order to avoid possible allocations coming from getParameterTypes()
?
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.
Good call. Will rework the PR. Thanks!
Refactor `ClassUtils.getMethodIfAvailable(...)` to loop over methods rather than cause a `NoSuchMethodException`. Since this method is called often, this change can help improve application startup times. Issue: SPR-16320
Hi @philwebb, I took the freedom to do some benchmarking with roughly the following benchmark:
The results below show that the new approach is considerably slower than sticking with the exception (though I wish one could get rid of this overhead):
This mainly comes from the additional heap-pressure as the JDK is copying the method array etc. The results above make me wonder if this will actually improve the performance. Cheers, |
@dreis2211 Thanks for writing those benchmarks. I think you're right, running my samples again with the changes reverted showed no difference in startup time. I was convinced that it had made a difference but I guess not. I wish there was a quick way to tell if a method existed without the exception, but I guess it's not to be. |
Refactor
ClassUtils.getMethodIfAvailable(...)
to loop over methodsrather than cause a
NoSuchMethodException
. Since this method is calledoften, this change can help improve application startup times.
Issue: