Thanks a lot to all three!

Miga, it worked perfectly. The key was that hint in your sentence. Thank you
very much.

Martin.

On Thu, Aug 6, 2009 at 5:39 PM, miga <[email protected]> wrote:

>
>
>
> On Aug 6, 7:36 pm, Martin <[email protected]> wrote:
> > Hi all there!
> >
> > I'm having problems to do the homework for annotations topic. Is that a
> new
> > one? Because I can't find any help on this group..., and not much at
> other
> > sites either...
> >
> > Well, the code I´m trying to run is this one:
> >
> > *import java.lang.annotation.*;
> >
> > public class MyOwnAnnotationExample {
> >
> >     public MyOwnAnnotationExample() {
> >    }
> >
> >     @RequestForEnhancement(id = 2868724,
> >     synopsis = "Enable time-travel",
> >     engineer = "Mr. Peabody",
> >     date = "4/1/3007")
> >     public String getSynopsis() {
> >         return synopsis;
> >     }
> >
> >     public void printAnnotations() {
> >         Class c = this.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();
> >     }
> >
> > }*
> >
> > And the output:
> >
> > *run:
> > Class MyOwnAnnotationExample has 0 annotations
> > BUILD SUCCESSFUL (total time: 1 second)*
> >
> > I guess the first problem is that in printAnnotations I´m trying to work
> > with class while I'm annotating a method, but I don't get to know how to
> > access the annotation of the getSynopsis() method.
> >
> > Can anyone give me any tips?
> First, as you use a getSomething method which returns the value of a
> variable, you should have a constructor with this variable to set it,
> a private field for it, and call your class with a value for this
> variable.
>
> Next, after getting the class, you should get the declared methods on
> this class (hint in my sentence you have almost the name of the method
> to do it). This method returns an array of methods. Then you loop over
> this array,  and in the loop again you should get the declared
> annotations for the method being inspected, which returns an array of
> annotations. (you may here have your count number of annotations on
> the method and print it using the method name). Again you loop over
> the annotations, and use to method toString to print their fields.
>
> Check also that you use @Target(ElementType.METHOD) annotation, and
> @Retention(Retention.RUN TIME) annotation on the interface.
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to