gpt4 book ai didi

java - ElementType.LOCAL_VARIABLE 注解类型

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:21 24 4
gpt4 key购买 nike

我想创建自己的注释来注释一些局部变量。写注解不是问题,问题是在运行时获取它们的信息。我只能从带注释的方法或方法参数中获取一些信息,而不能从局部变量中获取。有没有办法得到它?

我自己的注解是这样的:

public void m(int a)  
@MyOwnAnnotation(some information)
int b = 5;
}

或者,作为替代方案,有没有办法获取方法的代码,进一步解析它并最终获得注释值?

提前致谢。

最佳答案

通过反射,您无法检索局部变量。所以你不能通过反射检索局部变量上的注释。我认为这种注释仅用于编译器警告。

可以看看http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html

Local variable annotations are not retained in class files (or at runtime) regardless of the retention policy set on the annotation type. See JLS 9.6.1.2.

如果您不想检索方法代码,可以使用 JavaParser ( http://javaparser.org/ )。

关于java - ElementType.LOCAL_VARIABLE 注解类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17237813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com