Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElementType.LOCAL_VARIABLE annotation type

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?

Thanks in advance.

like image 248
Dmitry Sobetsky Avatar asked Jun 21 '13 14:06

Dmitry Sobetsky


1 Answers

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.

You can look 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.

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

like image 179
PomPom Avatar answered Sep 26 '22 16:09

PomPom