java bean 工具类

package org.sharpcode.util;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.ParameterDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.sharpcode.po.Account;

/**
* java bean 工具类
*
* @author kingschan
*
*/
public class JavaBeanUtil {
/**
* 得到一个bean的方法列表
*
* @param clazz
* @throws IntrospectionException
*/
public static void getBeansMethods(Class<?> clazz)
throws IntrospectionException {
// 方法列表
MethodDescriptor[] methods;
// 参数列表
ParameterDescriptor[] parameters;
methods = Introspector.getBeanInfo(clazz).getMethodDescriptors();
System.out.println(String.format("%s:共有%s个方法", clazz.getName(),
methods.length));
for (MethodDescriptor m : methods) {
System.out.println(m.getName());
parameters = m.getParameterDescriptors();
if (null != parameters) {
for (ParameterDescriptor p : parameters) {
System.out.println("parametes:" + p.getName());
}
}

}
}

/**
* 得到bean的属性名字
*
* @param clazz
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void getProperty(Object obj) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
// 属性列表
PropertyDescriptor[] propertys;
Method method;
propertys = Introspector.getBeanInfo(obj.getClass())
.getPropertyDescriptors();
for (PropertyDescriptor p : propertys) {
System.out.println(p.getName());
method = p.getReadMethod();
// Set<String> set = new HashSet<String>();
// set.add("accountUsername");
// getPropertyValue(p,obj,set);
getPropertyValue(p, obj);

}
}

/**
* 得到bean 属性的值
*
* @param p
* @param obj
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void getPropertyValue(PropertyDescriptor p, Object obj)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
System.out.println(String.format("key:%s value:%s", p.getName(), p
.getReadMethod().invoke(obj)));
}

/**
* 只打出指定属性名
*
* @param p
* @param obj
* @param pname
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void getPropertyValue(PropertyDescriptor p, Object obj,
Set<String> pname) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
if (pname.contains(p.getName())) {
System.out.println(String.format("key:%s value:%s", p.getName(), p
.getReadMethod().invoke(obj)));
}

}

/**
* 构建指定的对象 并初始化
*
* @param obj
* 对象类型
* @param map
* key 属性名 value 要初始化的值
* @return
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws ClassNotFoundException
* @throws InstantiationException
*/
public static Object buildObject(Object obj, Map<String, Object> map)
throws IntrospectionException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
ClassNotFoundException, InstantiationException {
//实例化一个对象
Object objclass = ((Class<?>) obj).newInstance();
// 属性列表
PropertyDescriptor[] propertys;
Method method;
Class<?> ptype;// 参数列类
propertys = Introspector.getBeanInfo(objclass.getClass())
.getPropertyDescriptors();
for (PropertyDescriptor p : propertys) {
//对属性赋值
if (map.keySet().contains(p.getName())) {
method = p.getWriteMethod();
ptype = method.getParameterTypes()[0];
method.invoke(objclass, map.get(p.getName()));
}

}
return objclass;
}

public static void main(String[] args) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException, ClassNotFoundException,
InstantiationException {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("accountUsername", "admin");
map.put("accountId", 100);
map.put("accountEnable", true);
Account a = (Account) JavaBeanUtil.buildObject(Account.class, map);
JavaBeanUtil.getProperty(a);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值