package Demo;
/**
* 在类UserInfo中有属性userName,我们可以通过getUserName和setUserName得到其值和设置新值
* 通过getUserName/setUserName来访问userName属性,java JDK提供了一套api用来访问某个属性的getter/setter方法,这就是内省
JDK内省类库:
PropertyDescriptor类:
PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
1. getPropertyType(),获得属性的Class对象;
2. getReadMethod(),获得用于读取属性值的方法;getWriteMethod(),获得用于写入属性值的方法;
3. hashCode(),获取对象的哈希值;
4. setReadMethod(Method readMethod),设置用于读取属性值的方法;
5. setWriteMethod(Method writeMethod),设置用于写入属性值的方法。
Introspector 将javaBean中的属性封装起来进行操作。在程序中把一个类当做javaBean来看,就是调用Introspector的getBeanInfo(),得到BeanInfo对象封装了把这个类当做javaBean
k看到的结果信息,既属性的信息
getPropertyDescriptors()获得属性的描述,可以通过遍历beanInfo 方法来查找设置类的属性
*/
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanUtil {
public static void setProperty(UserInfo user)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PropertyDescriptor propDesc = new PropertyDescriptor("userName",
user.getClass());
Method writeMethod = propDesc.getWriteMethod();
writeMethod.invoke(user, "min");
System.out.println(user.getUserName());
}
public static void getProperty(UserInfo user) throws Exception {
PropertyDescriptor proDesc = new PropertyDescriptor("userName",
user.getClass());
Method readMethod = proDesc.getReadMethod();
Object userInfo = readMethod.invoke(user);
System.out.println(userInfo.toString());
}
public static void setPropertyByIntrospector(UserInfo user)
throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(user.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
for (PropertyDescriptor prodesc : propertyDescriptors) {
if (prodesc.getName().equals("userName")) {
Method writeMethod = prodesc.getWriteMethod();
writeMethod.invoke(user, "panmin");
System.out.println("set userName:" + user.getUserName());
break;
}
}
}
}
public static void getPropertyByIntrospector(UserInfo user) throws Exception{
BeanInfo beanInfo = Introspector.getBeanInfo(user.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if(propertyDescriptors!=null && propertyDescriptors.length>0){
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method readMethod = propertyDescriptor.getReadMethod();
Object object = readMethod.invoke(user);
System.out.println(object.toString());
}
}
}
public static void main(String[] args) throws Exception {
UserInfo userInfo = new UserInfo();
setProperty(userInfo);
getProperty(userInfo);
}
}
以下例子是用的ListComparator排序
package com.letv.common.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Comparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author zxwu
*/
public class ListDataComparator implements Comparator
{
private Log logger = LogFactory.getLog(this.getClass());
private String field;
private boolean asc = false;
public ListDataComparator(){
}
public ListDataComparator(String field){
this.setField(field);
}
/**
* @return the field
*/
public String getField() {
return field;
}
/**
* @param field the field to set
*/
public void setField(String field) {
this.field = field;
}
/**
* @return the asc
*/
public boolean isAsc() {
return asc;
}
/**
* @param asc the asc to set
*/
public void setAsc(boolean asc) {
this.asc = asc;
}
@Override
public int compare(Object o1, Object o2) {
try {
BeanInfo info = Introspector.getBeanInfo(o1.getClass());
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; i++) {
if (props[i].getName().equals(field)) {
Method method = props[i].getReadMethod();
try {
Integer f1 = (Integer) method.invoke(o1, new Object[0]);
Integer f2 = (Integer) method.invoke(o2, new Object[0]);
int result = f1.compareTo(f2);
if (isAsc()) {
return result;
} else {
if (result < 0) {
return 1;
} else if (result > 0) {
return -1;
}
}
} catch (java.lang.ClassCastException e) {
try {
Float f1 = (Float) method.invoke(o1, new Object[0]);
Float f2 = (Float) method.invoke(o2, new Object[0]);
int result = f1.compareTo(f2);
if (isAsc()) {
return result;
} else {
if (result < 0) {
return 1;
} else if (result > 0) {
return -1;
}
}
} catch (java.lang.ClassCastException e1) {
try {
String f1 = (String) method.invoke(o1, new Object[0]);
String f2 = (String) method.invoke(o2, new Object[0]);
int result = f1.compareTo(f2);
if (isAsc()) {
return result;
} else {
if (result < 0) {
return 1;
} else if (result > 0) {
return -1;
}
}
} catch (java.lang.ClassCastException e2) {
Long f1 = (Long) method.invoke(o1, new Object[0]);
Long f2 = (Long) method.invoke(o2, new Object[0]);
int result = f1.compareTo(f2);
if (isAsc()) {
return result;
} else {
if (result < 0) {
return 1;
} else if (result > 0) {
return -1;
}
}
}
}
}
break;
}
}
} catch (Exception e) {
logger.error(e, e);
}
return 0;
}
}
/** * 对list对象中的id字段进行排序 * * @param list * @return */ private List getDescList(List list) { ListDataComparator compare = new ListDataComparator("id"); compare.setAsc(false); Collections.sort(list, compare); return list; }