Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit c429f65

Browse filesBrowse files
committed
add and test method to transfer a bean to another bean
1 parent 780c4b9 commit c429f65
Copy full SHA for c429f65

4 files changed

+131Lines changed: 131 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/com/zhazhapan/util/BeanUtils.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/zhazhapan/util/BeanUtils.java
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ public class BeanUtils {
2828

2929
private BeanUtils() {}
3030

31+
/**
32+
* 将一个Bean的数据装换到另外一个(需实现setter和getter)
33+
*
34+
* @param object 一个Bean
35+
* @param clazz 另外一个Bean
36+
* @param <T> 另外Bean类型
37+
*
38+
* @return {@link T}
39+
*
40+
* @throws IllegalAccessException 异常
41+
* @throws InstantiationException 异常
42+
* @throws NoSuchMethodException 异常
43+
* @throws InvocationTargetException 异常
44+
* @since 1.1.1
45+
*/
46+
public static <T> T bean2Another(Object object, Class<T> clazz) throws IllegalAccessException,
47+
InstantiationException, NoSuchMethodException, InvocationTargetException {
48+
T t = clazz.newInstance();
49+
Method[] methods = object.getClass().getMethods();
50+
Map<String, Method> clazzMethods = ReflectUtils.getMethodMap(clazz, "set");
51+
for (Method method : methods) {
52+
String name = method.getName();
53+
if (name.startsWith("get") && !"getClass".equals(name)) {
54+
String clazzMethod = "s" + name.substring(1);
55+
if (clazzMethods.containsKey(clazzMethod)) {
56+
clazzMethods.get(clazzMethod).invoke(t, method.invoke(object));
57+
}
58+
}
59+
}
60+
return t;
61+
62+
}
63+
3164
/**
3265
* 反序列化对象
3366
*
Collapse file

‎src/test/java/com/zhazhapan/util/BeanUtilsTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/com/zhazhapan/util/BeanUtilsTest.java
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
import com.zhazhapan.util.enums.FieldModifier;
77
import com.zhazhapan.util.enums.JsonMethod;
88
import com.zhazhapan.util.enums.JsonType;
9+
import com.zhazhapan.util.model.A;
10+
import com.zhazhapan.util.model.B;
911
import net.sf.cglib.proxy.Enhancer;
12+
import org.junit.Assert;
1013
import org.junit.Test;
1114

1215
import java.io.File;
1316
import java.io.IOException;
1417
import java.io.Serializable;
18+
import java.lang.reflect.InvocationTargetException;
1519
import java.util.Date;
1620

1721
/**
@@ -80,12 +84,30 @@ public void toJsonString2() {
8084
@Test
8185
public void toJsonStringByAnnotation() {
8286
}
87+
88+
@Test
89+
public void bean2Another() throws InvocationTargetException, NoSuchMethodException, InstantiationException,
90+
IllegalAccessException {
91+
B b = new B();
92+
b.setAge(102);
93+
b.setBio("test");
94+
A a = BeanUtils.bean2Another(b, A.class);
95+
Assert.assertNotNull(a);
96+
System.out.println(a);
97+
}
98+
99+
@Test
100+
public void toJsonObject() {
101+
}
83102
}
84103

85104
@ToJsonString(type = JsonType.PRETTY, modifier = FieldModifier.PRIVATE, method = JsonMethod.MANUAL)
86105
class User implements Serializable {
106+
87107
public int id;
108+
88109
private String name;
110+
89111
private Date birth;
90112

91113
public User() {}
Collapse file
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.zhazhapan.util.model;
2+
3+
/**
4+
* @author pantao
5+
* @since 2018/9/22
6+
*/
7+
public class A {
8+
9+
private String name;
10+
11+
private String gender;
12+
13+
private Integer age;
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
23+
public String getGender() {
24+
return gender;
25+
}
26+
27+
public void setGender(String gender) {
28+
this.gender = gender;
29+
}
30+
31+
public Integer getAge() {
32+
return age;
33+
}
34+
35+
public void setAge(Integer age) {
36+
this.age = age;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "A{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + '}';
42+
}
43+
}
Collapse file
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.zhazhapan.util.model;
2+
3+
/**
4+
* @author pantao
5+
* @since 2018/9/22
6+
*/
7+
public class B {
8+
9+
private Integer age;
10+
11+
private String bio;
12+
13+
@Override
14+
public String toString() {
15+
return "B{" + "age=" + age + ", bio='" + bio + '\'' + '}';
16+
}
17+
18+
public String getBio() {
19+
return bio;
20+
}
21+
22+
public void setBio(String bio) {
23+
this.bio = bio;
24+
}
25+
26+
public Integer getAge() {
27+
return age;
28+
}
29+
30+
public void setAge(Integer age) {
31+
this.age = age;
32+
}
33+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.