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);
}
}
推荐阅读
-
PHP Request工具类
-
实用技巧:PHP中调用Java类的两种方法
-
C#编写了一个基于Lucene.Net的搜索引擎查询通用工具类:SearchEngineUtil
-
有没有必要把pdo对数据库的操作封装成工具类
-
php工具类之【视频变换类】
-
Java面向对象(1)面向对象的思想概述以及类的介绍,封装和构造方法
-
java基本类型的包装类
-
php实现网页缓存的工具类分享
-
java-类库-Apache Commons补充
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符