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 Runti
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/).
As of Java 8, local variable annotations are retained in class files. As noted by Erick Hagstrom, this long-standing bug was fixed by JSR 308, which also added type annotations to the Java language.
However, Java's reflection API has not been updated to give access within method bodies. You will need to parse the classfile yourself. You can use a tool such as ASM. EDIT: I don't recommend JavaParser, because it has not been updated beyond Java 1.5. JavaParser has been updated.
JLS 9.6.1.2 does indeed state that local variable annotations are not retained. However, JSR 308 is working its way through the community process. It should give you the capability you need.
If you want an interim solution, here is an implementation of JSR 308.
At the moment, as mentioned in some other posts, you cannot retrieve a local variable value simply from the annotation alone.
However, I did have a simimlar issue and managed to come up with a solution using fields. Sample code is below:
Interface Class
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface test{
}
PoJo
public class testObject{
@test
private String one;
private String two;
//Getters and Setters
}
Get Object values
public void getFields (Object obj){
Field fields = obj.getClass().getDeclaredFields();
for (Field f : fields){
test fieldAnnotation = f.getAnnotation(test.Class);
if (fieldAnnotation != null){
f.get(obj);
// Do things here based on the values
}
}
}
Main Class
public static void main(String[] args){
//Create object
testObject test = new testObject();
test.setOne("testOne");
test.setTwo("testTwo");
getFields(test);
}
Hopefully this helps in explaining how you can get the fields for an object based on the annotation. You are simply using the annotation to 'mark' the fields you want to retrieve and then reading the value from the object.