Java 如何发现子类中的方法(参数)是否在实现的接口中定义了注释?
不幸的是,注释继承似乎受到严格限制,因为只能继承来自类(而不是接口)的类级注释 考虑以下代码:Java 如何发现子类中的方法(参数)是否在实现的接口中定义了注释?,java,inheritance,reflection,methods,annotations,Java,Inheritance,Reflection,Methods,Annotations,不幸的是,注释继承似乎受到严格限制,因为只能继承来自类(而不是接口)的类级注释 考虑以下代码: interface Foo { @A void bar(String str, @B int i); } class FooImpl implements Foo { void bar(String str, @B int i) { ... } } 如果我有一个FooImpl的实例,是否可以发现该方法是否已经用A进行了注释(在类中(easy)或在实现的接口中) 方法参数呢?
interface Foo {
@A
void bar(String str, @B int i);
}
class FooImpl implements Foo {
void bar(String str, @B int i) { ... }
}
如果我有一个FooImpl
的实例,是否可以发现该方法是否已经用A
进行了注释(在类中(easy)或在实现的接口中)
方法参数呢?是否可以切换是否以及哪个参数已用B
注释
这在AspectJ中似乎是不可能的,我需要使用Java反射
固溶体是什么样的?它可能会在类对象上使用
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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface Revision {
int minor() default 0;
int major() default 1;
}
方法注释
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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface Revision {
int minor() default 0;
int major() default 1;
}
与注释的接口
package mawi12345;
public interface Foo {
@Revision(minor=1, major=3)
public void setMark(@B(version=3) int mark);
}
使用注释和Foo接口初始化
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);
}
}
}