欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java bean 工具类

程序员文章站 2022-06-15 09:09:00
...
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);
}
}
相关标签: javabean