读取属性及其特点
public static void main(String [] arg)
{
Class clazz = TestAnnotation.class;
//testClass.get
Map<String, FieldDescriptor> map = fieldDescriptorMap.get(clazz.getName()) ;
if (map != null) {
System.out.println("不是 null");
}
map = new HashMap<String, FieldDescriptor>();
Class<?> c = clazz;
Field f = null;
FieldDescriptor fd = null;
String propertyName = null;
System.out.println("ObjectClass = "+Object.class.getName());
while (c != null && !c.getName().equals(Object.class.getName())) {
/***
* getPropertyDescriptors(Class c):
* Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.
* Does not work with DynaBeans.
*/
PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(c);
for (PropertyDescriptor p : properties) {
try {
//获得此特性的编程名称
propertyName = p.getName();
System.out.println("属性名为:"+propertyName);
/**
* getDeclaredField(propertyName)
* 返回一个 Field 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明字段。
* propertyName 参数是一个 String,它指定所需字段的简称。注意,此方法不反映数组类的 length 字段。
*/
f = c.getDeclaredField(propertyName);
if (f != null) {
fd = new FieldDescriptor();
fd.setField(f);
/**
* p.getReadMethod()
* 获得应该用于读取属性值的方法
*/
fd.setReadable(p.getReadMethod() != null);
/**
* p.getWriteMethod()
* 获得应该用于写入属性值的方法
*/
fd.setWriteable(p.getWriteMethod() != null);
}
if (!map.containsKey(propertyName)) {
map.put(propertyName, fd);
}
} catch (Exception e) {
}
}
c = c.getSuperclass();
}
System.out.println("end");
}
读取属性及方法应用3
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.PropertyUtils;
import com.company.plat.persistence.util.BaseUtils;
public class BeanMapTest {
public static void main(String[] args) {
TestAnnotation bean = null;
BeanMap beanMap = null;
Class<?>[] methodParaTypes = null;
Method method = null;
try {
bean = TestAnnotation.class.newInstance();
bean.setAge(11) ;
bean.setBirthday(new Date());
bean.setId(1);
bean.setName("测试");
bean.setWifeName("秘密");
beanMap = new BeanMap(bean);
// PropertyUtils.setProperty(record, propertyName, value);
PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(TestAnnotation.class);
String propertyName;
for(PropertyDescriptor propertie : properties )
{
//获得此特性的编程名称
propertyName = propertie.getName();
System.out.println("属性名:"+propertyName);
method = null;
if (propertyName != null) {
//获取set方法
method = beanMap.getWriteMethod(propertyName);
}
if (propertyName == null || method == null) {
continue;
}
methodParaTypes = method.getParameterTypes();
if (methodParaTypes == null || methodParaTypes.length != 1) {
continue;
}
//判断是否是基本数据类型
if (BaseUtils.isBaseType(methodParaTypes[0]))
{
System.out.println(methodParaTypes[0].getName());
}
//设置值
//PropertyUtils.setProperty(bean, propertyName, null);
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static <T> Object getValueData(ResultSet rs, String column,
Class<T> propertyClass) throws SQLException{
String className = propertyClass.getName();
Object value = null;
if (!BaseUtils.isBaseType(propertyClass))
throw new SQLException("Cannot Recognise the data Type, column="
+ column + ", type=" + className);
if ("java.lang.String".equals(className)) {
value = rs.getString(column);
} else if ("int".equals(className)
|| "java.lang.Integer".equals(className)) {
value = new Integer(rs.getInt(column));
} else if ("float".equals(className)
|| "java.lang.Float".equals(className)) {
value = new Float(rs.getFloat(column));
} else if ("double".equals(className)
|| "java.lang.Double".equals(className)) {
value = new Double(rs.getDouble(column));
} else if ("long".equals(className)
|| "java.lang.Long".equals(className)) {
value = new Long(rs.getLong(column));
} else if ("short".equals(className)
|| "java.lang.Short".equals(className)) {
value = new Short(rs.getShort(column));
} else if ("byte".equals(className)
|| "java.lang.Byte".equals(className)) {
value = new Byte(rs.getByte(column));
} else if ("boolean".equals(className)
|| "java.lang.Boolean".equals(className)) {
value = new Boolean(rs.getBoolean(column));
} else if ("java.util.Date".equals(className)) {
Timestamp t = rs.getTimestamp(column);
if (t == null) {
value = null;
} else {
value = new java.util.Date(t.getTime());
}
} else if ("java.sql.Timestamp".equals(className)) {
value = rs.getTimestamp(column);
} else if ("java.sql.Date".equals(className)) {
Timestamp t = rs.getTimestamp(column);
if (t == null) {
value = null;
} else {
value = new java.sql.Date(t.getTime());
}
} else if ("java.lang.BigDecimal".equals(className)) {
value = rs.getBigDecimal(column);
} else {
throw new SQLException("Cannot Recognise the data Type, column="
+ column + ", type=" + className);
}
return value;
}
}