A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张强+ 黑马帝   /  2011-10-25 23:44  /  2377 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;

import com.sun.corba.se.impl.orbutil.GetPropertyAction;
import com.sun.jmx.mbeanserver.Introspector;

import reflect.MethodDemo;


public class IntroSpectorTest {

        /**
         * @param args
         * @throws IntrospectionException
         * @throws Exception
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         */
        public static void main(String[] args) throws Exception {
           ReflectPoint pt1=new ReflectPoint(3,5);
           
           String propertyName="x";
           //"x"--->"X"-->"getX"-->MethodGetX-->
         Object retVal=getProperty(pt1,propertyName);
            System.out.println(retVal);
            
             Object value=7;
         setProperties(pt1, propertyName, value);
          System.out.println( pt1.getX());
        }

        private static void setProperties(Object pt1, String propertyName, Object value)
                        throws Exception {
                PropertyDescriptor pd2=new PropertyDescriptor(propertyName,pt1.getClass());
                Method methodSetX=pd2.getWriteMethod();
                     methodSetX.invoke(pt1,value);
        }

        private static Object getProperty(Object pt1, String propertyName) throws Exception {
//        PropertyDescriptor pd=new PropertyDescriptor(propertyName,pt1.getClass());
        /*Method methodGetX=pd.getReadMethod();
          Object retVal=methodGetX.invoke(pt1);
          return retVal;*/
            BeanInfo beanInfo=Introspector.getBeanInfo(pt1.getClass());
        PropertyDescriptor []pds=beanInfo.getPropertyDescriptors();
        Object retVal=null;
                for (PropertyDescriptor pd : pds) {
                          if(pd.getName().equals(propertyName)){
                                  Method methodGetX = pd.getReadMethod();
                                  retVal=methodGetX.invoke(pt1);
                                  break;
                          }
                }
          return retVal;
        }

}

getBeanInfo报错,这是为什么?


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        The method getBeanInfo(Class<capture#2-of ?>) is undefined for the type Introspector

        at IntroSpectorTest.getProperty(IntroSpectorTest.java:48)
        at IntroSpectorTest.main(IntroSpectorTest.java:28)

怎么解决这个问题。。。

1 个回复

倒序浏览
郭敏 黑马帝 2011-10-26 08:44:01
沙发
兄弟! 你的导入的包出错了
import com.sun.corba.se.impl.orbutil.GetPropertyAction;
import com.sun.jmx.mbeanserver.Introspector;
实际IntroSpector 是包
import java.beans.IntrospectionException;
import java.beans.Introspector;
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马