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;
}
}