Check class file for annotation  
Author Message
Ian Pilcher





PostPosted: 2006-1-24 4:50:00 Top

java-programmer, Check class file for annotation Does anyone know of a simple way to determine whether a marker
annotation is present in a given class file?

TIA

--
========================================================================
Ian Pilcher email***@***.com
========================================================================
 
Chris Uppal





PostPosted: 2006-1-24 5:14:00 Top

java-programmer >> Check class file for annotation Ian Pilcher wrote:

> Does anyone know of a simple way to determine whether a marker
> annotation is present in a given class file?

Parse the .class file. Simple enough if you use one of the classfile parsing
libraries such as ASM or BCEL.

-- chris


 
Tony Morris





PostPosted: 2006-1-24 6:56:00 Top

java-programmer >> Check class file for annotation "Ian Pilcher" <email***@***.com> wrote in message
news:email***@***.com...
> Does anyone know of a simple way to determine whether a marker
> annotation is present in a given class file?
>
> TIA
>
> --
> ========================================================================
> Ian Pilcher email***@***.com
> ========================================================================

Class.isAnnotationPresent
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAnnotationPresent(java.lang.Class)


.. or do you mean the marker interface hack?
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAssignableFrom(java.lang.Class)

--
Tony Morris
http://tmorris.net/


 
 
Chris Uppal





PostPosted: 2006-1-24 18:40:00 Top

java-programmer >> Check class file for annotation Tony Morris wrote:

> Class.isAnnotationPresent

Two problems with that (which may not, in fact, be problems for the
OP's application):

1) It requires you to load the class before checking it.

2) It won't work[*] if the annotation is not marked with
RetentionPolicy.RUNTIME.

([*] at least I assume it won't -- I admit I haven't checked.)

BTW, I have just noticed the new "annotations processing tool" (apt)
which is a utility supplied with the 1.5 JDK. I'm not (yet) clear on
what it actually /does/, but then I'm not clear on what the OP /wants/
to do, so it's clearly the right tool for this job ;-)

-- chris
 
 
Ian Pilcher





PostPosted: 2006-1-24 23:20:00 Top

java-programmer >> Check class file for annotation Chris Uppal wrote:
>
> BTW, I have just noticed the new "annotations processing tool" (apt)
> which is a utility supplied with the 1.5 JDK. I'm not (yet) clear on
> what it actually /does/, but then I'm not clear on what the OP /wants/
> to do, so it's clearly the right tool for this job ;-)
>

A little more info.

I'm writing JUnit tests, following the usual practice of creating a
seperate source tree, with identical package names. However, I also
want to test some private stuff. (Yes, I know the arguments against
this; let's not rehash them.)

Rather than use reflection, I have created a nested class with package
accessible methods, which can be called by the unit tests. I have
created an annotation (@TestCode) and applied it to this class. Now I
want to create an Ant task to move all class files with the @TestCode
annotation from the ${PROJECT}/bin tree to ${PROJECT}/test/bin.

From what I can tell of apt, it processes source files, not compiled
classes. It might be possible to use apt to control the build itself,
and put any @TestCode classes into the test/bin tree, but it seems like
more work than it's worth.

I've looked at BCEL; it doesn't support annotations at all. ASM does,
but I really want to avoid adding a dependency on an external library
for something so trivial. At this point, I think I'm just going to
munge the file name into a class name and use Class.forName, etc.

I don't suppose the string munging code is accessible somewhere in the
JDK?

Thanks!

--
========================================================================
Ian Pilcher email***@***.com
========================================================================
 
 
Chris Uppal





PostPosted: 2006-1-24 23:43:00 Top

java-programmer >> Check class file for annotation Ian Pilcher wrote:

> I don't suppose the string munging code is accessible somewhere in the
> JDK?

I don't think so.


> Rather than use reflection, I have created a nested class with package
> accessible methods, which can be called by the unit tests. I have
> created an annotation (@TestCode) and applied it to this class. Now I
> want to create an Ant task to move all class files with the @TestCode
> annotation from the ${PROJECT}/bin tree to ${PROJECT}/test/bin.

I may be missing something, but wouldn't it be simpler to skip annotations
altogether and always name your nested "backdoor access" classes something like
BackdoorAccessForTests ? Then you could identify the corresponding .class
files easily with any tool.

-- chris


 
 
Ian Pilcher





PostPosted: 2006-1-24 23:48:00 Top

java-programmer >> Check class file for annotation Chris Uppal wrote:
>
> I may be missing something, but wouldn't it be simpler to skip annotations
> altogether and always name your nested "backdoor access" classes something like
> BackdoorAccessForTests ? Then you could identify the corresponding .class
> files easily with any tool.
>

Funny, that's what I originally did, and I suppose that nothing prevents
me from going back. An annotation sure seems like the "right way to do
this" (TM).

I must admit, I thought that apt would be able to handle this when I
originally started. I should have learned from the @SuppressWarnings
fiasco.

--
========================================================================
Ian Pilcher email***@***.com
========================================================================