2015-03-13 75 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);