Looks like you're pretty much there!
I've had a lot of difficulties with this homework too.
Here's what I've finished up with:

Note: I could not get it to work with the travelThroughTime method taking no
parameters, that is: travelThroughTime(), so used travelThroughTime(String
s) instead.



Output:

Method travelThroughTime has 2 annotations
Annotation 0: @RequestForEnhancement(engineer=Mr. Peabody, date=14/1/3007,
id=2868724, synopsis=Enable time-travel), Type: RequestForEnhancement
Method travelThroughTime has an annotation of type RequestForEnhancement
id:       2868724
date:     14/1/3007
engineer: Mr. Peabody
synopsis: Enable time-travel
Annotation 1: @Name(first=Earnest, last=Peabody), Type: Name



AnnotatedClass.java:

    @RequestForEnhancement(
        id = 2868724,
        synopsis = "Enable time-travel",
        engineer = "Mr. Peabody",
        date = "14/1/3007"
    )
    @Name(
        first = "Earnest",
        last = "Peabody"
    )
    public void travelThroughTime(String s) {
        System.out.println("travelthroughTime Method");
    }



RuntimeAnnotation.java:

    // Get specific Method
    try{
        //Method method = c.getMethod("travelThroughTime", null);
        //Method method = c.getMethod("travelThroughTime", (Class)null);
        Method method = c.getMethod("travelThroughTime", new
Class[]{String.class});   // obtain method object

        // Get specific annotation for a method
        //Annotation annotation =
method.getAnnotation(RequestForEnhancement.class);

        // Get all annotations for a method
        Annotation[] annotations2 = method.getDeclaredAnnotations();
        int numberOfAnnotations2 = annotations2.length;
        System.out.println("Method " + method.getName() + " has " +
numberOfAnnotations2 + " annotations");

        for (int i = 0 ; i < numberOfAnnotations2; i++) {
            System.out.println("Annotation " + i + ": " + annotations2[i] +
            ", Type: " + annotations2[i].annotationType().getName());

            if(annotations2[i] instanceof RequestForEnhancement){
                System.out.println("Method " + method.getName() + " has an
annotation of type " + annotations2[i].annotationType().getName());
                RequestForEnhancement myAnnotation = (RequestForEnhancement)
annotations2[i];
                System.out.println("id:       " + myAnnotation.id());
                System.out.println("date:     " + myAnnotation.date());
                System.out.println("engineer: " + myAnnotation.engineer());
                System.out.println("synopsis: " + myAnnotation.synopsis());
            }
        }
            
    } catch(Exception e){
        System.out.printf("Caught runtime exception = %s", e);
    }



Regards,
Mark


-----Original Message-----
From: papatya [mailto:[email protected]] 
Sent: Wednesday, 18 November 2009 7:39 AM
To: Java Programming Online Training Course By Sang Shin
Subject: [java programming] Re: lab 1107-annotation

OK FORGET First one please
this is the situation that i am in.

output:
Class MyOwnAnnotationProject has 1 declaredAnnotations
Annotation 0: @RequestForEnhancement(engineer=Ms. Eagle,
date=03/03/2009, id=55, synopsis=sum), typeRequestForEnhancement
Class MyOwnAnnotationProject has 0 declaredAnnotations
Class MyOwnAnnotationProject has 0 declaredAnnotations

is it ok?
____________________________________________________________________________
__________________________
import java.lang.annotation.*;
import java.lang.reflect.*;

public class MyOwnAnnotationProject {


    public MyOwnAnnotationProject() {

    }
 @RequestForEnhancement(id = 55,
    synopsis = "sum",
    engineer = "Ms. Eagle",
    date = "03/03/2009")




    public void printAnnotations() {
        Class c = this.getClass();
        Method[] m = c.getDeclaredMethods();
        for (int i = 0; i<m.length; i++){
            Annotation[] declaredAnnotations = m
[i].getDeclaredAnnotations();
int numberDeclaredAnnotations = declaredAnnotations.length;
System.out.println("Class " + c.getName() + " has " +
       numberDeclaredAnnotations + " declaredAnnotations");
for (int j = 0 ; j < numberDeclaredAnnotations; j++) {
       System.out.println("Annotation " + j + ": "+
       declaredAnnotations[j] +  ", type" + declaredAnnotations
[j].annotationType().getName().toString());
}

                }
}
    public static void main(String[] args) {
        MyOwnAnnotationProject ar = new MyOwnAnnotationProject();
        ar.printAnnotations();

       }
    public void addition(){
       int i = 2;
       int j = 1;
       int result=i+j;
    }
}

On Nov 17, 10:01 pm, papatya <[email protected]> wrote:
> i read the forum but i mixed evrythg
> i can't find the right output
> hope you to help me
>
> run:
> Class MyOwnAnnotationProject has 0 declaredAnnotations
> BUILD SUCCESSFUL (total time: 0 seconds)
>
> What is wrong with this?
> ______________________________________________________________
> import java.lang.annotation.Annotation;
> import java.lang.reflect.Method;
>
> /**
>  *
>  * @author ppty
>  */
> import java.lang.annotation.*;
> import java.lang.reflect.*;
> public class RunTimeAnnotation {
>     MyOwnAnnotationProject ac;
>     public RunTimeAnnotation() {
>         ac = new MyOwnAnnotationProject ();
>     }
>     public void printAnnotations() {
>         Class c = ac.getClass();
>         Method[] m = c.getDeclaredMethods();
>
>         for (int i = 0; i<m.length; i++){
>           Annotation[] declaredAnnotations = m
> [i].getDeclaredAnnotations();
> int numberDeclaredAnnotations = declaredAnnotations.length;
> System.out.println("Class " + c.getName() + " has " +
>        numberDeclaredAnnotations + " declaredAnnotations");
> for (int j = 0 ; j < numberDeclaredAnnotations; j++) {
>        System.out.println("Annotation " + j + ": "+
>        declaredAnnotations[j] +  ", type" + declaredAnnotations
> [j].annotationType().getName().toString());}
>                 }
> }
>
>     public static void main(String[] args) {
>         RunTimeAnnotation ar = new RunTimeAnnotation();
>         ar.printAnnotations();
>        }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

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

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