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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 曹冬明 中级黑马   /  2014-4-21 09:55  /  1417 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itast;

  2. import java.beans.BeanInfo;
  3. import java.beans.IntrospectionException;
  4. import java.beans.Introspector;
  5. import java.beans.PropertyDescriptor;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;

  8. import org.apache.commons.beanutils.BeanUtils;

  9. public class IntroSpector {

  10.         public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  11.                 // TODO Auto-generated method stub
  12.                 ReflectPoint pt1 = new ReflectPoint(3, 5);
  13.                
  14.                 String propertyName = "x";
  15.                 //"x"-->"X"-->"getX"-->methodGetX-->
  16.                 Object retVal = getProperty(pt1, propertyName);
  17.                 System.out.println(retVal);
  18.                
  19.                 Object value = 7;
  20.                 setProperty(pt1, propertyName, value);
  21.                 BeanUtils.getProperty(pt1,"x");
  22.                 System.out.println(pt1.getX());
  23.         }

  24.         private static void setProperty(ReflectPoint pt1, String propertyName,
  25.                         Object value) throws IntrospectionException,
  26.                         IllegalAccessException, InvocationTargetException {
  27.                 PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, pt1.getClass());
  28.                 Method methodSetX = pd2.getWriteMethod();
  29.                 methodSetX.invoke(pt1,value);
  30.         }j

  31.         private static Object getProperty(Object pt1, String propertyName)
  32.                         throws IntrospectionException, IllegalAccessException,
  33.                         InvocationTargetException {
  34.                 /*PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());
  35.                 Method methodGetX = pd.getReadMethod();
  36.                 Object retVal = methodGetX.invoke(pt1);*/
  37.                 BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
  38.                 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
  39.                 Object retVal = null;
  40.                 for(PropertyDescriptor pd : pds){
  41.                         if(pd.getName().equals(propertyName)){
  42.                                 Method methodGetX = pd.getReadMethod();
  43.                                 retVal = methodGetX.invoke(pt1);
  44.                                 break;
  45.                         }
  46.                 }
  47.                        
  48.                
  49.                 return retVal;
  50.         }

  51. }
复制代码

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

        at cn.itast.IntroSpector.main(IntroSpector.java:14)
报上面的异常,昨天也是这个问题,14行就是主函数声明的那一行

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

2 个回复

倒序浏览
第35行多个 j
回复 使用道具 举报
这get和set 方法我怎么就抽不出来呢?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马