这样的类型的?public class Test2 { public static void main(String[] args) throws Exception { A a = new A(); Field[] fa = a.getClass().getFields(); Field[] fc = Class.forName("c").getFields();
for (Field f : fa) { for (Field ff : fc) { if (ff.getName().equals(f.getName())) { ff.setAccessible(true); ff.set(a.c, f.get(a)); } } } } } class A{ C c; } class C{
}
这个你看看行不行public class Client {
public static List<Bean> list = new ArrayList<Bean>(); static{ Bean b1 = new Bean("id", "id"); Bean b2 = new Bean("name", "name"); Bean b3 = new Bean("newAge", "age"); Bean b4 = new Bean("email", "newEmail"); list.add(b1); list.add(b2); list.add(b3); list.add(b4); }
public static void main(String[] args) throws Exception{ A a = new A(); a.setId(1l); a.setName("json"); a.setNewAge(11); a.setEmail("[email protected]"); B b = new B(); cloneObj(list, a, b); System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail()); }
public static List<Bean> list = new ArrayList<Bean>(); static{ Bean b1 = new Bean("id", "id"); Bean b2 = new Bean("name", "name"); Bean b3 = new Bean("newAge", "age"); Bean b4 = new Bean("email", "newEmail"); Bean b5 = new Bean("c", "d"); list.add(b1); list.add(b2); list.add(b3); list.add(b4); list.add(b5); }
public static void main(String[] args) throws Exception{ A a = new A(); a.setId(1l); a.setName("json"); a.setNewAge(11); a.setEmail("[email protected]"); C c = new C(); c.setId(1000l); a.setC(c); B b = new B(); cloneObj(list, a, b); System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail() + "[c:" + b.getD().getId() + "]"); }
C---D 中的属性和字段都是对应的
public static void main(String[] args) throws Exception {
A a = new A();
Field[] fa = a.getClass().getFields(); Field[] fc = Class.forName("c").getFields();
for (Field f : fa) {
for (Field ff : fc) {
if (ff.getName().equals(f.getName())) {
ff.setAccessible(true);
ff.set(a.c, f.get(a));
}
}
}
}
}
class A{
C c;
}
class C{
}
public static List<Bean> list = new ArrayList<Bean>(); static{
Bean b1 = new Bean("id", "id");
Bean b2 = new Bean("name", "name");
Bean b3 = new Bean("newAge", "age");
Bean b4 = new Bean("email", "newEmail");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
}
public static void main(String[] args) throws Exception{
A a = new A();
a.setId(1l);
a.setName("json");
a.setNewAge(11);
a.setEmail("[email protected]");
B b = new B();
cloneObj(list, a, b);
System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail());
}
private static void cloneObj(List<Bean> params, Object a, Object b){
Class<?> classA = a.getClass();
Class<?> classB = b.getClass();
for(Bean bean : params){
String srcFieldName = bean.getSrcField();
String distFieldName = bean.getDistField();
try {
Field srcField = classA.getDeclaredField(srcFieldName);
Field distField = classB.getDeclaredField(distFieldName);
Class<?> srcType = srcField.getType();
Class<?> distType = distField.getType();
if(srcType == distType){
String srcGetMethodName = "get" + srcFieldName.substring(0, 1).toUpperCase() + srcFieldName.substring(1);
String distSetMethodName = "set" + distFieldName.substring(0, 1).toUpperCase() + distFieldName.substring(1);
Method getSrcMethod = classA.getMethod(srcGetMethodName, new Class[]{});
Method setDistMethod = classB.getMethod(distSetMethodName, new Class[]{distType});
setDistMethod.invoke(b, getSrcMethod.invoke(a, new Object[]{}));
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}
class A{
private Long id;
private String name;
private Integer newAge;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNewAge() {
return newAge;
}
public void setNewAge(Integer newAge) {
this.newAge = newAge;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class B{
private Long id;
private String name;
private Integer age;
private String newEmail;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getNewEmail() {
return newEmail;
}
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
}
}
class Bean{
private String srcField;
private String distField;
public Bean(String srcField, String distField){
this.srcField = srcField;
this.distField = distField;
}
public String getSrcField() {
return srcField;
}
public void setSrcField(String srcField) {
this.srcField = srcField;
}
public String getDistField() {
return distField;
}
public void setDistField(String distField) {
this.distField = distField;
}
}
Class<?> distType = distField.getType();
if(srcType == distType){
String srcGetMethodName = "get" + srcFieldName.substring(0, 1).toUpperCase() + srcFieldName.substring(1);
String distSetMethodName = "set" + distFieldName.substring(0, 1).toUpperCase() + distFieldName.substring(1);
Method getSrcMethod = classA.getMethod(srcGetMethodName, new Class[]{});
Method setDistMethod = classB.getMethod(distSetMethodName, new Class[]{distType});
setDistMethod.invoke(b, getSrcMethod.invoke(a, new Object[]{}));
}
这个只能复制普通的属性,A类中的对象属性无法复制
A
C
B
C
这种情况
public class Client {
public static List<Bean> list = new ArrayList<Bean>(); static{
Bean b1 = new Bean("id", "id");
Bean b2 = new Bean("name", "name");
Bean b3 = new Bean("newAge", "age");
Bean b4 = new Bean("email", "newEmail");
Bean b5 = new Bean("c", "d");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
}
public static void main(String[] args) throws Exception{
A a = new A();
a.setId(1l);
a.setName("json");
a.setNewAge(11);
a.setEmail("[email protected]");
C c = new C();
c.setId(1000l);
a.setC(c);
B b = new B();
cloneObj(list, a, b);
System.out.println(b.getId() + "--" + b.getName() + "--" + b.getAge() + "--" + b.getNewEmail() + "[c:" + b.getD().getId() + "]");
}
private static void cloneObj(List<Bean> params, Object a, Object b){
Class<?> classA = a.getClass();
Class<?> classB = b.getClass();
for(Bean bean : params){
String srcFieldName = bean.getSrcField();
String distFieldName = bean.getDistField();
try {
Field srcField = classA.getDeclaredField(srcFieldName);
Field distField = classB.getDeclaredField(distFieldName);
Class<?> srcType = srcField.getType();
Class<?> distType = distField.getType();
// 都不是基本类型,如果都不是java包下的
String srcGetMethodName = "get" + srcFieldName.substring(0, 1).toUpperCase() + srcFieldName.substring(1);
String distSetMethodName = "set" + distFieldName.substring(0, 1).toUpperCase() + distFieldName.substring(1);
Method setDistMethod = classB.getMethod(distSetMethodName, new Class[]{distType});
Method getSrcMethod = classA.getMethod(srcGetMethodName, new Class[]{});
Object srcObj = getSrcMethod.invoke(a, new Object[]{});
if(!srcType.isPrimitive() && !distType.isPrimitive() && !srcType.getName().startsWith("java.lang") && !distType.getName().startsWith("java.")){
Object distObj = distType.newInstance();
cloneObj(params, srcObj, distObj);
setDistMethod.invoke(b, distObj);
}else{
setDistMethod.invoke(b, srcObj);
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}
class C{
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class D{
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
class A{
private Long id;
private String name;
private Integer newAge;
private String email;
private C c;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNewAge() {
return newAge;
}
public void setNewAge(Integer newAge) {
this.newAge = newAge;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
class B{
private Long id;
private String name;
private Integer age;
private String newEmail;
private D d;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getNewEmail() {
return newEmail;
}
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
}
public D getD() {
return d;
}
public void setD(D d) {
this.d = d;
}
}
class Bean{
private String srcField;
private String distField;
public Bean(String srcField, String distField){
this.srcField = srcField;
this.distField = distField;
}
public String getSrcField() {
return srcField;
}
public void setSrcField(String srcField) {
this.srcField = srcField;
}
public String getDistField() {
return distField;
}
public void setDistField(String distField) {
this.distField = distField;
}
}
get、set方法都有
需求就是 【将对象A的id、name复制给对象C的id、name
将对象A中的对象C的id、name 复制给对象A中的对象D的id、name】
A、B、C、D都是泛指 即代表所有自定义对象
跪求指教,能不能写个dome来看看 若是好用我还可以追加积分!!!
BeanUtils.copyProperties(A.d,A.c);A中的c赋值给A中的d
BeanUtils.copyProperties(C,A);A对象赋值给C对象
get、set方法都有
需求就是 【将对象A的id、name复制给对象C的id、name
将对象A中的对象C的id、name 复制给对象A中的对象D的id、name】
A、B、C、D都是泛指 即代表所有自定义对象
你的A中没有D的引用啊?B中才有D啊?