Introspector

    <br><br> introspector与反射类似,主要是对java bean属性、方法等的一种处理方法.
 
1. class diagram
 
 <img src="http://dl.iteye.com/upload/attachment/482916/592d562b-9054-3026-8b27-7cdb400a8207.jpg" alt="">
 
2. 实例
 
package com.siyuan.jdktest;
import java.beans.beandescriptor;<br>import java.beans.beaninfo;<br>import java.beans.introspectionexception;<br>import java.beans.introspector;<br>import java.beans.methoddescriptor;<br>import java.beans.propertydescriptor;<br>import java.lang.reflect.method;
class person {<br> <br> private string name;<br> <br> private int age;
 /**<br>  * @return the age<br>  */<br> public int getage() {<br>  return age;<br> }
 /**<br>  * @param age the age to set<br>  */<br> public void setage(int age) {<br>  this.age = age;<br> }
 /**<br>  * @return the name<br>  */<br> public string getname() {<br>  return name;<br> }
 /**<br>  * @param name the name to set<br>  */<br> public void setname(string name) {<br>  this.name = name;<br> }<br> <br>}
public class introspectortest {
 /**<br>  * @param args<br>  * @throws introspectionexception <br>  */<br> public static void main(string[] args) throws introspectionexception {<br>  // todo auto-generated method stub<br>  beaninfo beaninfo = introspector.getbeaninfo(person.class);<br>  <br>  system.out.println("beandescriptor===========================================");<br>  beandescriptor beandesc = beaninfo.getbeandescriptor();<br>  class cls = beandesc.getbeanclass();<br>  system.out.println(cls.getname());<br>  <br>  system.out.println("methoddescriptor===========================================");<br>  methoddescriptor[] methoddescs = beaninfo.getmethoddescriptors();<br>  for (int i = 0; i < methoddescs.length; i++) {<br>   method method = methoddescs[i].getmethod();<br>   system.out.println(method.getname());<br>  }<br>  <br>  system.out.println("propertydescriptor===========================================");<br>  propertydescriptor[] propdescs = beaninfo.getpropertydescriptors();<br>  for (int i = 0; i < propdescs.length; i++) {<br>   method methodr = propdescs[i].getreadmethod();<br>   if (methodr != null) {<br>    system.out.println(methodr.getname());<br>   }<br>   method methodw = propdescs[i].getwritemethod();<br>   if (methodw != null) {<br>    system.out.println(methodw.getname());<br>   }<br>  }<br> }
}
3. 运行结果
 
beandescriptor===========================================<br>com.siyuan.jdktest.person<br>methoddescriptor===========================================<br>hashcode<br>setage<br>equals<br>wait<br>wait<br>notify<br>getclass<br>tostring<br>getage<br>notifyall<br>setname<br>wait<br>getname<br>propertydescriptor===========================================<br>getage<br>setage<br>getclass<br>getname<br>setname
 

你可能感兴趣的:(java,工作)