2015-03-13 76 views
1

是否可以在对象的setter中获取当前实例的变量名称?获取有关实例变量名称的内部setter信息

像这样的事情

public class Class { 
    private DataType dataType; 
} 

public class DataType { 

    public void setValue(String value) { 
     if (variableName is 'dataType') { 
      this.value = value; 
     } else { 
      this.value = null; 
     } 
    } 
} 

如果使用标准的实用工具则可以创建某种注解存储有变量名,然后在二传手使用它是不可能的?

当我尝试这样做 - 注释为空。 创建注释

@Retention(RetentionPolicy.CLASS) 
@Target(ElementType.FIELD) 
public @interface FieldName { 

    String fieldName(); 
} 

然后我把它添加到现场

public class Class { 
    @FieldName(fieldName = "dataType") 
    private DataType dataType; 
} 

当我设法得到它在DataTypegetter - 注释FieldName为空。

private String wrapGetter(String requiredFieldName, String dataTypeField) { 
    FieldName fieldName = this.getClass().getAnnotation(FieldName.class); 
    if (fieldName.fieldName().equals(requiredFieldName)) { 
     return dataTypeField; 
    } else { 
     return dataTypeField; 
    } 
} 
+1

的可能重复[Java反射:如何获得一个变量的名称(HTTP://计算器.com/questions/744226/java-reflection-how-to-get-the-name-of-variable) – Batty 2015-03-13 09:15:39

+0

什么是变量名? – SMA 2015-03-13 09:16:17

+0

@SMA呃......更确切地说,我的意思是字段名称 - 在这种情况下是'dataType'。 – lapots 2015-03-13 09:18:53

回答

1

有几个问题,你正在尝试做的事:

  1. RetentionPolicy是将其设置为CLASS这意味着类加载器会丢弃它,它不会在运行时可用。您应该改用RetentionPolicy.RUNTIME

  2. this.getClass().getAnnotation(FieldName.class)会给你类的注释。

在下面的例子中,注释不为空,你可以在setValue方法得到"example"字符串:

@FieldName(fieldName = "example") 
public class DataType { 

    public void setValue(String value) { 
     System.out.println(this.getClass().getAnnotation(FieldName.class)); 
    } 

} 

public static void main(String[] args) { 
    new DataType().setValue("ignored"); 
} 

这也需要您的注释的目标更改为@Target(ElementType.TYPE)

  1. 变量或字段名称只是一个引用,它指向内存中的某个对象。你可以有多个对同一个对象的引用。虽然你可以在不同的类中注解字段,但是这对局部变量和参数来说是一个问题 - 很难说,因为我不知道你想要实现什么。

有问题的例子:

public class ClassOne { 
    @FieldName(fieldName = "dataType") 
    private DataType a; 
} 

public class ClassTwo { 
    @FieldName(fieldName = "dataType") 
    private DataType b; 
} 

public class ClassThree { 
    public void doSomething() { 
     DataType c = new DataType(); 
    } 
} 

public class ClassFour { 
    public void doSomething(DataType d) { 
     // ... 
    } 
} 

一般情况下,这一切都归结于该类的实例没有关于它是如何被称为信息的问题。但是,对于该领域,封闭类具有此信息。考虑将你的方法转移到该类中。你可以处理这个问题,没有任何注解:

public class DataType { 
    public void setValue(String value) { 
     // ... 
    } 
} 

public class ClassOne { 
    private DataType dataType; 

    public void setDataTypeValue(String value) { 
     dataType.setValue(value); 
    } 
} 

public class ClassTwo { 
    private DataType anyOtherFieldName; 

    public void setDataTypeValue(String value) { 
     anyOtherFieldName.setValue(null); 
    } 
} 

,设置为空,而忽略参数设置器非常误导,你的IDE应该给你一个关于未使用的参数警告,这不是没有原因的。我认为你应该考虑重新设计,但如果不知道更多的细节,我不能给你提供更多建议。

不是解决问题,而是尝试解决问题的原因。

0

使用@Retention(RetentionPolicy.RUNTIME)

替换此

FieldName fieldNameAnnotation = field.getAnnotation(FieldName.class); 

有了这个

Field field = this.getClass().getField("dataType"); 
FieldName fieldName = field.getAnnotation(FieldName.class);