reflection

V

vinayakumar

Hi
I have following requirement.
Write information of metods in some class in xml format. The methods
will have some thing called atachement.
for eg:

public void mymethd(){}

public static final string atch$mymethd$description="Metod
description".

My xml will look like
<name ="mymethod" descriptin ="method description"/>
I am dping this using reflection and xml parsing.
The problem I am facing is some classes have static attributes that
gets initilized by calling a static method which uses data base etc. I
mean this need application server context. But my tool won't be runnig
in application server it is just a stand alone applicaion and get
required claases from class path.
If I have these kind of classes I get error when I do class.forName()
becuase these static variables trying to get initilized which needs
application server context.
I tried class.forName(className,boolean initlize, Claaloader) method
also by giving initilze as false. Now I face problem when I try to get
value of static final String. This try to initlize all the the
attributes of the class again and I get same error when I do
class.forName(className). Any suggestion welcome
 
C

Chris Uppal

The problem I am facing is some classes have static attributes that
gets initilized by calling a static method which uses data base etc. I
mean this need application server context. But my tool won't be runnig
in application server it is just a stand alone applicaion and get
required claases from class path.
If I have these kind of classes I get error when I do class.forName()
becuase these static variables trying to get initilized which needs
application server context.

Two approaches I can think of:

The quick and dirty way would be to run your program with a classpath which
replaced the database classes with dummy classes of the same name. So the
classes you are interested in would still execute their initialisation code,
but that code would not do anything since it was using the dummy classes.

A far more effective way, but more work (especially if you have this almost
working already, since you'll have to throw away half your code), would be to
parse the classfiles directly instead of using reflection. There are several
free libraries for parsing classfiles. You might find that ASM
(http://asm.objectweb.org/) is as good as any, and it has some decent
tutorials.

I would definitely do it the second way, myself, but then I already understand
the classfile format.

Aside: Does anyone know of a front-end to ASM (or any other bytecode library)
which accurately emulates (part of) the reflection API ? I think such a
library could be rather useful.

-- chris
 
D

Domagoj Klepac

I have following requirement.
Write information of metods in some class in xml format. The methods
will have some thing called atachement.
for eg:

public void mymethd(){}

public static final string atch$mymethd$description="Metod
description".

It might be more convenient to use annotations than static Strings, if
you're not limited to older JDK. And you wouldn't need to instantiate
classes to peek at their methods.

Domchi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top