Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Check class file for annotation

Thread view: 
Ian Pilcher - 23 Jan 2006 20:50 GMT
Does anyone know of a simple way to determine whether a marker
annotation is present in a given class file?

TIA

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Chris Uppal - 23 Jan 2006 21:13 GMT
> 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 - 23 Jan 2006 22:56 GMT
> Does anyone know of a simple way to determine whether a marker
> annotation is present in a given class file?
>
> TIA

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(ja
va.lang.Class
)

Signature

Tony Morris
http://tmorris.net/

Chris Uppal - 24 Jan 2006 10:40 GMT
> 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 - 24 Jan 2006 15:19 GMT
> 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!

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Chris Uppal - 24 Jan 2006 15:42 GMT
> 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 - 24 Jan 2006 15:48 GMT
> 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.

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.