Get annotation value?

How do you get the values ​​set inside the annotation?

I have the following annotation:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface JsonElement { int type(); } 

And here is the method that uses it in the POJO class

 @JsonElement(type=GETTER_METHOD) public String getUsername{ ........................ } 

And the util class, which uses reflection to check whether this method is present in the JSonElement annotation, 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 know what type() means? I can find if the annotation is present, but I cannot find a method that finds out what value - if any - has been set for type() .

+6
source share
5 answers
 JsonElement jsonElem = methods[i].getAnnotation(JsonElement.class); int itsTypeIs = jsonElem.type(); 

Note that you must be sure that jsonElem not null either with

 isAnnotationPresent(JsonElement.class) 

or simple

 if (jsonElem != null) { } 

check.


Also, if you changed the annotation to

 public @interface JsonElement { int type() default -1; } 

you would not need to specify the type attribute every time @JsonElement in your code - by default it will be -1 .

You can also consider using enum for this instead of some whole flags, for example:

 public enum JsonType { GETTER, SETTER, OTHER; } public @interface JsonElement { JsonType type() default JsonType.OTHER; } 
+16
source

You can check if the annotation belongs to JSonElement, if so, you can use and call your methods

If you loop all the annotations, then

 for(Annotation annotation : methods[i].getAnnotations()) { if(annotation instanceOf(JsonElement)){ ((JsonElement)annotation).getType(); } } 

or

 JSonElement jSonElement = methods[i].getAnnotations(JSonElement.class); jSonElement.getType(); 
+3
source
 JsonElement jsonElement = methods[i].getAnnotation(JsonElement.class); int type = jsonElement.type(); 
+2
source

decision:

  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; } 
0
source

From what I understand, your code does: - iterate over declared methods - check if the current method is declared using JsonElement.class, its name starts with GET_CHAR_SEQUENCE, and the value of the annotation type is GETTER_METHOD. - you build your json in accordance with the conditions

I do not see that you are changing the current value of a member like the annotation itself. It seems you no longer need.

But does anyone know how to solve this problem?

0
source

Source: https://habr.com/ru/post/916237/


All Articles