• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Dynamic Reflection

 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write a program which support "dynamic reflection." That is, using this program the user selects a class (for which the user may or may not have the original source), and performs reflection on it, say, getting a list of all methods in the class. The user would then select a Method, and save "it", such that the actual Method object can later be used, say in another program.
As I think about this, there are two approaches.
1) Use reflection.
In this case, I would need to get an instance of the class. IIRC, I can just use a Class object, and don't need to instantiate an actual instanc eof the object, right? Still, in either case, I need to write my own class loader, because the class I'm inspecting may not be in the classpath. If I do need to create an intsance of the object, then I need to make sure all the objects it references can also be loaded, sine they may also not be in the classpath. Does this sound right? Anything else I'm not thinking of?
2) Parse the byte code
I could simply inspect the byte code in the class file. This is simple text processing. Of course, in this case I don't think I can generate actual Method objects (as in java.lang.reflect.Method). I would have to just save the message name, and my second program would have to dynamically resolve the name into an actual Method object. Classpath issues should not be a problem in this second program. Does this sound right? Anything else I'm not thinking of?

Has anyone tried anything like this? I'd love to hear opinions and advice.
--Mark
hershey@vaultus.com
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark,
I've done something similar to the first scenario you described: I created code that would allow a class to be loaded dynamically. Yes, you'll need to create your own ClassLoader for this purpose, and yes, you'll need to make sure that any referenced classes can also be loaded. For example, if you have a class called SubTest that extends SuperTest and your ClassLoader tries to load SubTest, it will also need to be able to load SuperTest. In other words, a subclass can't be loaded successfully unless its superclass can also be loaded.
I can't say that I've ever done anything like what you described in the second scenario (i.e., the byte code parsing option), but I suspect that doing so may not be all that easy. Without knowing more about what you're trying to accomplish, my guess is that you're going to be much better off going with the first (ClassLoader) option.
------------------
Brett Spell
Author, Professional Java Programming
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see, this is a guess. You have a class file from some one else, called Other.class You could decompile it( just load it into and IDE), and see what package it in in. Then put it in that package stucture, and use an import statement in your own code. Then use reflection on it.
As far as being able to do that dynamically, ( such as a user selecting a .class file from a gui) you could run exec commands that did all of the above. Quite a chore. There must be a better way.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic