2011-09-19 34 views
4

不幸的是,似乎註解繼承受到只能從類(而不是接口)繼承的類級註釋的嚴重限制。如何發現子類中的方法(參數)是否在已實現的接口中定義了註釋?

考慮以下代碼:

interface Foo { 
    @A 
    void bar(String str, @B int i); 
} 

class FooImpl implements Foo { 
    void bar(String str, @B int i) { ... } 
} 

如果我有FooImpl一個實例可以發現,如果該方法已經標註了A(無論是在類(容易)或實現的接口)?

方法參數怎麼樣?是否有可能dicover如果和哪個參數已被註釋B

看來,這是不可能與AspectJ和我需要使用Java反射。

固體解決方案的外觀如何?

回答

3

它可能在類對象上使用getInterfaces()並查詢結果。

參數註釋

package mawi12345; 
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.PARAMETER) 
public @interface B { 
    int version() default 0; 
} 

方法註解

與註釋界面

package mawi12345; 
public interface Foo { 
    @Revision(minor=1, major=3) 
    public void setMark(@B(version=3) int mark); 
} 

用註解類和富接口

package mawi12345; 
public class Test implements Foo { 

    public void setMark(int mark) { 

    } 

    @Revision(minor=2, major=4) 
    public boolean isPassed() { 
     return true; 
    } 
} 

測試類

package mawi12345; 

import java.lang.annotation.Annotation; 
import java.lang.reflect.Method; 

public class ReflectionTest { 

    public static void printAnnotations(Class<?> clazz) { 
     // array of methods 
     Method[] methods = clazz.getDeclaredMethods(); 
     System.out.println("found "+methods.length+" methods"); 
     for (int i=0; i<methods.length; i++) { 
      // get the annotations of this method 
      Annotation[] methodAnnotations = methods[i].getAnnotations(); 
      // if you only wont to check for one annotation use getAnnotation(Class<T>) 

      for (Annotation methodAnnotation : methodAnnotations) { 
       System.out.println(methodAnnotation); 
      } 
      // get the parameter annotations (2d array) 
      Annotation[][] parameterAnnotations = methods[i].getParameterAnnotations(); 
      // get an array of parameters 
      Class<?>[] parameters = methods[i].getParameterTypes(); 
      for(int x=0; x<parameterAnnotations.length; x++) { 
       Class<?> parameter = parameters[x]; 
       for(Annotation annotation : parameterAnnotations[x]){ 
        // print the parameter name and his annotation 
        System.out.println(parameter.getName() + " " + annotation); 
       } 
      } 
     } 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // create Test object 
     Test test = new Test(); 
     // get the class 
     Class<?> clazz = test.getClass(); 
     System.out.println("Class Test"); 
     // print annotations 
     printAnnotations(clazz); 
     System.out.println(); 

     // get the interfaces of the class 
     Class<?>[] interfaces = clazz.getInterfaces(); 

     System.out.println("found "+interfaces.length+" interfaces"); 
     // print annotations for each interface 
     for (Class<?> type : interfaces) { 
      System.out.println(type); 
      printAnnotations(type); 
     } 

    } 

} 

輸出

Class Test 
found 2 methods 
@mawi12345.Revision(minor=2, major=4) 

found 1 interfaces 
interface mawi12345.Foo 
found 1 methods 
@mawi12345.Revision(minor=1, major=3) 
int @mawi12345.B(version=3) 
相關問題