繁体   English   中英

将自定义方法添加到系统类

[英]Add Custom Method to System Class

我将使用一个特定的示例使其更易于理解。

而不是使用:

Annotation[] annotations = method.getAnnotations();

我希望能够获得与每个注释关联的类类型的数组:

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

有没有办法重写Method类并添加我的自定义方法,该方法将使用getAnnotations来收集注释,但返回类类型而不是Annotation类型?

无法完全按照您的要求进行操作。 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 ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM