Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reflection and Annotation-arguments

Reply
Thread Tools

Reflection and Annotation-arguments

 
 
grz01
Guest
Posts: n/a
 
      09-25-2009

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

 
Reply With Quote
 
 
 
 
Mayeul
Guest
Posts: n/a
 
      09-25-2009
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
 
Reply With Quote
 
 
 
 
Arved Sandstrom
Guest
Posts: n/a
 
      09-25-2009
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
 
Reply With Quote
 
grz01
Guest
Posts: n/a
 
      09-25-2009
On Sep 25, 11:22*am, Mayeul <(E-Mail Removed)> 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!
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-28-2009
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/>
 
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
Type Reflection and Code Directory Andrew Feldman ASP .Net 2 12-20-2004 04:08 PM
ASP.NET 2.0 (beta) and Reflection deja.com@joel.net ASP .Net 2 11-04-2004 04:19 AM
Reflection and COM Alan Seunarayan ASP .Net 3 01-14-2004 10:56 PM
Re: Reflection and SqlConnection WebDevHokie ASP .Net 0 07-10-2003 06:38 PM
Re: Reflection and SqlConnection WebDevHokie ASP .Net 0 07-10-2003 06:33 PM



Advertisments