2012-03-12 83 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 ; 
} 
相关问题