不幸的是,注释继承似乎受到了严重的限制,因为只有来自类(而不是接口)的类级注释才能被继承。
考虑以下代码:
interface Foo {
@A
void bar(String str, @B int i);
}
class FooImpl implements Foo {
void bar(String str, @B int i) { ... }
}
如果我有一个FooImpl
的实例,是否有可能发现该方法是否已与A
注释(无论是在类(容易)或在实现的接口)?
方法参数呢?是否有可能发现是否以及哪个参数被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;
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);
}
}
}
输出Class Test
found 2 methods
@mawi12345.Revision(minor=2, major=4)
found 1 interfaces
interface mawi12345.Foo
found 1 methods
@mawi12345.Revision(minor=1, major=3)
int @mawi12345.B(version=3)