- public class IntrospectorDemo {
- public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- // TODO Auto-generated method stub
- IntrospectorDemo demo=new IntrospectorDemo();
- demo.setName("pengfeilu");
- //内省
- BeanInfo bi=Introspector.getBeanInfo(demo.getClass());
- PropertyDescriptor[] pd=bi.getPropertyDescriptors();
-
- Object retval=null;
- for (PropertyDescriptor propertyDescriptor : pd) {
- //String name=propertyDescriptor.getName();
- Method method=propertyDescriptor.getReadMethod();
- retval= method.invoke(demo);
- System.out.println((String)retval);
- }
- }
- String name=null;
- String getName() {
- return name;
- }
- void setName(String name) {
- this.name = name;
- }
- }
复制代码 只是简单的调用内省,获取变量的get方法。调用方法获取name……//结果:Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String at com.itheima.IntrospectorDemo.main(IntrospectorDemo.java:25)
|