samedi 28 novembre 2015

Returning Annotations of all methods in a given class using reflection

I have been searching around the internet for awhile now, and am having some issues when it comes to returning annotations specifically for methods. The code I have already returns it for classes, but I want a second section that names each method, and its annotations. Can someone point me to how I can achieve this?

My main file:

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Scanner;
import java.lang.reflect.*;



public class A1 {
     public static void main (String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
            for (String s: args) {
                Class classTemp = Class.forName(s); //class named after argument
                Method tempMethods[] = classTemp.getMethods();

                if(classTemp.isAnnotationPresent(AnnoInterface.class)) { // checks for annotations
                AnnoInterface ai = (AnnoInterface) classTemp.getAnnotation(AnnoInterface.class); //gets annotations

                System.out.println("");
                System.out.println("---------------------------------------");
                System.out.println("Class Review Information:");

                //Print out class annotation for reviewers
                System.out.println("");
                System.out.print("Reviewers: ");
                for (String reviewers: ai.reviewers()) {
                System.out.print(reviewers + ", ");
                } // end

                //Print out class annotation for review dates
                System.out.println("");
                System.out.print("Review Dates: ");
                for (String reviewers: ai.reviewDates()) {
                System.out.print(reviewers + ", ");
                } // end

                //Prints out problem with class
                System.out.println("");
                System.out.print("Problem(s): " + ai.problem());
                //end

                //Prints out if it has been changed since the last review
                System.out.println();
                String temp = "";
                if (ai.isChanged() == true) { temp = "The code has been changed since the last review"; }
                if (ai.isChanged() == false) { temp = "The current review is in date, no changes made since."; }
                System.out.print(temp);
                //end

                System.out.println();
                System.out.println("--------------------------------------- ");



                }

                else {
                    System.out.println("");
                    System.out.println("-------------------------------------------");
                    System.out.println("Sorry, this class has not yet been reviewed");
                    System.out.println("Please review it then try again.");
                    System.out.println("-------------------------------------------");
                }
}
}
}

AnnotationInterface -

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })

public @interface AnnoInterface
{
/** Reviewers */
 String[] reviewers();

 /** reviewed date */
 String[] reviewDates();

 /** what kind of problem the class/method has */
 public enum problems { 
     Syntax, Logic, Runtime, None;
     }

 problems problem();

 /** have changes been made since the last review */
 boolean isChanged() default false;

 /** has the class been reviewed */
 boolean isReviewed() default false;

}

ClassExample1 - (The class I'm running the annotation check on) -

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;



@AnnoInterface (reviewDates = { "1/5/1995", "5/10/2005" }, 
                reviewers = { "John Doe", "Mom" }, 
                isChanged = false,
                isReviewed = true,
                problem = AnnoInterface.problems.Logic
    )

public class ClassExample1 {

    @AnnoInterface (reviewDates = { "5/1/5991", "10/5/5002" }, 
            reviewers = { "Mom", "John Doe" }, 
            isChanged = true,
            isReviewed = false,
            problem = AnnoInterface.problems.Syntax
)
    public static void helloWorld(String args[]){
        System.out.println("Hello World !");

}

    public static void evenOrOdd (String[] args) {
        int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
          for(int i=0; i < numbers.length; i++){
              if(numbers[i]%2 == 0)
                  System.out.println(numbers[i] + " even");
              else
                  System.out.println(numbers[i] + " odd");
          }
    }


}

The only code I've managed to get some kind of success with was when I asked the user specifically for what method they wanted to look up annotations for, but it would always return a "NoSuchMethodException". The thing is I would like to return all annotations in the class file, and not just the specific methods asked anyway.

Any help is greatly appreciated.. thanks!

Edit -

Okay so.. I tried something again and its closer than what I've had.. i added this snippet -

Method[] methods = ai.getClass().getMethods();
                 for (Method method : methods) {
                     if ( method.isAnnotationPresent(AnnoInterface.class));
                        AnnoInterface temp1 = method.getAnnotation(AnnoInterface.class);
                        System.out.println("");
                        System.out.print("Reviewers: ");
                        for (String reviewers: temp1.reviewers()) {
                        System.out.print(reviewers + ", ");
             }
            }

but.. i get an exception in thread "main" java.lang.NullPointersException at A1.main(A1.java:62). Line 62 is:

for (String reviewers: temp1.reviewers())

Alright.. realized I didnt add brackets to an if statement which fixed the nullpointerexception.. but now its just empty after the class annotations.. will update when I figure this out...





Aucun commentaire:

Enregistrer un commentaire