ObjInvoke.java
1.63 KB
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
package cn.com.glsx.stoassistant.utils;
import cn.com.glsx.stoassistant.response.activity.ActivityStaticsResponse;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @author zhougeng
* @version V1.0
* @Title: ObjInvoke
* @Description:
* @Company: Didihu.com.cn
* @Copyright Copyright (c) 2022
**/
public class ObjInvoke {
public static ActivityStaticsResponse getActivityStaticsResponseDemo(ActivityStaticsResponse obj) {
// 得到类对象
Class objCla = obj.getClass();
Field[] fs = objCla.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
// 设置些属性是可以访问的
boolean isStatic = Modifier.isStatic(f.getModifiers());
if(isStatic){
continue;
}
// 设置些属性是可以访问的
f.setAccessible(true);
try {
// 得到此属性的值
Object val = f.get(obj);
// 得到此属性的类型
String type = f.getType().toString();
String name = f.getName();
if (!name.equals("id")&&name.contains("Rate") && val == null) {
f.set(obj, (int)(Math.random()*100));
}
else if(!name.equals("id")&&(type.endsWith("int") || type.endsWith("Integer") || type.endsWith("double")) && val == null)
{
f.set(obj,(int)(Math.random()*1000) );
}
} catch (Exception e) {
e.printStackTrace();
}
}
return obj;
}
}