通过属性名反射获取get和set方法

https://blog.csdn.net/weixin_39800144/article/details/79012369

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;

import everyDay.Person;

/**
* Created by lightClouds917
* Date 2017/12/29
* Description:根据属性名反射获取get和set方法
*/
public class TestReflect3 {

public static void main(String[] args)throws Exception{
//test removeLine
System.out.println(removeLine("abg_dwr"));
System.out.println(removeLine("ab_wr"));
System.out.println(removeLine("abgwr"));
System.out.println(removeLine(null));

//test get
Person person1 = new Person();
person1.setAge(11);
person1.setName("旺旺");
Object ob = getGetMethod(person1, "name");
System.out.println(ob);

//test set
Person person2 = new Person();
String field2 = "name";
setValue(person2, person2.getClass(), field2, Person.class.getDeclaredField(field2).getType(), "汪汪");
System.out.println(person2);
//获取某个属性的类型
System.out.println(Person.class.getDeclaredField("age").getType());

}

/**
* 根据属性,获取get方法
* @param ob 对象
* @param name 属性名
* @return
* @throws Exception
*/
public static Object getGetMethod(Object ob , String name)throws Exception{
Method[] m = ob.getClass().getMethods();
for(int i = 0;i < m.length;i++){
if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
return m[i].invoke(ob);
}
}
return null;
}

/**
* 根据属性,拿到set方法,并把值set到对象中
* @param obj 对象
* @param clazz 对象的class
* @param fileName 需要设置值得属性
* @param typeClass
* @param value
*/
public static void setValue(Object obj,Class<?> clazz,String filedName,Class<?> typeClass,Object value){
filedName = removeLine(filedName);
String methodName = "set" + filedName.substring(0,1).toUpperCase()+filedName.substring(1);
try{
Method method = clazz.getDeclaredMethod(methodName, new Class[]{typeClass});
method.invoke(obj, new Object[]{getClassTypeValue(typeClass, value)});
}catch(Exception ex){
ex.printStackTrace();
}
}

/**
* 通过class类型获取获取对应类型的值
* @param typeClass class类型
* @param value 值
* @return Object
*/
private static Object getClassTypeValue(Class<?> typeClass, Object value){
if(typeClass == int.class || value instanceof Integer){
if(null == value){
return 0;
}
return value;
}else if(typeClass == short.class){
if(null == value){
return 0;
}
return value;
}else if(typeClass == byte.class){
if(null == value){
return 0;
}
return value;
}else if(typeClass == double.class){
if(null == value){
return 0;
}
return value;
}else if(typeClass == long.class){
if(null == value){
return 0;
}
return value;
}else if(typeClass == String.class){
if(null == value){
return "";
}
return value;
}else if(typeClass == boolean.class){
if(null == value){
return true;
}
return value;
}else if(typeClass == BigDecimal.class){
if(null == value){
return new BigDecimal(0);
}
return new BigDecimal(value+"");
}else {
return typeClass.cast(value);
}
}
/**
* 处理字符串 如: abc_dex ---> abcDex
* @param str
* @return
*/
public static String removeLine(String str){
if(null != str && str.contains("_")){
int i = str.indexOf("_");
char ch = str.charAt(i+1);
char newCh = (ch+"").substring(0, 1).toUpperCase().toCharArray()[0];
String newStr = str.replace(str.charAt(i+1), newCh);
String newStr2 = newStr.replace("_", "");
return newStr2;
}
return str;
}
}

:转载文章请注明出处,谢谢~