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()
.
source share