Java 9+无法获取方法局部变量的注解
Java 9+ unable to get annotations of local variables of method
自 Java 9 起,以 LOCAL_VARIABLE 作为目标的注释的保留方式已发生变化。 Now the bytecode representation will hold information about annotated local variables in each method. 此外,规范指出 If m has an element whose value is java.lang.annotation.RetentionPolicy.RUNTIME, the reflection libraries of the Java SE Platform must make a available at run time.
,如果我理解正确,它确认至少应该通过反射提供方法的局部变量上的注释。
但是,我一直在尝试使用反射来查找注释(或注释的局部变量,或它们的值),但没有成功。
因此,是否有人知道如何执行此操作或是否可能?我正在使用 java 11.
例如,这是我的 Main class,我尝试从测试方法中获取 @Parallel
注释。
public class Main {
public static void main(String[] args) {
try {
System.out.println("Main.class.getDeclaredMethod(\"test\").getAnnotations().length = " +
Main.class.getDeclaredMethod("test").getAnnotations().length);
System.out.println("Main.class.getDeclaredMethod(\"test\").getDeclaredAnnotations().length = " +
Main.class.getDeclaredMethod("test").getDeclaredAnnotations().length);
Annotation annotation = Main.class.getDeclaredMethod("test").getAnnotation(Parallel.class);
System.out.println("annotation = " + annotation);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
void test() {
@Parallel int a = 2;
@Parallel int b = 4;
}
}
这是我的注释实现。我刚刚将所有内容添加为目标,因为我只是在测试它:
@Target({ ElementType.LOCAL_VARIABLE, ElementType.TYPE, ElementType.TYPE_USE, ElementType.TYPE_PARAMETER,
ElementType.FIELD, ElementType.PARAMETER, ElementType.PACKAGE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Parallel {
}
运行 javap -l -v -c -s Main.class
将显示:
void test();
descriptor: ()V
flags: (0x0000)
Code:
stack=1, locals=3, args_size=1
0: iconst_2
1: istore_1
2: iconst_4
3: istore_2
4: return
LineNumberTable:
line 22: 0
line 24: 2
line 25: 4
RuntimeVisibleTypeAnnotations:
0: #27(): LOCAL_VARIABLE, {start_pc=2, length=3, index=1}
Parallel
1: #27(): LOCAL_VARIABLE, {start_pc=4, length=1, index=2}
Parallel
很明显局部变量的注解信息是有的,就是不知道反射能不能取到(或者有没有其他方法在运行时取到)
.class
文件包含的信息比通过反射公开的信息多,但反射不会公开有关方法内局部变量的信息。
我没有找到正式说明的地方,但可以指向 java.lang.reflect.Method
which has no methods exposing anything about local variables. Looking at the rest of the the reflection package 的 Javadoc,没有比 Method
.[=14= 更合适的了。 ]
最后,我使用低级字节码处理库 ASM 设法获取了有关它们的注释和字节码信息。
自 Java 9 起,以 LOCAL_VARIABLE 作为目标的注释的保留方式已发生变化。 Now the bytecode representation will hold information about annotated local variables in each method. 此外,规范指出 If m has an element whose value is java.lang.annotation.RetentionPolicy.RUNTIME, the reflection libraries of the Java SE Platform must make a available at run time.
,如果我理解正确,它确认至少应该通过反射提供方法的局部变量上的注释。
但是,我一直在尝试使用反射来查找注释(或注释的局部变量,或它们的值),但没有成功。
因此,是否有人知道如何执行此操作或是否可能?我正在使用 java 11.
例如,这是我的 Main class,我尝试从测试方法中获取 @Parallel
注释。
public class Main {
public static void main(String[] args) {
try {
System.out.println("Main.class.getDeclaredMethod(\"test\").getAnnotations().length = " +
Main.class.getDeclaredMethod("test").getAnnotations().length);
System.out.println("Main.class.getDeclaredMethod(\"test\").getDeclaredAnnotations().length = " +
Main.class.getDeclaredMethod("test").getDeclaredAnnotations().length);
Annotation annotation = Main.class.getDeclaredMethod("test").getAnnotation(Parallel.class);
System.out.println("annotation = " + annotation);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
void test() {
@Parallel int a = 2;
@Parallel int b = 4;
}
}
这是我的注释实现。我刚刚将所有内容添加为目标,因为我只是在测试它:
@Target({ ElementType.LOCAL_VARIABLE, ElementType.TYPE, ElementType.TYPE_USE, ElementType.TYPE_PARAMETER,
ElementType.FIELD, ElementType.PARAMETER, ElementType.PACKAGE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Parallel {
}
运行 javap -l -v -c -s Main.class
将显示:
void test();
descriptor: ()V
flags: (0x0000)
Code:
stack=1, locals=3, args_size=1
0: iconst_2
1: istore_1
2: iconst_4
3: istore_2
4: return
LineNumberTable:
line 22: 0
line 24: 2
line 25: 4
RuntimeVisibleTypeAnnotations:
0: #27(): LOCAL_VARIABLE, {start_pc=2, length=3, index=1}
Parallel
1: #27(): LOCAL_VARIABLE, {start_pc=4, length=1, index=2}
Parallel
很明显局部变量的注解信息是有的,就是不知道反射能不能取到(或者有没有其他方法在运行时取到)
.class
文件包含的信息比通过反射公开的信息多,但反射不会公开有关方法内局部变量的信息。
我没有找到正式说明的地方,但可以指向 java.lang.reflect.Method
which has no methods exposing anything about local variables. Looking at the rest of the the reflection package 的 Javadoc,没有比 Method
.[=14= 更合适的了。 ]
最后,我使用低级字节码处理库 ASM 设法获取了有关它们的注释和字节码信息。