Java 获取注释值?

Java 获取注释值?,java,reflection,annotations,Java,Reflection,Annotations,如何在注释中设置值 我定义了以下注释: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface JsonElement { int type(); } 下面是在POJO类中使用它的方法 @JsonElement(type=GETTER_METHOD) public String getUsername{ ........................ } 以及使用反射检查此方

如何在注释中设置值

我定义了以下注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonElement {
    int type();
}
下面是在POJO类中使用它的方法

@JsonElement(type=GETTER_METHOD)
public String getUsername{
........................
}
以及使用反射检查此方法是否存在JSonElement注释并检查类型值的util类

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;
        }
methodmethods[]=classObject.getClass().getDeclaredMethods();
JSONObject JSONObject=新的JSONObject();
试一试{
for(int i=0;i

如何找出
type()
值是什么?我可以找到注释是否存在,但是我找不到一个方法来找出为
type()

设置了什么值(如果有的话),您可以检查注释是否属于JSonElement,如果是,您可以强制转换并调用您的方法

如果循环遍历所有注释,则

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

请注意,您必须确保您的

isAnnotationPresent(JsonElement.class)
还是简单的

if (jsonElem != null) {
}
检查


此外,如果将注释更改为

public @interface JsonElement {
    int type() default -1;
}
您不必在代码中每次出现
@jsonement
时都声明
type
属性-它将默认为
-1

您也可以考虑使用<代码>枚举>代码>代替一些整数标志,例如:

public enum JsonType {
    GETTER, SETTER, OTHER;
}

public @interface JsonElement {
    JsonType type() default JsonType.OTHER;
}
解决方案:

    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;
    }
methodmethods[]=classObject.getClass().getDeclaredMethods();
JSONObject JSONObject=新的JSONObject();
试一试{
for(int i=0;i
据我所知,您的代码: -迭代声明的方法 -检查当前方法是否使用JsonElement.class进行了注释,其名称以GET_CHAR_序列开头,注释类型的值是否等于GETTER_method。 -您可以根据条件构建json

我看不出您正在更改批注本身的类型成员的当前值。 看起来你不再需要它了


但是有人知道如何解决这项任务吗?

是的,我让它工作了;谢谢它们是使“type()”成为可选的方法吗?现在,type()必须在JsonElement注释中声明
public @interface JsonElement {
    int type() default -1;
}
public enum JsonType {
    GETTER, SETTER, OTHER;
}

public @interface JsonElement {
    JsonType type() default JsonType.OTHER;
}
    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;
    }