java反射 工具类

 

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**

 * @className:ReflectionUtil.java

 * @classDescription: 反射工具类

 * @author:qiuchen

 * @createTime:2012-7-4

 */

public class ReflectionUtil {

 

public static boolean isShow = true;

 

/**

* 获得一个对象的属性值

* @author:qiuchen

* @createTime:2012-7-4

* @param obj 目标对象

*/

@SuppressWarnings("unchecked")

public static String getFieldValue(Object obj){

if (isShow) return getFieldValue(obj, isShow);

return null;

}

 

/**

* 打印一个对象的属性值

* @author:qiuchen

* @createTime:2012-7-8

* @param obj

*/

public static void printFieldValue(Object obj){

if (isShow) System.out.println(getFieldValue(obj, isShow));

}

 

 

/**

* 获得一个对象的属性值

* @author:qiuchen

* @createTime:2012-7-4

* @param obj 目标对象

*/

@SuppressWarnings("unchecked")

private static String getFieldValue(Object obj,boolean isShow){

Class cls = obj.getClass();

        Method[] methods = cls.getDeclaredMethods();//获得类的方法集合

        StringBuffer buffer = new StringBuffer();

        try {

        for (Method method : methods) {

if(method!=null && method.getName().startsWith("get")){

Object value = method.invoke(obj,null);

value = method.getName().substring(3)+":"+value+","; 

buffer.append(value);

}

}

        } catch (Exception e) {

        e.printStackTrace();

        }

        return cls.toString()+"["+buffer.toString()+"]"; 

}

 

/**

* Servlet从页面表单域中取值,封装到一个JavaBean中

* *****页面表单域部分所有实体属性必须小写

 * *****目前仅仅支持基本数据类型的赋值

* @author:qiuchen

* @createTime:2012-7-8

* @param <T>泛型,可以代替任何java对象

* @param request请求对象

* @param beanClass表单JavaBean类对象

* @return 表单JavaBean实例

*/

@SuppressWarnings("unchecked")

public static <T> T buildBean(HttpServletRequest request,Class<T> beanClass){

 

//beanClass all setMethod

List<Method> setMethods = new ArrayList<Method>();

 

//beanClass all setMethod'name

List<String> setMethodNames = new ArrayList<String>();

 

//beanClass all property'name

List<String> propertyNames = new ArrayList<String>();

 

//page form values

List<String> formValues = new ArrayList<String>();

 

//beanClass all Method

Method[] methods = beanClass.getDeclaredMethods();

 

//catch all setMethod

for(Method m : methods){

if(m!=null && m.getName().startsWith("set")) setMethods.add(m);

}

 

//实例化Bean

Object beanObj = null;

try {

beanObj = beanClass.newInstance();

//通过方法名封装方法名和属性名

for(Method m : setMethods){

//method name

String mn = m.getName();

//property name

String propertyName = mn.replace("set","").toLowerCase();

 

setMethodNames.add(mn);

propertyNames.add(propertyName);

}

//通过request.getParameter(属性名)获得表单数据

for(String property : propertyNames){

formValues.add(request.getParameter(property));

}

 

//把表单对象属性值转为正确的类型(set方法参数类型)后实例化对象

int index = 0;

for(Method m : setMethods){

String type = m.getParameterTypes()[0].toString();

if(type.indexOf(".")>-1){

type = type.substring(type.lastIndexOf(".")+1);

}

String value = formValues.get(index);

if(value == null || "".equals(value)){

index ++;

continue;

}

//List,自定义类目前不支持赋值

if("String".endsWith(type)){

m.invoke(beanObj,value);

}else if("int".endsWith(type) || "Integer".endsWith(type)){

m.invoke(beanObj, Integer.parseInt(value));

}else if("double".endsWith(type) || "Double".endsWith(type)){

m.invoke(beanObj, Double.parseDouble(value));

}else if("float".endsWith(type) || "Float".endsWith(type)){

m.invoke(beanObj, Float.parseFloat(value));

}

index ++;

}

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

return (T)beanObj;

}

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值