package com.nemo.reflect;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 有几种实现反射的方式:
* 1、利用java.lang.refect包
* 2、利用java.beans包
* 3、利用组件入BeanUtils and PropertyUtils
*
* @author tanyouhao
*
*/
public class User {
public User(){}
public User(String name,String passwd){
this.name = name;
this.passwd = passwd;
}
public String name;
public String passwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
/**
* 测试java.lang.reflect包进行发射测试
*
* @throws Exception
*/
public static void testReflect() throws Exception{
Class cls = Class.forName("com.nemo.reflect.User");
Object ob = cls.newInstance();
//获取所有构造器
Constructor[] cons = cls.getConstructors();
Object o = null;
for(Constructor co:cons){
Class[] c = co.getParameterTypes();
if(c.length==2){
o = co.newInstance("name1","passd1");
break;
}
}
System.out.println("由构造器构造的对象之:name="+((User)o).getName()+",passwd="+((User)o).getPasswd());
//获取所有字段
Field[] fields = cls.getDeclaredFields();
for(Field field:fields){
String fieldName = field.getName();
//根据方法名和形式参数获取method,String.class是可变参数
//getDeclaredMethod(String name, Class<?>... parameterTypes)
Method method = cls.getDeclaredMethod("set"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1),String.class);
//请求调用method
method.invoke(ob, new String[]{"love"});
}
//获取所有方法
Method[] methods = cls.getMethods();
for(Method method:methods){
//执行get方法
if(method.getName().startsWith("get")){
System.out.println(method.getName()+"的值是:"+method.invoke(ob, null));
}
}
}
/**
*
* 测试<br>直接通过属性的描述器java.beans.PropertyDescriptor类,来访问属性的getter/setter 方法
*
* @param user 对象
* @param propertyName 要设置的属性
* @throws IntrospectionException
*/
public static void testPropertyDescriptor(User user,String propertyName) throws Exception{
PropertyDescriptor des = new PropertyDescriptor(propertyName,User.class);
//获取set方法
Method setMethod = des.getWriteMethod();
setMethod.invoke(user, "name_desc_set");
//获取get方法
Method getMethod = des.getReadMethod();
Object value = getMethod.invoke(user);
System.out.println("利用属性描述器设置bean调用set,get方法:"+value);
}
public static void testIntrospector(User user,String propertyName) throws Exception{
BeanInfo info = Introspector.getBeanInfo(User.class);
//获取所有属性描述
PropertyDescriptor[] pros = info.getPropertyDescriptors();
for(PropertyDescriptor pr:pros){
if(pr.getName().equals(propertyName)){
Method setMethod = pr.getWriteMethod();
setMethod.invoke(user,"nnme_introsprce_set");
Method getMethod = pr.getReadMethod();
Object value = getMethod.invoke(user);
System.out.println("利用内省设置bean调用set,get方法:"+value);
}
}
//获取所有方法描述
MethodDescriptor[] mthods = info.getMethodDescriptors();
System.out.println("********获取所有方法**********");
for(MethodDescriptor me:mthods){
System.out.println(me.getName());
}
}
public static void main(String[] args) throws Exception {
User.testReflect();
User u = new User();
User.testPropertyDescriptor(u, "name");
User.testIntrospector(u, "name");
}
}
结果:
由构造器构造的对象之:name=name1,passwd=passd1
getName的值是:love
getPasswd的值是:love
getClass的值是:class com.nemo.reflect.User
利用属性描述器设置bean调用set,get方法:name_desc_set
利用内省设置bean调用set,get方法:nnme_introsprce_set
********获取所有方法**********
getPasswd
testPropertyDescriptor
hashCode
wait
testReflect
setPasswd
notifyAll
equals
wait
testIntrospector
wait
toString
setName
notify
getClass
main
getName