How to Get Annotations of a Member Variable

How to get annotations of a member variable?

for(Field field : cls.getDeclaredFields()){
Class type = field.getType();
String name = field.getName();
Annotation[] annotations = field.getDeclaredAnnotations();
}

See also: http://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html

Getting annotated variables from a class

/** Annotation declaration */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface isSearchable{
//...
}

@isSearchable
public String anyField = "any value";

checking like:

//use MyClass.class.getDeclaredFields() if you want the fields only for this class.
//.getFields() returns the fields for all the class hierarchy
for(Field field : MyClass.class.getFields()){
isSearchable s = field.getAnnotation(isSearchable.class);
if (s != null) {
//field has the annotation isSearchable
} else {
//field has not the annotation
}
}

Sonar-Java how to get annotations of a variable class

 @Override
public void visitNode(Tree tree) {
org.sonar.plugins.java.api.tree.VariableTree variableTree = (VariableTree) tree;

if (variableTree.type().symbolType().symbol().metadata().isAnnotatedWith(SCOPE_ANNOTATION_FQN)) {
System.out.println("prototype annotation found " + variableTree.symbol().metadata().toString());
reportIssue(variableTree.simpleName(), "This spring bean is of type PROTOTYPE");
}
}

Found a way to read annotations on a variable class.

easy way to get annotation value of object member in Java

You cannot get the annotations of a field given it's value.

When you are calling your "Util" method, all you are passing it is the value of the field. It won't have enough information to access the annotation.

Here is a way you might do it...

public class SomeClass {

@Digits(fraction = 2, integer = 16)
private BigDecimal amount;

private void setAmount(double d) {
amount = BigDecimal.valueOf(d);

amount.setScale(Util.getFraction(this, "amount"));
}

public static void main(String[] args) {
new SomeClass().setAmount(12.3);
}
}

Notice I'm passing the instance of the object that the field is in and the name of the field to my "Util" method.

It can now get the actual value like this...

public class Util {

public static int getFraction(Object obj, String fieldName) {
try {
Digits annotation = obj.getClass().getDeclaredField(fieldName).getAnnotation(Digits.class);
return annotation.fraction();
} catch (NoSuchFieldException | SecurityException e) {
// BOOM!
throw new IllegalStateException("Something went awfully wrong...", e);
}
}
}

How to get type annotations for class members?

You could define the instance attribute type in the class body, as described in the PEP

from typing import Optional, get_type_hints

class Foo:
baz: Optional[int]

def __init__(self):
self.baz = None

get_type_hints(Foo)

Out[26]: {'baz': typing.Union[int, NoneType]}

Note that typing.Union[int, NoneType] is the same as Optional[int].

Can I access class variable from created own annotation

You can not get the annotated class with plain java. You have to use tool like org.reflections or annotation indexer to get a list of all annotated java.lang.Classes.



Related Topics



Leave a reply



Submit