Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Check class file for annotation

Reply
Thread Tools

Check class file for annotation

 
 
Ian Pilcher
Guest
Posts: n/a
 
      01-23-2006
Does anyone know of a simple way to determine whether a marker
annotation is present in a given class file?

TIA

--
================================================== ======================
Ian Pilcher http://www.velocityreviews.com/forums/(E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      01-23-2006
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


 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      01-23-2006
"Ian Pilcher" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> Does anyone know of a simple way to determine whether a marker
> annotation is present in a given class file?
>
> TIA
>
> --
> ================================================== ======================
> Ian Pilcher (E-Mail Removed)
> ================================================== ======================


Class.isAnnotationPresent
http://java.sun.com/j2se/1.5.0/docs/...ava.lang.Class)


... or do you mean the marker interface hack?
http://java.sun.com/j2se/1.5.0/docs/...ava.lang.Class)

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


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-24-2006
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
 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      01-24-2006
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 (E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      01-24-2006
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


 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      01-24-2006
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 (E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read java Annotation field values from class mani Java 2 03-01-2012 06:00 PM
read java Annotation field values from class mani Java 1 03-01-2012 05:56 PM
Check of java annotation data mike Java 1 09-25-2011 09:08 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
back-annotation SDF Timing Simulation Chao VHDL 1 05-06-2004 05:23 PM



Advertisments