【问题标题】:get annotation value?获取注释值?
【发布时间】:2012-05-21 09:45:27
【问题描述】:

如何获取注解中设置的值?

我定义了以下注释:

@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;
        }

我如何找出type() 的值是什么?我可以找到注释是否存在,但我找不到一种方法来找出为 type() 设置的值(如果有的话)。

【问题讨论】:

    标签: java reflection annotations


    【解决方案1】:
    JsonElement jsonElem = methods[i].getAnnotation(JsonElement.class);
    int itsTypeIs = jsonElem.type();
    

    请注意,您必须确保jsonElem 不是您的null

    isAnnotationPresent(JsonElement.class)
    

    还是简单的

    if (jsonElem != null) {
    }
    

    检查。


    此外,如果您将注释更改为

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

    您不必在代码中每次出现 @JsonElement 时都声明 type 属性 - 它会默认为 -1

    您也可以考虑为此使用enum 而不是一些整数标志,例如:

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

    【讨论】:

    • 是的,我成功了;谢谢。他们是使“type()”可选的方法吗?到目前为止,type() 必须在 JsonElement 注释中声明
    【解决方案2】:

    您可以检查注解是否属于 JSonElement,如果是,您可以强制转换并调用您的方法

    如果循环遍历所有注解则

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

    JSonElement jSonElement = methods[i].getAnnotations(JSonElement.class);
    jSonElement.getType();
    

    【讨论】:

      【解决方案3】:
      JsonElement jsonElement = methods[i].getAnnotation(JsonElement.class);
      
      int type = jsonElement.type();
      

      【讨论】:

        【解决方案4】:

        解决方案:

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

        【讨论】:

          【解决方案5】:

          据我了解,您的代码可以: - 遍历声明的方法 - 检查当前方法是否使用 JsonElement.class 注释,其名称以 GET_CHAR_SEQUENCE 开头,并且注释类型的值等于 GETTER_METHOD。 - 你根据条件构建你的json

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-19
            • 2020-05-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多