JavaBean的簡單內省操作 以及復雜內省操作 – JAVA編程語言程序開發技術文章

2段簡單的JavaBean代碼演示瞭簡單內省操作以及復雜內省操作  。
1、簡單內省操作
package me.test;
import java.lang.reflect.*;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
public class IntroSpectorTest
{
   public  static void main(String []args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
   {
    JavaBeanTest t=new JavaBeanTest() ;
    t.setX(10);
    PropertyDescriptor d=new PropertyDescriptor("X",JavaBeanTest.class);
    setProperty(t, d);
    Object val = getProperty(t, d);
    System.out.println(val);
   
   }
private static Object getProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException {
 Method mr=d.getReadMethod()  ;
    Object val=mr.invoke(t);
 return val;
}
private static void setProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException {
    Method mw=d.getWriteMethod()  ;
    mw.invoke(t, 5) ;
}
}
class  JavaBeanTest
{
 private int x  ;
 public void setX(int x)
 {
  this.x=x ;
 }
 public int getX()
  
 {
  return this.x ;
 }
}
 
2、復雜內省操作   BeanInfo類   Introspector類的使用
package me.test;
import java.lang.reflect.*;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
public class IntroSpectorTest
{
   public  static void main(String []args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
   {
    JavaBeanTest t=new JavaBeanTest(10) ;
    PropertyDescriptor d=new PropertyDescriptor("x",JavaBeanTest.class);
    setProperty(t, d);
    Object val = getProperty(t, d);
    System.out.println(val);
   
   }
private static Object getProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException, IntrospectionException {
    BeanInfo beanInfo=Introspector.getBeanInfo(t.getClass()) ;  
 PropertyDescriptor  pt[]=beanInfo.getPropertyDescriptors() ;
  for(PropertyDescriptor tem:pt)
  {
   if(tem.getName().equals(d.getName()))
     {
             Method mr=tem.getReadMethod() ;
             Object val=mr.invoke(t) ;
             return val ;
     }
  }
 return null;
}
private static void setProperty(JavaBeanTest t, PropertyDescriptor d)
  throws IllegalAccessException, InvocationTargetException, IntrospectionException {
   
    BeanInfo beanInfo=Introspector.getBeanInfo(t.getClass() ) ; //把JavaBeanTest的對象當做JavaBean看有什麼信息封裝在BeanInfo中
    PropertyDescriptor [] pd=beanInfo.getPropertyDescriptors() ;  //Get All Properties From BeanInfo Class
    for(PropertyDescriptor tem:pd)
    {  
     if(tem.getName().equals(d.getName()))
     {
      Method mw=tem.getWriteMethod() ;
      mw.invoke(t, 50)  ;
      break ;
     }
    }
}
}
class  JavaBeanTest
{
 private int x  ;
 public JavaBeanTest(int x)
 {
  this.x=x ;
 }
 public void setX(int x)
 {
  this.x=x ;
 }
 public int getX()
  
 {
  return this.x ;
 }
}
  摘自 yue7603835的專欄

發佈留言