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

Java 将自定义方法添加到系统类,java,methods,Java,Methods,我将使用一个具体的例子使其更容易理解 而不是使用: Annotation[] annotations = method.getAnnotations(); 我希望能够获得与每个注释关联的类类型数组: Class<?>[] annotationClasses = method.getAnnotationTypes(); Class[]annotationClasses=method.getAnnotationTypes(); 是否有方法重写Method类并添加我的自定义方法,该方

我将使用一个具体的例子使其更容易理解

而不是使用:

Annotation[] annotations = method.getAnnotations();
我希望能够获得与每个注释关联的类类型数组:

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

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

无法准确执行您的请求<代码>方法是最终的,不能扩展

但是,你可以这样做

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 ;
}
Class<?>[]getDeclaredAnnotations(方法)
{
注释[]as=method.getDeclaredAnnotations();
类别<?>[]bs=新类别<?>[作为长度];
for(int i=0;i