您的位置:首页 > 产品设计 > UI/UE

How to set field values using Java reflection

2015-12-18 18:28 393 查看
In the Java reflection API, the java.lang.reflect.Field class represents a generic field in a class or interface, and allows to retrieve programmatically field information, such as name, type, or annotations.
In addition, via the Field class we can get and set the value of a field in a given object.
The most generic method to set a field value is set(Object obj, Object value): it takes as first argument the object on which the field needs to be set, and as second argument the value for the field. The above
method can be used for any field type, either primitive or class type; in case of a primitive type, value must be an instance of the Java class corresponding to the type, such as java.lang.Integerjava.lang.Float,
and so on. For primitive types, there also exist specific methods that can be called passing them directly primitive type values:
·         setBoolean(Object obj, boolean z)
·         setByte(Object obj, byte b)
·         setChar(Object obj, char c)
·         setDouble(Object obj, double d)
·         setFloat(Object obj, float f)
·         setInt(Object obj, int i)
·         setLong(Object obj, long l)
·         setShort(Object obj, short s)
As with the generic set() method, in all the methods in the above list the first argument is the object whose field needs to be set and the second argument indicates the value for the field.
In the example below, we create an object of the java.awt.Rectangle class and then set field values in that object by using the setInt() method; to verify that field values are actually set, we retrieve these
values, using the getInt(Object obj) method of the Field class, before and after setting them.
import java.awt.*;import java.lang.reflect.*;public class SetFieldValueExample {  public static void main(String[] args) throws Exception {    Rectangle rect = new Rectangle();    Field xField = rect.getClass().getField("x");    Field yField = rect.getClass().getField("y");      Field widthField = rect.getClass().getField("width");    Field heightField = rect.getClass().getField("height");    System.out.println("---->> Before setting values <<----");    System.out.println("x value: " + xField.getInt(rect));    System.out.println("y value: " + yField.getInt(rect));    System.out.println("width value: " + widthField.getInt(rect));    System.out.println("height value: " + heightField.getInt(rect));    xField.setInt(rect, 10);    yField.setInt(rect, 20);    widthField.setInt(rect, 40);    heightField.setInt(rect, 80);    System.out.println("---->> After setting values <<----");    System.out.println("x value: " + xField.getInt(rect));    System.out.println("y value: " + yField.getInt(rect));      System.out.println("width value: " + widthField.getInt(rect));    System.out.println("height value: " + heightField.getInt(rect));  }}

The output of the above program demonstrates that all four field values have been correctly set:
 

 ---->> Before setting values <<----

 x field value: 0

 y field value: 0

 width field value: 0


 height field value: 0

 ---->> After setting values <<----

 x field value: 10

 y field value: 20

 width field value: 40


 height field value: 80

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java