Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用注释值获取属性_Java_Annotations - Fatal编程技术网

Java 使用注释值获取属性

Java 使用注释值获取属性,java,annotations,Java,Annotations,我想使用以下类的execute()方法: public class Parser { @Header("header1") private String attribute1; @Header("header2") private String attribute2; @Header("header3") private String attribute3; @Header("header4") private String

我想使用以下类的execute()方法:

public class Parser {
    @Header("header1")
    private String attribute1;

    @Header("header2")
    private String attribute2;

    @Header("header3")
    private String attribute3;

    @Header("header4")
    private String attribute4;

    public String execute(String headerValue) {
        //Execute
    }
}
我希望此方法实现的是将headerValue参数与@Header注释列表中的参数匹配,并返回相应属性的值。例如,如果我调用execute(“header3”),它应该返回attribute3的值


我怎样才能做到这一点?或者这是编写此需求的更好方法吗?

为什么不为此使用映射?无论如何,为了存储注释参数值到字段的映射,您都需要一个,但是如果您可以在不进行反射的情况下这样做,那么编码和维护应该会更容易

我的意思是:

Map<String, String> attributes; //initialized

attributes.put("header1", value1);
...

如果您需要为头类型使用一个字符串,可以考虑使用一个附加的映射字符串->头类型来查找正确的类型。

或者,您可以使用switch语句,因为Java 7也应该使用字符串。

尝试以下操作:

public String execute(String headerValue) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException {
  for(Field field:this.getClass().getFields()) {
    if (field.isAnnotationPresent(Header.class)) {
            Header annotation = field.getAnnotation(Header.class); 

            String name = annotation.value(); 
            if(name.equals(headerValue)) {
            Object val = this.getClass().getField(name).get(this);
            return (String) val;
            }
            }
    }    
    return null;
}
有两个异常需要处理:

Object val=this.getClass().getField(name).get(this)

如果不想从此方法中抛出异常,则可以为该异常返回null。

这可能会对您有所帮助
This may help you


     Field f[]= Parser.class.getDeclaredFields();
     for (int i = 0; i < f.length; i++) {

     Annotation annotation[]= f[i].getAnnotations();
     for (int j=0;j<annotation.length;j++){

     Class<Annotation> type = (Class<Annotation>) annotation[j].annotationType();               

        for (Method method : type.getDeclaredMethods()) {

            if(method.getName() .equals(headerValue))
            {
            String name=f[i].getName();
           return name;
            }
        }

     }
     } 
字段f[]=Parser.class.getDeclaredFields(); 对于(int i=0;i对于(int j=0;jI希望用这个得到更干净的东西。显然不是这样。你想详细说明一下吗?
This may help you


     Field f[]= Parser.class.getDeclaredFields();
     for (int i = 0; i < f.length; i++) {

     Annotation annotation[]= f[i].getAnnotations();
     for (int j=0;j<annotation.length;j++){

     Class<Annotation> type = (Class<Annotation>) annotation[j].annotationType();               

        for (Method method : type.getDeclaredMethods()) {

            if(method.getName() .equals(headerValue))
            {
            String name=f[i].getName();
           return name;
            }
        }

     }
     }