反射bean操作

package org.kjcx.entity;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;

/**
 * 
 * @author LiuQing
 * @param <T>
 * @since
 * @version 1.0
 * @see 2011-10-3
 */
public class TestMangs<T> {
	
	private T disObj;

	/**
	 * @param args
	 * @throws Exception 
	 * @throws SecurityException 
	 */
	public static void main(String[] args) throws SecurityException, Exception {
		
		StudentInfo stu = new StudentInfo();
		Vector<CoreInfo> list = new Vector<CoreInfo>();
		
		CoreInfo coreInfo1 = new CoreInfo();
		coreInfo1.setId(1);
		coreInfo1.setComu(23.89d);
		coreInfo1.setCoreStr("HelloWorld");
		CoreInfo coreInfo2 = new CoreInfo();
		coreInfo2.setId(2);
		coreInfo2.setComu(34.90d);
		coreInfo2.setCoreStr("xxx.....");
		CoreInfo coreInfo3 = new CoreInfo();
		coreInfo3.setId(4);
		coreInfo3.setComu(24.90d);
		coreInfo3.setCoreStr("xxx..---...");
		list.add(coreInfo1);
		list.add(coreInfo2);
		list.add(coreInfo3);
		
		TestMangs<StudentInfo> sf = new TestMangs<StudentInfo>();
		sf.setDisObj(stu);
		sf.setMethodValue("id", 1000035);
		sf.setMethodValue("name", "LiuQing");
		sf.setMethodValue("msbsss", list.toArray());
		list.add(new CoreInfo());
		sf.setMethodValue("coreInfoes", list);

	}
	
	/**
	 * 
	 * @param fieldName
	 * @param isBoolean
	 * @return Class<?>
	 * @throws NoSuchMethodException
	 */
	public Class<?> getMethodReturnType(String fieldName,boolean isBoolean) throws NoSuchMethodException {
		
		return this.disObj.getClass()
		.getMethod(isBoolean? "is":"get" + this.firstToUpper(fieldName), null)
		.getReturnType();
	}
	
	/**
	 * 
	 * @param fieldName
	 * @param value
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public void setMethodBooleanValue(String fieldName,Object value) throws NoSuchMethodException, 
	    IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		
		Class<?> returnType = this.getMethodReturnType(fieldName,true);
		Method setMethod = this.disObj.getClass()
		.getMethod("set" + this.firstToUpper(fieldName),returnType);
		
		if (!returnType.isArray()) {
			setMethod.invoke(disObj, value);
		}
		else {
			Class<?> szElementType = returnType.getComponentType();
			Object arrayValue[] = (Object[]) value;
			Object obj = Array.newInstance(szElementType, arrayValue.length);
			System.arraycopy(value, 0, obj,0,arrayValue.length);
			setMethod.invoke(this.disObj, obj);
		}
	}
	
	/**
	 * 
	 * @param fieldName
	 * @param value
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public void setMethodValue(String fieldName,Object value) throws NoSuchMethodException, 
	    IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		
		Class<?> returnType = this.getMethodReturnType(fieldName,false);
		Method setMethod = this.disObj.getClass()
		.getMethod("set" + this.firstToUpper(fieldName),returnType);
		
		if (!returnType.isArray()) {
			setMethod.invoke(disObj, value);
		}
		else {
			Class<?> szElementType = returnType.getComponentType();
			Object arrayValue[] = (Object[]) value;
			Object obj = Array.newInstance(szElementType, arrayValue.length);
			System.arraycopy(value, 0, obj,0,arrayValue.length);
			setMethod.invoke(this.disObj, obj);
		}
	}
	
	/**
	 * 
	 * @param fieldName
	 * @return String
	 */
	public String firstToUpper(String fieldName) {
		
		if (fieldName == null) {
			throw new NullPointerException();
		}
		
		char[] ch = fieldName.toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char)(ch[0] - 32);
		}
		return new String(ch);
	}
	
	/**
	 * 
	 * @param fieldName
	 * @return String
	 */
	public String firstToLower(String fieldName) {
		
		if (fieldName == null) {
			throw new NullPointerException();
		}
		
		char[] ch = fieldName.toCharArray();
		if (ch[0] >= 'A' && ch[0] <= 'Z') {
			ch[0] = (char)(ch[0] + 32);
		}
		return new String(ch);
	}

	public T getDisObj() {
		return disObj;
	}

	public void setDisObj(T disObj) {
		this.disObj = disObj;
	}
	
	

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值