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.Integer, java.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.
The output of the above program demonstrates that all four field values have been correctly set:
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.Integer, java.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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树