【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了BeanIntrospectorExample.java提供大家参考,希望对大家有所帮助!
package ejava.beansch7;
import java.lang.reflect.*;
import java.beans.Beans;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.BeanDescriptor;
import java.beans.PropertyDescriptor;
import java.beans.MethodDescriptor;
import java.beans.EventSetDescriptor;
import java.beans.ParameterDescriptor;
/**
* This class introspects the AuditBean
*/
public class BeanIntrospectorExample
{
public static void main(String[] args)
{
try{
ClassLoader classLoader = null;
// Instantiate a bean
AuditBean auditBean = (AuditBean)Beans.instantiate(
classLoader,"ejava.beansch7.AuditBean");
// Get Class for the bean
Class presentClass = auditBean.getClass();
// Get super Class for the bean
Class superClass = auditBean.getClass().getSuperclass();
// get BeanInfo
BeanInfo beanInfo =
Introspector.getBeanInfo(presentClass, superClass);
// Get the descriptor for the Bean
BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
// Get the display name of the bean
String displayName = beanDescriptor.getDisplayName();
System.out.println(" Display Name :"+displayName);
//Get the properties of the bean
PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
//Print the properties
System.out.println("Properties :");
for ( int i = 0; i < propertyDescriptors.length; i++ ) {
System.out.println(" Property Name :" +
propertyDescriptors[i].getName());
System.out.println(" Set Method Name :" +
propertyDescriptors[i].getWriteMethod());
System.out.println(" Get Method Name :" +
propertyDescriptors[i].getReadMethod());
}
System.out.println(" Methods :");
//Get the methods of the bean
MethodDescriptor[] methodDescriptors=
beanInfo.getMethodDescriptors();
//Print the methods
for(int i = 0; i<methodDescriptors.length; i++){
System.out.println(" Method Name :" +
methodDescriptors[i].getMethod());
ParameterDescriptor[] parameterDescriptors =
methodDescriptors[i].getParameterDescriptors();
for(int j = 0; j<parameterDescriptors.length; j++){
System.out.println("Parameter Name :" +
parameterDescriptors[j].getName());
}
}
//Get the events of the bean
EventSetDescriptor[] eventSetDescriptors =
beanInfo.getEventSetDescriptors();
//Print the events
System.out.println("Events :");
for(int i = 0; i< eventSetDescriptors.length; i++){
System.out.println("EventName:" +
eventSetDescriptors[i].getName());
}
Class auditBeanClass = auditBean.getClass();
Class[] interfaces = auditBeanClass.getInterfaces();
for(int i = 0; i< interfaces.length; i++){
String name = interfaces[i].getName();
if(name.equalsIgnoreCase("java.io.Serializable")){
System.out.println(" The bean is serializable");
} else if (name.equalsIgnoreCase("java.io.Externalizable")){
System.out.println("The bean is Externalizable ");
}
}
}
catch (Exception exception) {
System.out.println(exception);
exception.printStackTrace();
}
}
}