Reflection and Annotation-arguments

Discussion in 'Java' started by grz01, Sep 25, 2009.

  1. grz01

    grz01 Guest

    Hi again,

    Another reflection question...

    I can get at the annotations of a class by doing something like:

    import java.lang.annotation.Annotation
    ...
    Bean bean = new Bean();
    Annotation a = bean.getClass().getAnnotations()[0];

    But if the Annotation has parameters, like

    @Table(name="FOOBAR")
    public class Bean{ ... }

    how do I extract the argument here (i e: name = "FOOBAR") ?

    Looking at the java.lang.annotation.Annotation class, I dont see any
    method for this?

    TIA / grz01
    grz01, Sep 25, 2009
    #1
    1. Advertising

  2. grz01

    Mayeul Guest

    grz01 wrote:
    > Hi again,
    >
    > Another reflection question...
    >
    > I can get at the annotations of a class by doing something like:
    >
    > import java.lang.annotation.Annotation
    > ...
    > Bean bean = new Bean();
    > Annotation a = bean.getClass().getAnnotations()[0];
    >
    > But if the Annotation has parameters, like
    >
    > @Table(name="FOOBAR")
    > public class Bean{ ... }
    >
    > how do I extract the argument here (i e: name = "FOOBAR") ?
    >
    > Looking at the java.lang.annotation.Annotation class, I dont see any
    > method for this?


    That is because annotations extend the java.lang.annotation.Annotation
    interface, and declare by themselves the parameters they accept, in the
    form of parameterless methods.

    Since your Table annotation accepts a 'name' parameter, it defines at
    least a name() method, which probably returns a String.
    You need to cast your Annotation object to Table and call this name()
    method on it.

    You can call the annotationType() method if you're not sure what to cast
    your Annotation to. It is also possible to inspect the methods provided
    by annotation classes, and to get an annotation's parameters values by
    invoking those methods on an Annotation object.

    Examples



    Annotation a = bean.getClass().getAnnotations()[0];
    Table table = (Table)a;
    String name = table.name();
    System.out.println(name); // Should display FOOBAR


    Other example: (couldn't find a better way to ignore equals() and so
    methods, really not Google's friend, and curious to learn)

    Annotation a = bean.getClass().getAnnotations()[0];
    for(Method method : a.getClass().getDeclaredMethods()) {
    String paramName = method.getName().intern();
    if("equals" != paramName && "hashCode" != paramName &&
    "toString" != paramName && "annotationType" != paramName) {
    Object param = method.invoke(a);
    System.out.println(paramName + ": " + param);
    }
    }
    // Should display at least 'name: FOOBAR'


    --
    Mayeul
    Mayeul, Sep 25, 2009
    #2
    1. Advertising

  3. grz01 wrote:
    > Hi again,
    >
    > Another reflection question...
    >
    > I can get at the annotations of a class by doing something like:
    >
    > import java.lang.annotation.Annotation
    > ...
    > Bean bean = new Bean();
    > Annotation a = bean.getClass().getAnnotations()[0];
    >
    > But if the Annotation has parameters, like
    >
    > @Table(name="FOOBAR")
    > public class Bean{ ... }
    >
    > how do I extract the argument here (i e: name = "FOOBAR") ?
    >
    > Looking at the java.lang.annotation.Annotation class, I dont see any
    > method for this?
    >
    > TIA / grz01
    >

    The Annotation class in and of itself is not useful here. Discover your
    class/method/parameter etc annotations using the methods in java.lang.Class.

    AHS
    Arved Sandstrom, Sep 25, 2009
    #3
  4. grz01

    grz01 Guest

    On Sep 25, 11:22 am, Mayeul <> wrote:
    > grz01 wrote:
    > > Hi again,

    >
    > > Another reflection question...

    >
    > > I can get at the annotations of a class by doing something like:

    >
    > >     import java.lang.annotation.Annotation
    > >     ...
    > >     Bean bean = new Bean();
    > >     Annotation a = bean.getClass().getAnnotations()[0];

    >
    > > But if the Annotation has parameters, like

    >
    > >    @Table(name="FOOBAR")
    > >    public class Bean{ ... }

    >
    > > how do I extract the argument here (i e: name = "FOOBAR") ?

    >
    > > Looking at the java.lang.annotation.Annotation class, I dont see any
    > > method for this?

    >
    > That is because annotations extend the java.lang.annotation.Annotation
    > interface, and declare by themselves the parameters they accept, in the
    > form of parameterless methods.
    >
    > Since your Table annotation accepts a 'name' parameter, it defines at
    > least a name() method, which probably returns a String.
    > You need to cast your Annotation object to Table and call this name()
    > method on it.
    >
    > You can call the annotationType() method if you're not sure what to cast
    > your Annotation to. It is also possible to inspect the methods provided
    > by annotation classes, and to get an annotation's parameters values by
    > invoking those methods on an Annotation object.
    >
    > Examples
    >
    > Annotation a = bean.getClass().getAnnotations()[0];
    > Table table = (Table)a;
    > String name = table.name();
    > System.out.println(name); // Should display FOOBAR
    >
    > Other example: (couldn't find a better way to ignore equals() and so
    > methods, really not Google's friend, and curious to learn)
    >
    > Annotation a = bean.getClass().getAnnotations()[0];
    > for(Method method : a.getClass().getDeclaredMethods()) {
    >    String paramName = method.getName().intern();
    >    if("equals" != paramName && "hashCode" != paramName &&
    >      "toString" != paramName && "annotationType" != paramName) {
    >      Object param = method.invoke(a);
    >      System.out.println(paramName + ": " + param);
    >    }}
    >
    > // Should display at least 'name: FOOBAR'
    >
    > --
    > Mayeul


    I see, very helpful.
    Thanks Mayeul!
    grz01, Sep 25, 2009
    #4
  5. grz01

    Daniel Pitts Guest

    Mayeul wrote:
    > grz01 wrote:
    >> Hi again,
    >>
    >> Another reflection question...
    >>
    >> I can get at the annotations of a class by doing something like:
    >>
    >> import java.lang.annotation.Annotation
    >> ...
    >> Bean bean = new Bean();
    >> Annotation a = bean.getClass().getAnnotations()[0];
    >>
    >> But if the Annotation has parameters, like
    >>
    >> @Table(name="FOOBAR")
    >> public class Bean{ ... }
    >>
    >> how do I extract the argument here (i e: name = "FOOBAR") ?
    >>
    >> Looking at the java.lang.annotation.Annotation class, I dont see any
    >> method for this?

    >
    > That is because annotations extend the java.lang.annotation.Annotation
    > interface, and declare by themselves the parameters they accept, in the
    > form of parameterless methods.
    >
    > Since your Table annotation accepts a 'name' parameter, it defines at
    > least a name() method, which probably returns a String.
    > You need to cast your Annotation object to Table and call this name()
    > method on it.
    >
    > You can call the annotationType() method if you're not sure what to cast
    > your Annotation to. It is also possible to inspect the methods provided
    > by annotation classes, and to get an annotation's parameters values by
    > invoking those methods on an Annotation object.
    >
    > Examples
    >
    >
    >
    > Annotation a = bean.getClass().getAnnotations()[0];
    > Table table = (Table)a;
    > String name = table.name();
    > System.out.println(name); // Should display FOOBAR


    Generally, you ask for an annotation by type, rather than look at the
    available annotations:

    Table table = bean.getClass().getAnnotation(Table.class);
    System.out.println(table.name());

    If you don't know the type before hand, then you can use reflection on
    the returned object to get the public methods, and call those.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Sep 28, 2009
    #5
    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. Ingo R. Homann
    Replies:
    0
    Views:
    555
    Ingo R. Homann
    Jul 26, 2005
  2. =?iso-8859-1?B?bW9vcJk=?=

    reflection: wrong number of arguments

    =?iso-8859-1?B?bW9vcJk=?=, Jan 20, 2006, in forum: Java
    Replies:
    5
    Views:
    11,236
    Chris Smith
    Jan 24, 2006
  3. pek
    Replies:
    2
    Views:
    1,066
  4. Biagio
    Replies:
    0
    Views:
    708
    Biagio
    May 19, 2011
  5. Tom Counsell
    Replies:
    1
    Views:
    96
    Florian Gross
    Sep 17, 2004
Loading...

Share This Page