Check class file for annotation

Discussion in 'Java' started by Ian Pilcher, Jan 23, 2006.

  1. Ian Pilcher

    Ian Pilcher Guest

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

    TIA

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Jan 23, 2006
    #1
    1. Advertising

  2. Ian Pilcher

    Chris Uppal Guest

    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
    Chris Uppal, Jan 23, 2006
    #2
    1. Advertising

  3. Ian Pilcher

    Tony Morris Guest

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


    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/
    Tony Morris, Jan 23, 2006
    #3
  4. Ian Pilcher

    Chris Uppal Guest

    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
    Chris Uppal, Jan 24, 2006
    #4
  5. Ian Pilcher

    Ian Pilcher Guest

    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
    ========================================================================
    Ian Pilcher, Jan 24, 2006
    #5
  6. Ian Pilcher

    Chris Uppal Guest

    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
    Chris Uppal, Jan 24, 2006
    #6
  7. Ian Pilcher

    Ian Pilcher Guest

    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
    ========================================================================
    Ian Pilcher, Jan 24, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chao
    Replies:
    1
    Views:
    9,777
    Tim Hubberstey
    May 6, 2004
  2. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  3. mike
    Replies:
    1
    Views:
    434
    Robert Klemme
    Sep 25, 2011
  4. mani
    Replies:
    2
    Views:
    1,334
  5. mani
    Replies:
    1
    Views:
    284
Loading...

Share This Page