2012-03-12 87 views
0

我將使用特定示例使其更容易理解。將自定義方法添加到系統類

而不是使用:

Annotation[] annotations = method.getAnnotations(); 

我希望能夠得到與每個註釋相關聯的類類型的數組:

Class<?>[] annotationClasses = method.getAnnotationTypes(); 

有沒有辦法覆蓋的方法類,並添加我的自定義方法,它將使用getAnnotations來收集註釋,但返回類類型而不是Annotation類型?

回答

1

沒有辦法完成你正在請求的內容。 Method是最終的,不能擴展。

但是,你可以做

Class <?> [ ] getDeclaredAnnotations (Method method) 
{ 
     Annotation [ ] as = method . getDeclaredAnnotations () ; 
     Class <?> [ ] bs = new Class <?> [ as . length ] ; 
     for (int i = 0 ; i < as . length ; i ++) 
     { 
       Annotation a = as [ i ] ; 
       b [ i ] = a ; 
     } 
     return bs ; 
} 
相關問題