Java内省(基础一)

来源:互联网 发布:关口知宏 新疆妹子 编辑:程序博客网 时间:2024/06/11 02:37

程序代码:内省Person类。

package neixing;

public class Person {

 private String name;
 private String password;
 private int age;
 private String province;// 这个不是Person的属性,因为他并没有提供相应 的get或者set方法。

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getAddress() {
  System.out.println();
  return "";
 }

 // 只要Person类提供了相应的get或set方法(get
 // 方法有返回值,set方法接收传入的参数),那么对应的这个就是他的属性,比如说上面的那个getAddress方法也是他的属性
 // 因为Person类继承自object,object有一个getClass方法,所以Person类共有5个属性

}
----------------------------------
测试代码:


package neixing;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo1 {
 // 内省APi操作Bean属性
 @Test
 public void test1() throws IntrospectionException {
  BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
  PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();// 得到属性描述器
  for (PropertyDescriptor pd : pds) {
   System.out.println(pd);// 获知Bean到底有多少属性
   // 程序输出一下内容,共四个属性
   // java.beans.PropertyDescriptor[name=age; propertyType=int;
   // readMethod=public int neixing.Person.getAge(); writeMethod=public
   // void neixing.Person.setAge(int)]
   // java.beans.PropertyDescriptor[name=address; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getAddress()]
   // java.beans.PropertyDescriptor[name=age; propertyType=int;
   // readMethod=public int neixing.Person.getAge(); writeMethod=public
   // void neixing.Person.setAge(int)]
   // java.beans.PropertyDescriptor[name=class; propertyType=class
   // java.lang.Class; readMethod=public final native java.lang.Class
   // java.lang.Object.getClass()]
   // java.beans.PropertyDescriptor[name=name; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getName(); writeMethod=public void
   // neixing.Person.setName(java.lang.String)]
   // java.beans.PropertyDescriptor[name=password; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getPassword(); writeMethod=public void
   // neixing.Person.setPassword(java.lang.String)]
   // 这个说明了属性是由get或者set方法决定的

  }

 }

 @Test
 public void test2() throws Exception {
  // ------上面中class属性是父类的属性,只是子类继承了object的getClass()方法。如果只想获取Bean自己的属性,那该怎么做??
  // Open Declaration BeanInfo
  // java.beans.Introspector.getBeanInfo(Class<?> beanClass, Class<?>
  // stopClass) throws IntrospectionException
  // 其中Class<?> stopClass表示不再内省这个类的继承树种包括这个类以及这个类的父类的所有属性。
  BeanInfo beanInfo = Introspector
    .getBeanInfo(Person.class, Object.class);
  PropertyDescriptor[] pds2 = beanInfo.getPropertyDescriptors();// 得到属性描述器
  for (PropertyDescriptor pd : pds2) {
   System.out.println(pd);// 获知Bean到底有多少属性

   // 打印结果表明不再内省父类Object 的Class属性
   // java.beans.PropertyDescriptor[name=address; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getAddress()]
   // java.beans.PropertyDescriptor[name=age; propertyType=int;
   // readMethod=public int neixing.Person.getAge(); writeMethod=public
   // void neixing.Person.setAge(int)]
   // java.beans.PropertyDescriptor[name=class; propertyType=class
   // java.lang.Class; readMethod=public final native java.lang.Class
   // java.lang.Object.getClass()]
   // java.beans.PropertyDescriptor[name=name; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getName(); writeMethod=public void
   // neixing.Person.setName(java.lang.String)]
   // java.beans.PropertyDescriptor[name=password; propertyType=class
   // java.lang.String; readMethod=public java.lang.String
   // neixing.Person.getPassword(); writeMethod=public void
   // neixing.Person.setPassword(java.lang.String)]

  }

 }

 @Test
 public void test3() throws Exception {

  // 获取属性最终是为了操作属性,如何操作?
  Person p = new Person();
  // 操作age属性
  PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
    Person.class);// 第一个是属性名
  // 这样pdDescriptor就专门用来操作age属性.操作无非就是写数据和读数据
  Method method = pdDescriptor.getWriteMethod();// ---->public void
              // setAge(int age);
  method.invoke(p, 45);
  System.out.println(p.getAge());// 成功打印
  // 获取属性的值
  Method methodRead = pdDescriptor.getReadMethod();// ---->public void
               // getAge()
  int ageRead = (Integer) methodRead.invoke(p, null);// get方法不需要接收参数
  System.out.println(ageRead);
 }

 /**
  * 获取当前属性的类型
  *
  * @throws IntrospectionException
  *
  */
 @Test
 public void test4() throws IntrospectionException {
  // 操作age属性
  PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
    Person.class);// 第一个是属性名
  // 查看age属性的类型
  Class classz = pdDescriptor.getPropertyType();
  System.out.println(classz);// 打印:int
 }

}


0 0
原创粉丝点击