You are actually annotating the getId() method . And when you say
Class c = ai.getClass();
Annotation[] annotations = c.getAnnotations();
It returns the annotations for the class , as you dont have one , it says 0
annotations.
If you want to get annotations of a method , you can use this code
Class c = ai.getClass();
Method[] mym = c.getDeclaredMethods();
Annotation[] a = mym[i].getDeclaredAnnotations();
On Thu, Aug 27, 2009 at 6:02 AM, Guillermo <[email protected]>wrote:
>
> Hi all,
> I dont really understand annotations and I dont know what I am doing
> wrong with this lab, the code is the following:
>
> "RequestForEnhancement.java"
>
> import java.lang.annotation.*;
>
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.METHOD)
>
> public @interface RequestForEnhancement {
> int id();
> String synopsis();
> String engineer() default "[unassigned]";
> String date() default "[unimplemented]";
> }
>
> *******************************************************************************************
>
> "AnnotationImplementation.java"
>
> import java.util.Date;
>
> public class AnnotationImplementation {
> int id;
> String synopsis;
> String engineer;
> Date date;
>
> @RequestForEnhancement(
> id = 2868724,
> synopsis = "Enable time-travel",
> engineer = "Mr. Peabody",
> date = "4/1/3007")
>
> public int getId(){
> return id;
> }
> public String getSynopsis(){
> return synopsis;
> }
> public String getEngineer(){
> return engineer;
> }
> public Date getDate(){
> return date;
> }
> }
>
> ********************************************************************************
> "MyOwnAnnotationExample.java"
>
> import java.lang.annotation.*;
>
> public class MyOwnAnnotationExample {
>
> AnnotationImplementation ai;
>
> public MyOwnAnnotationExample(){
> ai=new AnnotationImplementation();
> }
>
>
> public void printAnnotations() {
> Class c = ai.getClass();
> Annotation[] annotations = c.getAnnotations();
> int numberOfAnnotations = annotations.length;
> System.out.println("Class " + c.getName() + " has " +
> numberOfAnnotations + " annotations");
>
> for (int i = 0 ; i < numberOfAnnotations; i++) {
> System.out.println("Annotation " + i + ": " + annotations
> [i] +
> ", type" + annotations[i].annotationType().getName
> ());
> }
> }
>
> public static void main(String[] args) {
> MyOwnAnnotationExample moae = new MyOwnAnnotationExample();
> moae.printAnnotations();
> }
>
> }
>
> ***********************************************************
>
> The resul when build and run is:
>
> run:
> Class AnnotationImplementation has 0 annotations
> BUILD SUCCESSFUL (total time: 2 seconds)
>
>
> Am I annotating a method, if so, why the result says 0 annotations,
> what´s wrong
>
> Thank you.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---