Reputation: 18128
How do you get values set inside a annotation?
I have the following annotation defined:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonElement {
int type();
}
And here is the method that uses it in a POJO class
@JsonElement(type=GETTER_METHOD)
public String getUsername{
........................
}
And the util class that uses reflection to check if this method has the JSonElement annotation present and to check what the type value is.
Method methods[] = classObject.getClass().getDeclaredMethods();
JSONObject jsonObject = new JSONObject();
try {
for (int i = 0; i < methods.length; i++) {
String key = methods[i].getName();
System.out.println(key);
if (methods[i].isAnnotationPresent(JsonElement.class) && key.startsWith(GET_CHAR_SEQUENCE)) {
methods[i].getDeclaredAnnotations();
key = key.replaceFirst(GET_CHAR_SEQUENCE, "");
jsonObject.put(key, methods[i].invoke(classObject));
}
}
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
How do I find out what type()
value is? I can find whether the annotation is present, but I can't find a method that finds out what value - if any - was set for type()
.
Upvotes: 7
Views: 24910
Reputation: 3533
JsonElement jsonElement = methods[i].getAnnotation(JsonElement.class);
int type = jsonElement.type();
Upvotes: 2
Reputation: 1
From what i understood, your code does: - iterate over the declared methods - check if current method is annoted with JsonElement.class, its name starts with GET_CHAR_SEQUENCE and the value of annotation type is equal to GETTER_METHOD. - you build your json according conditions
i can't see that you are changing the currenty value of the type member of the annotation itself. Seemes like you did not need it anymore.
But does anyone know how to get this task sorted out?
Upvotes: 0
Reputation: 38444
JsonElement jsonElem = methods[i].getAnnotation(JsonElement.class);
int itsTypeIs = jsonElem.type();
Note that you must be sure that jsonElem
is not null
either by your
isAnnotationPresent(JsonElement.class)
or a simple
if (jsonElem != null) {
}
check.
Additionally, if you changed your annotation to
public @interface JsonElement {
int type() default -1;
}
you wouldn't have to state the type
attribute at every occurence of @JsonElement
in your code - it would default to -1
.
You could also consider using an enum
for this instead of some integer flags, for example:
public enum JsonType {
GETTER, SETTER, OTHER;
}
public @interface JsonElement {
JsonType type() default JsonType.OTHER;
}
Upvotes: 17
Reputation: 18128
solution:
Method methods[] = classObject.getClass().getDeclaredMethods();
JSONObject jsonObject = new JSONObject();
try {
for (int i = 0; i < methods.length; i++) {
String key = methods[i].getName();
System.out.println(key);
if (methods[i].isAnnotationPresent(JsonElement.class)
&& key.startsWith(GET_CHAR_SEQUENCE)
&& (methods[i].getAnnotation(JsonElement.class).type() == GETTER_METHOD)) {
key = key.replaceFirst(GET_CHAR_SEQUENCE, "");
jsonObject.put(key, methods[i].invoke(classObject));
}
}
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
Upvotes: 0
Reputation: 20323
You can check if annotation belongs to JSonElement if yes you can cast and call your methods
If looping through all the annotation then
for(Annotation annotation : methods[i].getAnnotations()) {
if(annotation instanceOf(JsonElement)){
((JsonElement)annotation).getType();
}
}
or
JSonElement jSonElement = methods[i].getAnnotations(JSonElement.class);
jSonElement.getType();
Upvotes: 3