反射例子希望对大家有帮助
//有数据的bean
package utilbeans;
public class UtilsBeansOne {
public UtilsBeansOne() {
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
private String userName = "张三";
private String password = "123456";
}
//////////////////////////////////////////
没有数据的bean
package utilbeans;
public class UtilsBeansTwo {
public UtilsBeansTwo() {
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println(userName);
this.userName = userName;
}
public void setPassword(String password) {
System.out.println(password);
this.password = password;
}
private String userName = null;
private String password = null;
}
///////////////////////////////////////////
具体的填充方法
package utilbeans;
import java.lang.reflect.Method;
import java.lang.reflect.*;
public class UtilsBeansTest {
public UtilsBeansTest() {
}
/**
* 此功能为将第一个对象的内容填充到第二个对象中
* @param oneObject String 内容已经存在的对象
* @param twoObject String 要填充的对象
*/
public static Object utilBean(String oneObject, String twoObject) {
Class utilOneClass = null;
Class utilTwoClass = null;
try {
//将字符串转化为Class对象
utilOneClass = Class.forName(oneObject);
utilTwoClass = Class.forName(twoObject);
} catch (ClassNotFoundException ex) {
System.out.println(ex.toString());
return null;
}
Object objUtilBeanOne = null;
Object objUtileBeanTwo = null;
try {
//创建对象实例
objUtilBeanOne = utilOneClass.newInstance();
objUtileBeanTwo = utilTwoClass.newInstance();
} catch (IllegalAccessException ex1) {
System.out.println(ex1.toString());
return null;
} catch (InstantiationException ex1) {
System.out.println(ex1.toString());
return null;
}
//获得对象的所有方法;
Method[] oneMethod = utilOneClass.getDeclaredMethods();
Method[] twoMethod = utilTwoClass.getDeclaredMethods();
Method oneBeanMethod = null;
Method twoBeanMethod = null;
//循环所有方法
for (int oneIndex = 0; oneIndex < oneMethod.length; oneIndex++) {
oneBeanMethod = oneMethod[oneIndex];
String oneMethodName = oneBeanMethod.getName();
//判断是否为get方法
if (oneMethodName.indexOf("get") > -1) {
for (int twoIndex = 0; twoIndex < twoMethod.length; twoIndex++) {
twoBeanMethod = twoMethod[twoIndex];
//获得方法名
String towMethodName = twoBeanMethod.getName();
String oneName = oneMethodName.substring(oneMethodName.
indexOf("get") + 3);
String twoName = towMethodName.substring(towMethodName.
indexOf("set") + 3);
//判断去掉get/set后的方法名是否相同
if (oneName.equals(twoName)) {
try {
//获得第一个bean的值
String oneAttrValuedd = oneBeanMethod.invoke(
objUtilBeanOne, null).toString();
//将从第一个bean中取出的值,填充到第二个bean中
Object[] obj = {oneAttrValuedd};
//将obj内容填充到objUtileBeanTwo对象的twoBeanMethod方法中
twoBeanMethod.invoke(
objUtileBeanTwo, obj);
} catch (InvocationTargetException ex2) {
System.out.println(ex2.toString());
return null;
} catch (IllegalArgumentException ex2) {
ex2.printStackTrace();
return null;
} catch (IllegalAccessException ex2) {
System.out.println(ex2.toString());
return null;
}
}
}
}
}
return objUtileBeanTwo;
}
public static void main(String[] args) {
//bean填充前
UtilsBeansTwo utitlBean = new UtilsBeansTwo();
System.out.println("username : " + utitlBean.getUserName());
System.out.println("password : " + utitlBean.getPassword());
UtilsBeansTwo utitlBeanAfter = (UtilsBeansTwo)utilBean("utilbeans.UtilsBeansOne", "utilbeans.UtilsBeansTwo");
System.out.println("---------------------------------------------");
//bean填充后
System.out.println("username : " + utitlBeanAfter.getUserName());
System.out.println("password : " + utitlBeanAfter.getPassword());
}
}