ElementType.LOCAL_VARIABLE 注释类型 [英] ElementType.LOCAL_VARIABLE annotation type

查看:29
本文介绍了ElementType.LOCAL_VARIABLE 注释类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的注释来注释一些局部变量.写注解不是问题,问题是在Runtime获取它们的信息.我只能从带注释的方法或方法参数中获取一些信息,而不能从局部变量中获取一些信息.有什么办法可以得到吗?

I`d like to create my own annotations to annotate some local variable. To write the annotation is not the problem, the problem is to get the information of them at the Runtime. I could only get some information from annotated methods or method parameters, but not from local variables. Is there any way to get it?

我自己的注释是这样的:

My own annotation is something like that:

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

或者,作为替代,有什么方法可以获取方法的代码,进一步解析并最终获取注释值?

Or, as an alternative, is there any way to get the code of the method, to parse it further and finally get annotation value?

提前致谢.

推荐答案

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

With reflection you can't retrieve a local variable. So you can't retrieve an annotation on a local variable via reflection. I think that this kind of annotation is only used for compiler warnings.

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

无论在注释类型上设置的保留策略如何,局部变量注释都不会保留在类文件中(或在运行时).请参阅 JLS 9.6.1.2.

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/).

If you wan't to retrieve method code, you can use JavaParser (http://javaparser.org/).

这篇关于ElementType.LOCAL_VARIABLE 注释类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆