2011-07-06 26 views
27

假設我有這樣的註解類的Java尋求與特定的註解和註釋元素的方法


@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MethodXY { 
    public int x(); 
    public int y(); 
} 

public class AnnotationTest { 
    @MethodXY(x=5, y=5) 
    public void myMethodA(){ ... } 

    @MethodXY(x=3, y=2) 
    public void myMethodB(){ ... } 
} 

那麼,有沒有辦法尋找到一個對象,「求」出與@MethodXY註釋的方法,其元素x = 3,y = 2,並調用它?

感謝

回答

45

這裏有一個方法,它返回的具體註釋方法:

public static List<Method> getMethodsAnnotatedWith(final Class<?> type, final Class<? extends Annotation> annotation) { 
    final List<Method> methods = new ArrayList<Method>(); 
    Class<?> klass = type; 
    while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance 
     // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation 
     final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(klass.getDeclaredMethods()));  
     for (final Method method : allMethods) { 
      if (method.isAnnotationPresent(annotation)) { 
       Annotation annotInstance = method.getAnnotation(annotation); 
       // TODO process annotInstance 
       methods.add(method); 
      } 
     } 
     // move to the upper class in the hierarchy in search for more methods 
     klass = klass.getSuperclass(); 
    } 
    return methods; 
} 

它可以很容易地修改您的特定需求。請注意,所提供的方法遍歷類層次結構以查找具有所需註釋的方法。

這裏是您的特定需求的方法:

public static List<Method> getMethodsAnnotatedWithMethodXY(final Class<?> type) { 
    final List<Method> methods = new ArrayList<Method>(); 
    Class<?> klass = type; 
    while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance 
     // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation 
     final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(klass.getDeclaredMethods())); 
     for (final Method method : allMethods) { 
      if (method.isAnnotationPresent(MethodXY.class)) { 
       MethodXY annotInstance = method.getAnnotation(MethodXY.class); 
       if (annotInstance.x() == 3 && annotInstance.y() == 2) {   
        methods.add(method); 
       } 
      } 
     } 
     // move to the upper class in the hierarchy in search for more methods 
     klass = klass.getSuperclass(); 
    } 
    return methods; 
} 

對於發現的方法(S)的調用請參考一tutorial。這裏可能遇到的困難之一是方法參數的數量,這些參數可能因所找到的方法而異,因此需要一些額外的處理。

+4

我想每一個代碼示例到目前爲止,我所看到的一切涉及「掃描」的所有方法 – nobody

+0

剛擡起頭,註釋可能是在該行註釋annotInstance =方法無效。 getAnnotation(註釋); – kliron

+0

@kliron有警衛'if(method.isAnnotationPresent(MethodXY.class))...'爲什麼'null'? – 01es

3

試試這個代碼示例:

import java.lang.annotation.Annotation; 
import java.lang.reflect.Method; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 
import java.lang.annotation.ElementType; 
import java.lang.reflect.InvocationTargetException; 

class AnotTest { 
    public static void main(String... args) { 
     AnnotationTest at = new AnnotationTest(); 
     for (Method m : at.getClass().getMethods()) { 
      MethodXY mXY = (MethodXY)m.getAnnotation(MethodXY.class); 
      if (mXY != null) { 
       if (mXY.x() == 3 && mXY.y() == 2){ 
        try { 
         m.invoke(at); 
        } catch (IllegalAccessException e) { 
         //do nothing; 
        } catch (InvocationTargetException o) { 
         //do nothing; 
        } 
       } 
      } 
     } 
    } 
    @Retention(RetentionPolicy.RUNTIME) 
    @Target(ElementType.METHOD) 
    static public @interface MethodXY { 
     public int x(); 
     public int y(); 
    } 

    static class AnnotationTest { 
     @MethodXY(x=5, y=5) 
     public void myMethodA() { 
      System.out.println("boo"); 
     } 

     @MethodXY(x=3, y=2) 
     public void myMethodB() { 
      System.out.println("foo"); 
     } 
    } 
} 
+0

你能解釋你做了什麼嗎? – tilaprimera

+1

@tilaprimera創建AnnotationTest的新實例,獲取定義類的所有方法,迭代遍歷方法,在每個方法上嘗試獲得名爲MethodXY的註釋,如果我們能夠獲得註釋,那麼我們將檢查「x」和如果它們分別是3和2,我們就調用這個方法。 – dhblah

相關問題