java反射执行pojo中的set/get方法

时间:2025-03-10 18:42:41


1、pojo类

package ;

public class Dept {
	private String ldapUserName ;
	private String userName ;
	private String phoneNum ;
	private String email ;
	private int deviceNum ;
	private int groupId ;
	private String groupName ;
	private String groupLevel ;
	private int parentGroupID ;
	
	public String getLdapUserName() {
		return ldapUserName;
	}
	public void setLdapUserName(String ldapUserName) {
		 = ldapUserName;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		 = userName;
	}
	public String getPhoneNum() {
		return phoneNum;
	}
	public void setPhoneNum(String phoneNum) {
		 = phoneNum;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		 = email;
	}
	public int getDeviceNum() {
		return deviceNum;
	}
	public void setDeviceNum(int deviceNum) {
		 = deviceNum;
	}
	public int getGroupId() {
		return groupId;
	}
	public void setGroupId(int groupId) {
		 = groupId;
	}
	public String getGroupName() {
		return groupName;
	}
	public void setGroupName(String groupName) {
		 = groupName;
	}
	public String getGroupLevel() {
		return groupLevel;
	}
	public void setGroupLevel(String groupLevel) {
		 = groupLevel;
	}
	public int getParentGroupID() {
		return parentGroupID;
	}
	public void setParentGroupID(int parentGroupID) {
		 = parentGroupID;
	}
}


2、通过反射执行Dept中的所有get方法

package ;

import ;
import ;
import ;

public class T {

	/**
	 * @param args
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws InstantiationException 
	 */
	public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class<?> clazz =  ;
		// 取得所有属性,不包括父类的属性
		Field[] fields = () ;
		for (int i = 0; i < ; i++) {
			// 迭代每一个属性
			String field = fields[i].getName() ;
			// 拼装出get方法名
			String methodName = "get"+((0)).toUpperCase()+(1) ;
			// 通过方法名取得方法对象
			Method m = (methodName) ;
			// (obj)传入的是该方法所在类的实例
			((()));
		}

	}

}



二、java中通过reflect,Introspector,PropertyDescriptor执行setter/getter方法比较


1、反射执行方法


package ;

import ;

/**
 * 通过反射获取一个方法并执行
 * 
 * 这样必须清楚的知道一个类中的方法,才好进行调用
 * 
 * 文件名 : 
 * @version : 1.0
 * @author : zhangh
 * @email : zhangh@	
 * @create date : 2012-9-21		
 */
public class ReflectInvoke {
	public static void main(String[] args) throws Exception {
		Class<?> clazz = ("") ;
		Method readName = ("getName") ;
		Method writeName = ("setName",) ;
		Object obj = () ;
		(obj, "zhangh") ;
		Object o = (obj) ;
		(o);
	}
}


2、Introspector执行方法

package ;

import ;
import ;
import ;
import ;

/**
 * 通过Introspector类获取bean类的所有缺省setter/getter方法
 * 
 * 这样做可以遍历所有的方法,与PropertyDescriptor类似,但没有那个方便                                                                                                                                                                                                                                                                                                                                                                                             
 * 
 * 
 * 文件名 : 
 * @version : 1.0
 * @author : zhangh
 * @email : zhangh@	
 * @create date : 2012-9-21		
 */
public class IntrospectorInvoke {

	public static void main(String[] args) throws Exception {
		Class<?> clazz = ("");
		BeanInfo beanInfo = (clazz, );
		PropertyDescriptor[] pds = ()  ;
		for (PropertyDescriptor propertyDescriptor : pds) {
			if("name".equals(())){
				Method readName = ();
				Method writeName = () ;
				Object obj = () ;
				(obj, "zhangh") ;
				Object o = (obj) ;
				(o);
			}
		}
	}
}


3、PropertyDescriptor执行方法

package ;

import ;
import ;

/**
 * 通过PropertyDescriptor类,通过传入一个属性值获取标准的setter/getter方法进行
 * 调用,简单方便
 * 
 * 文件名 : 
 * @version : 1.0
 * @author : zhangh
 * @email : zhangh@	
 * @create date : 2012-9-21		
 */
public class PropertyDescriptorInvoke {

	public static void main(String[] args) throws Exception {
		Class<?> clazz = ("") ;
		PropertyDescriptor pd = new PropertyDescriptor("name", clazz) ;
		Method readName = () ;
		Method writeName = () ;
		
		Object obj = () ;
		(obj, "zhangh") ;
		Object o = (obj) ;
		(o);
	}

}


package ;

public class Student {
	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		 = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		 = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		 = age;
	}

}



嗲用方法时类型的转换



package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .v2.;
import .v2.;

public final class ConnectionManager {
	private static ConnectionManager instance;

	public ComboPooledDataSource ds;
	private static String c3p0Properties = "";

	private ConnectionManager() throws Exception {
		Properties p = new Properties();
		(().getResourceAsStream(
				c3p0Properties));
		ds = new ComboPooledDataSource();
		setDataSourceValue(ds, p);

	}

	private Object getRealType(Class<?> field, String value) {
		Object obj = value;
		if (()) {
			obj = ();
		} else if (() || ()) {
			obj = (value);
		} else if (() || ()) {
			obj = (value);
		} else if (() || ()) {
			obj = (value);
		} else if (() || ()) {
			obj = (value);
		} else if (() || ()) {
			obj = (value);
		} else if (() || ()) {
			obj = (value);
		}

		return obj;

	}

	private void setDataSourceValue(ComboPooledDataSource ds, Properties p)
			throws SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, IntrospectionException {
		Iterator<Object> keys = ().iterator();
		BeanInfo beanInfo = Introspector
				.getBeanInfo();
		PropertyDescriptor[] pds = ();
		while (()) {
			String key = (String) ();
			for (int i = 0; i < ; i++) {
				String propertyName = pds[i].getName();
				if ((propertyName)) {
					String value = (key);
					Class<?> propertyType = pds[i].getPropertyType();
					Object val = getRealType(propertyType, value);
					Method method = pds[i].getWriteMethod();
					(() + "----" + key + "---"
							+ value);
					(ds, new Object[] { val });
				}
			}
		}
	}

	public static final ConnectionManager getInstance() {
		if (instance == null) {
			try {
				instance = new ConnectionManager();
			} catch (Exception e) {
				();
			}
		}
		return instance;
	}

	public synchronized final Connection getConnection() {
		try {
			return ();
		} catch (SQLException e) {
			();
		}
		return null;
	}

	protected void finalize() throws Throwable {
		(ds); // 关闭datasource
		();
	}

}