Skip to content

Commit 3137cfa

Browse files
提交利用反射设置简单类单级属性
1 parent a8129a4 commit 3137cfa

10 files changed

+218
-7
lines changed

src/com/java/demo15/TestDemo.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/com/java/demo15/TestDeptDemo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.java.demo15;
2+
3+
import com.java.demo15.action.DeptAction;
4+
5+
public class TestDeptDemo {
6+
public static void main(String[] args) {
7+
String info = "dept.dname:事业部|dept.loc:重庆";
8+
DeptAction deptAction = new DeptAction();
9+
deptAction.index(info);
10+
}
11+
}

src/com/java/demo15/TestEmpDemo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.java.demo15;
2+
3+
import com.java.demo15.action.EmpAction;
4+
5+
public class TestEmpDemo {
6+
public static void main(String[] args) {
7+
String info = "emp.ename:Jaywu|emp.job:Java";
8+
EmpAction empAction = new EmpAction();
9+
empAction.index(info);
10+
}
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.java.demo15.action;
2+
3+
import com.java.demo15.utils.BeanOperation;
4+
import com.java.demo15.vo.Dept;
5+
6+
public class DeptAction {
7+
8+
Dept dept = new Dept();
9+
10+
public void index(String info) {
11+
BeanOperation.setFeildValue(this, info);
12+
System.out.println(this.dept);
13+
}
14+
15+
public Dept getDept() {
16+
return dept;
17+
}
18+
19+
public void setDept(Dept dept) {
20+
this.dept = dept;
21+
}
22+
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.java.demo15.action;
2+
3+
import com.java.demo15.utils.BeanOperation;
4+
import com.java.demo15.vo.Emp;
5+
6+
/**
7+
* 模拟Action
8+
*
9+
* @author jack w
10+
*
11+
*/
12+
public class EmpAction {
13+
14+
private Emp emp = new Emp();
15+
16+
public void index(String info) {
17+
BeanOperation.setFeildValue(this, info);
18+
System.out.println(this.emp);
19+
}
20+
21+
public Emp getEmp() {
22+
return emp;
23+
}
24+
25+
public void setEmp(Emp emp) {
26+
this.emp = emp;
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.java.demo15.utils;
2+
3+
public class BeanOperation {
4+
5+
private BeanOperation(){}
6+
7+
/**
8+
* 根据字符串设置对象的属性值
9+
* @param object
10+
* @param value
11+
*/
12+
public static void setFeildValue(Object object,String info) {
13+
String [] fieldStrArr = info.split("\\|");
14+
for(int i = 0 ; i < fieldStrArr.length ; i++){
15+
String [] fieldArr = fieldStrArr[i].split(":");
16+
//两个属性名字符串
17+
String fieldName = fieldArr[0];
18+
//设置属性值
19+
String fieldValue = fieldArr[1];
20+
String[] fieldTemp = fieldName.split("\\.");
21+
try {
22+
Object object2 = ObjectUtil.getValue(object, fieldTemp[0]);
23+
ObjectUtil.setValue(object2, fieldTemp[1], fieldValue);
24+
} catch (Exception e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.java.demo15.utils;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
*
8+
* 利用反射进行属性值的取得和设置
9+
* @author jack w
10+
*
11+
*/
12+
public class ObjectUtil {
13+
private ObjectUtil(){}
14+
15+
/**
16+
* 为某个对象根据属性设置值
17+
* @param object
18+
* @param fieldName
19+
* @param value
20+
* @throws Exception
21+
*/
22+
public static void setValue(Object object,String fieldName,String value) throws Exception {
23+
String methodName = "set"+StringUtil.initCat(fieldName);
24+
Field field = object.getClass().getDeclaredField(fieldName);
25+
if (null == field) {
26+
field = object.getClass().getField(fieldName);
27+
}
28+
Method method = object.getClass().getMethod(methodName, field.getType());
29+
method.invoke(object, value);
30+
}
31+
32+
/**
33+
* 取得某个对象的属性对象
34+
* @param object
35+
* @param field
36+
* @return
37+
* @throws Exception
38+
*/
39+
public static Object getValue(Object object,String fieldName) throws Exception {
40+
String methodName = "get"+StringUtil.initCat(fieldName);
41+
Method method = object.getClass().getMethod(methodName);
42+
return method.invoke(object);
43+
}
44+
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.java.demo15.utils;
2+
3+
/**
4+
* 根据属性名获取setter/getter方法名
5+
*
6+
* @author jack w
7+
*
8+
*/
9+
public class StringUtil {
10+
11+
private StringUtil(){
12+
13+
}
14+
15+
public static String initCat(String fieldName) {
16+
return fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
17+
}
18+
}

src/com/java/demo15/vo/Dept.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.java.demo15.vo;
2+
3+
public class Dept {
4+
5+
private String dname;
6+
private String loc;
7+
8+
public String getDname() {
9+
return dname;
10+
}
11+
12+
public void setDname(String dname) {
13+
this.dname = dname;
14+
}
15+
16+
public String getLoc() {
17+
return loc;
18+
}
19+
20+
public void setLoc(String loc) {
21+
this.loc = loc;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Dept [dname=" + dname + ", loc=" + loc + "]";
27+
}
28+
29+
}

src/com/java/demo15/vo/Emp.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
import java.io.Serializable;
44

5-
65
@SuppressWarnings("serial")
7-
public class Emp implements Serializable{
6+
public class Emp implements Serializable {
87
private String ename;
98
private String job;
9+
10+
public String getEname() {
11+
return ename;
12+
}
13+
14+
public void setEname(String ename) {
15+
this.ename = ename;
16+
}
17+
18+
public String getJob() {
19+
return job;
20+
}
21+
22+
public void setJob(String job) {
23+
this.job = job;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "Emp [ename=" + ename + ", job=" + job + "]";
29+
}
30+
1031
}

0 commit comments

Comments
 (0)