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
Chris Uppal - 25 May 2006 14:52 GMT
> 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
[quoted text clipped - 4 lines]
> 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
Domagoj Klepac - 25 May 2006 14:57 GMT
>I have following requirement.
>Write information of metods in some class in xml format. The methods
[quoted text clipped - 5 lines]
>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

Signature
Ouroboros ltd. - http://www.ouroboros.hr
Antispam: to reply, remove extra monkey from reply-to address.