1构造方法
通过调用 getFoo 和 setFoo 存取方法,为符合标准 Java 约定的属性构造一个PropertyDescriptor
public PropertyDescriptor(String propertyName, Class<?> beanClass)该构造函数使用简单属性的名称,以及读取和写入属性的方法名称
public PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName)此构造函数使用简单属性的名称,以及用于读取和写入属性的Method对象。
public PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)
测试
@Test public void show() throws Exception { Class clazz = UserInfo.class; PropertyDescriptor p = new PropertyDescriptor("name", clazz); System.out.println("显示属性描述类:" + p); }
输出:
显示属性描述类:
java.beans.PropertyDescriptor[
name=name;
propertyType=class java.lang.String;
readMethod=public java.lang.String com.example.feizhou.model.UserInfo.getName();
writeMethod=public void com.example.feizhou.model.UserInfo.setName(java.lang.String)
]
2public Method getWriteMethod()
获取属性的setter方法
测试
@Test public void getWriteMethod() throws Exception { Object obj = clazz.newInstance(); //setter方法给属性赋值 Method method = pd.getWriteMethod(); method.invoke(obj, "xiaoming"); System.out.println(obj.toString()); }输出
UserInfo{name='xiaoming'}
3public method getWriteMethod()
获取属性的getter方法
测试
@Test public void getReadMethod() throws Exception { Object obj = clazz.newInstance(); Field field = clazz.getDeclaredField("name"); field.setAccessible(true); field.set(obj, "xiaoming"); //getter方法给属性赋值 Method method = pd.getReadMethod(); method.invoke(obj); System.out.println(obj.toString()); }输出:
UserInfo{name='xiaoming'}
4public Class<?> getPropertyType()
获取属性的类型
测试
@Test public void getPropertyType() throws Exception { Class<?> propertyType = pd.getPropertyType(); System.out.println("propertyType:"+propertyType); }输出:
propertyType:class java.lang.String
5public void setReadMethod(Method readMethod)
设置用于读取属性值的方法
测试
@Test public void setReadMethod() throws Exception { Object obj = clazz.newInstance(); Method getName2=clazz.getDeclaredMethod("getName2"); //设置读属性的方法 pd.setReadMethod(getName2); //获取读属性的方法 Method method = pd.getReadMethod(); Object invoke = method.invoke(obj); System.out.println(invoke.toString()); }输出:getName2
6public void setWriteMethod(Method writeMethod)
设置用于写入属性值的方法
测试
@Test public void setWriteMethod() throws Exception { Object obj = clazz.newInstance(); Method setName2=clazz.getDeclaredMethod("setName2",String.class); //设置写属性的方法 pd.setWriteMethod(setName2); //获取写属性的方法 Method method = pd.getWriteMethod(); method.invoke(obj,"1111"); System.out.println(obj); }输出:
UserInfo{name='1111'}
测试的数据
package com.example.feizhou.ex;
import com.example.feizhou.model.UserInfo;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author hui
* @description
* @create 2018/8/30 上午11:29
*/
public class PropertyDescriptorTest {
private Class clazz;
private PropertyDescriptor pd;
@BeforeTest
public void init() throws Exception {
this.clazz = UserInfo.class;
this.pd = new PropertyDescriptor("name", clazz);
}
@Test
public void show() throws Exception {
Class clazz = UserInfo.class;
PropertyDescriptor p = new PropertyDescriptor("name", clazz);
System.out.println("显示属性描述类:" + p);
}
@Test
public void getWriteMethod() throws Exception {
Object obj = clazz.newInstance();
//setter方法给属性赋值
Method method = pd.getWriteMethod();
method.invoke(obj, "xiaoming");
System.out.println(obj.toString());
}
@Test
public void getReadMethod() throws Exception {
Object obj = clazz.newInstance();
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
field.set(obj, "xiaoming");
//setter方法给属性赋值
Method method = pd.getReadMethod();
method.invoke(obj);
System.out.println(obj.toString());
}
@Test
public void getPropertyType() throws Exception {
Class<?> propertyType = pd.getPropertyType();
System.out.println("propertyType:"+propertyType);
}
@Test
public void setReadMethod() throws Exception {
Object obj = clazz.newInstance();
Method getName2=clazz.getDeclaredMethod("getName2");
//设置读属性的方法
pd.setReadMethod(getName2);
//获取读属性的方法
Method method = pd.getReadMethod();
Object invoke = method.invoke(obj);
System.out.println(invoke.toString());
}
@Test
public void setWriteMethod() throws Exception {
Object obj = clazz.newInstance();
Method setName2=clazz.getDeclaredMethod("setName2",String.class);
//设置写属性的方法
pd.setWriteMethod(setName2);
//获取写属性的方法
Method method = pd.getWriteMethod();
method.invoke(obj,"1111");
System.out.println(obj);
}
}
---------------------------------------
public class UserInfo {
private String name;
public String getName() {
return name;
}
public String getName2() {
return "getName2";
}
public void setName(String name) {
this.name = name;
}
public void setName2(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserInfo{" +
"name='" + name + '\'' +
'}';
}
}