forked from GitHubyen/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectHelper.java
78 lines (73 loc) · 2.51 KB
/
ReflectHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.lang.reflect.Field;
/**
* DateTime: 2016/9/20 17:28
* 功能:反射工具
* 思路:
*/
public class ReflectHelper {
//测试
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
Demodemo demodemo=new Demodemo();
System.out.println(getFieldByFieldName(demodemo,"name"));
System.out.println(getValueByFieldName(demodemo,"name"));
demodemo.setName("YEN");
System.out.println(getValueByFieldName(demodemo,"name"));
setValueByFieldName(demodemo,"name","LMC");
System.out.println(getValueByFieldName(demodemo,"name"));
}
/**
* 获取object对象fieldName的Field
* @param obj
* @param fieldName
* @return
*/
public static Field getFieldByFieldName(Object obj,String fieldName){
for ( Class<?> superClass = obj.getClass();superClass!=Object.class;superClass=superClass.getClass() ){
try {
return superClass.getDeclaredField(fieldName);
} catch ( NoSuchFieldException e ) {
e.printStackTrace();
}
}
return null;
}
/**
*获取obj对象fieldName的属性值
* @param object
* @param fieldName
* @return
* @throws IllegalAccessException
*/
public static Object getValueByFieldName(Object object,String fieldName) throws IllegalAccessException {
Field field=getFieldByFieldName(object,fieldName);
Object value=null;
if(null!=field){
if(field.isAccessible()){
value=field.get(object);
}else {
field.setAccessible(true); //如果字段是私有的,那么必须要对这个字段设置field.setAccessible(true) 这样才可以正常使用
value=field.get(object);
field.setAccessible(false);
}
}
return value;
}
/**
* 设置obj对象fieldName的属性值
* @param object
* @param fieldName
* @param value
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static void setValueByFieldName(Object object,String fieldName,Object value) throws NoSuchFieldException, IllegalAccessException {
Field field=object.getClass().getDeclaredField(fieldName);
if(field.isAccessible()){
field.set(object,value);
}else {
field.setAccessible(true);
field.set(object,value);
field.setAccessible(false);
}
}
}