I added [ code ] tags above in hopes of improving the readability of the code; unfortunatley the indentation scheme is badly broken. Please take the time to format your code to be readable; it greatly improves your chances of getting people to take time to understand it.
How many classes are you loading, anyway? Are you searching the entire core
Java libraries or something?
If you were able to specify the parameter types as well as the method name, getDeclaredMethod(
String, Class[]) would probably speed things up.
If you're going to be searching the same files multiple times (looking for different methods) it may be preferable to build a data structure for faster searches. E.g. a HashMap using method name as the key. Each value could be a List of all classes found so far with a method of that name. It might still be slow to read all the class files the first time, but subsequent searches can be made much faster. If it takes too much memory to store all that data, you can use a simple SQL database like
HSQL or
mckoi to create tables which can be searched fairly quickly. I'd try the HashMap idea first and see how well it works.
Your catch (NullPointerException nullpointer) looks suspicious. If you're getting a NullPointerException I suspect you should take the time to find out exactly which variable is null, and why. If there's a legitimate reason it may be null,
test for that by checking if (var == null) before you invoke var.method() (because that would throw the exception). The problem with catching NullPointerException as you do is that it may be hiding a more serious programming error.
Note that it looks like your findMethod returns as soon as it finds one class which has a method matching the requested name. It's entirely possible that there are other classes in that package (or subpackages) which also have the same method. Are you sure you want to omit those? Of course that will make your search longer...