Override fillInStackTrace for JVM standard exceptions

If I use reflection and I want to find whether the method is implemented or not, I can use the getMethod () method. This method throws a NoSuchMethodException.

Is there a way to overload fillInStackTrace of this exception to optimize performance? Currently, about 40% of the time is spent in this method.

I use an exception framework as a way to execute a specific control flow.

Therefore, I do not want to be too aggressive. If I create a class extending Throwable and using this new class instead of NoSuchMethodException, I have something like:

NewException is never thrown in body of corresponding trystatement 

thanks

+1
source share
2 answers

No, since getMethod() calls new directly, and you cannot replace the code for NoSuchMethodException because the class is signed and fillInStackTrace() is native .

It is best to cache calls to getMethod() in a central location: just create a two-level map: Map<Class, Map<String, Method>> and use a quick search without any exception.

+1
source

My two points below do not exactly determine the name of your question, but I think they might be useful ...


I confirm your effectiveness.

I read about the solution in java execution. We applied this to our own application, with a few exceptions (where the stack trace is not important and the possible frequency is high). I do not know if you will like it ...; -)

Create a unique instance of the Exception class and save it. Throw this instance away .

This seems ideal if you do not want to disrupt an existing thread that relies on exceptions.


If your compiler complains that the other method does not throw this exception, this is because you have selected the marked exception.
Use a subclass of RuntimeException (they are not marked, so the compiler does not know if they are thrown or not, it will not complain).

+1
source

All Articles