• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reflection api

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic