Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reflection api  RSS feed

 
pinjalim bora
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
I have a serious problem...
I am making a tool that will dynamicallly load the classes inside a dir strcuture to find the name of a class that has the declaration of a method.I am using the Reflection API...But it has become very slow....
Please suggest

[Added [ code ] tags - Jim]
[ January 18, 2004: Message edited by: Jim Yingst ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mhh, on the first scan, I don't see anything horribly wrong with that code. You should probably use a profiler to get some information about where the bottleneck lies.
Or you should tell us, *why* you are doing this. Possibly that there is an alternative, less costly strategy to do the thing you need to do...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect it would be faster to do a text search on the files for the method you are looking for. You could use reflection on just those files to check it was the method you want (and not just one with the same name.) Of course, this would only save time if the method is in a small percentage of the files.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
I suspect it would be faster to do a text search on the files for the method you are looking for. You could use reflection on just those files to check it was the method you want (and not just one with the same name.) Of course, this would only save time if the method is in a small percentage of the files.

That could work, I guess. Interesting idea!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Corneil du Plessis
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you use a HashMap to store the results so the subsequent search can be served from the cache.
However if memory is a concern do not store the results in a database just use a java.lang.ref.SoftReference. This will allow the garbage collector to reclaim memory. You will then do the search as if the first time after that.
I am also interested in why you need this functionality.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!