Java反射获得注解大全
这里记录着一些方便以后获得定位注解的方法,推荐收藏,方便以后快速定位,没有甲骨文话术,全是代码,有错误或者补充欢迎留言
前提条件
-
方便测试的注解
&: 此类中的order
用于标记存放的位置@Retention(RetentionPolicy.RUNTIME) @Target({ TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE }) @interface Mark { /** * @return 注解位置 */ int order(); }
-
放置注解的对象
@Mark(order = 7) static class TargetRef<@Mark(order = 8) X> { @Mark(order = 9) private final List<@Mark(order = 10) X> x = null; @Mark(order = 1) public <@Mark(order = 2) T> List<@Mark(order = 3) T> target(@Mark(order = 4) List<@Mark(order = 5) T> t) throws @Mark(order = 6) Exception { throw new IllegalAccessException("test method"); } }
实现方法
公共方法
Method getTargetMethod() {
try {
return TargetRef.class.getDeclaredMethod("target", List.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
获得@Mark(order=1)
@Test
public void getMarkOrder1() {
Method targetMethod = getTargetMethod();
Mark mark = targetMethod.getDeclaredAnnotation(Mark.class);
System.out.println(mark);
}
获得@Mark(order=2)
@Test
public void getMarkOrder2() {
Method targetMethod = getTargetMethod();
ParameterizedType genericReturnType = (ParameterizedType) targetMethod.getGenericReturnType();
Type[] actualTypeArguments = genericReturnType.getActualTypeArguments();
for (Type type : actualTypeArguments) {
AnnotatedElement variable = (AnnotatedElement) type;
Mark mark = variable.getDeclaredAnnotation(Mark.class);
System.out.println(mark);
}
}
获得@Mark(order=3)
@Test
public void getMarkOrder3() {
Method targetMethod = getTargetMethod();
AnnotatedParameterizedType returnType = (AnnotatedParameterizedType) targetMethod.getAnnotatedReturnType();
for (AnnotatedType argument : returnType.getAnnotatedActualTypeArguments()) {
Mark mark = argument.getAnnotation(Mark.class);
System.out.println(mark);
}
}
获得@Mark(order=4)
@Test
public void getMarkOrder4() {
Method targetMethod = getTargetMethod();
System.out.println("Params Ann: " + Arrays.deepToString(targetMethod.getParameterAnnotations()));
}
获得@Mark(order=5)
@Test
public void getMarkOrder5() {
Method targetMethod = getTargetMethod();
for (Parameter parameter : targetMethod.getParameters()) {
AnnotatedType x = parameter.getAnnotatedType();
AnnotatedParameterizedType tp = (AnnotatedParameterizedType) x;
for (AnnotatedType argument : tp.getAnnotatedActualTypeArguments()) {
System.out.println("Param {" + parameter.getName() + "}: " + argument.getType() + ": " + Arrays.toString(argument.getAnnotations()));
}
}
}
获得@Mark(order=6)
@Test
public void getMarkOrder6() {
Method targetMethod = getTargetMethod();
for (AnnotatedType et : targetMethod.getAnnotatedExceptionTypes()) {
System.out.println("Throws: {" + et.getType() + "}: " + Arrays.toString(et.getDeclaredAnnotations()));
}
}
获得@Mark(order=7)
@Test
public void getMarkOrder7() {
Mark mark = TargetRef.class.getDeclaredAnnotation(Mark.class);
System.out.println(mark);
}
获得@Mark(order=8)
@Test
public void getMarkOrder8() {
TypeVariable<Class<TargetRef>>[] variables = TargetRef.class.getTypeParameters();
for (TypeVariable<Class<TargetRef>> variable : variables) {
Mark mark = variable.getDeclaredAnnotation(Mark.class);
System.out.println(variable + ": " + mark);
}
}
获得@Mark(order=9)
@Test
public void getMarkOrder9() throws NoSuchFieldException {
Field field = TargetRef.class.getDeclaredField("x");
Mark mark = field.getDeclaredAnnotation(Mark.class);
System.out.println(mark);
}
获得@Mark(order=10)
@Test
public void getMarkOrder10() throws NoSuchFieldException {
Field field = TargetRef.class.getDeclaredField("x");
AnnotatedParameterizedType type = (AnnotatedParameterizedType) field.getAnnotatedType();
for (AnnotatedType argument : type.getAnnotatedActualTypeArguments()) {
System.out.println(argument.getDeclaredAnnotation(Mark.class));
}
}