JAVA反射之内省

JavaBean:
就是一个java类
属性全部私有,提供公有的getter,setter方法
提供一个无参的构造方法
类必须公有,必须打包


JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。

内省API:
Introspector类: 构建一个全面描述目标 bean 的 BeanInfo 对象
BeanInfo接口:封装bean的信息 实例通过Introspector的静态方法getBeanInfo(Class<?> beanClass)得到
MethodDescriptor[] getMethodDescriptors():返回该bean类的所有方法的描述信息
PropertyDescriptor[] getPropertyDescriptors():返回该bean类的所有属性的描述信息


-------------------------------------------------
MethodDescriptor类:描述方法
Method getMethod()

重点常用方法:
PropertyDescriptor类:描述属性
PropertyDescriptor(String propertyName, Class<?> beanClass):根据指定的属性名和目标bean类的class对象创建一个PropertyDescriptor 对象
Class<?> getPropertyType():返回属性类型的class对象
Method getReadMethod():返回属性的get方法
Method getWriteMethod():返回属性的set方法
---------------------------------------------------------

Array 类提供了动态创建和访问 Java 数组的方法。
static Object newInstance(Class<?> componentType,int... dimensions):创建一个多维动态数组 第一个参数指定元素类型,第二个指定每个维度的长度
static Object newInstance(Class<?> componentType, int length) :创建一个一维度数组 第一个参数指定元素类型,第二个指定数组长度

static void set(Object array, int index, Object value):设置指定数组 指定索引的元素为指定值
static Object get(Object array, int index):获取数组指定索引的元素
static int getLength(Object array):获取数组长度

-----------------------------------

猜你喜欢

转载自www.cnblogs.com/java888/p/10391315.html