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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张强+ 黑马帝   /  2011-11-10 21:35  /  2906 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
       
       
        public class IntroSpectorTest {
       
                public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
                   ReflectPoint p1=new ReflectPoint(3,5);
                   
                    String properyName="x";
        //            "x"==>"X"==>"getX"--->MethodGetX-->
            Object retVal = getProperty(p1, properyName);
          System.out.println(retVal);
          
                  setProperty(p1, properyName);
                  
                  System.out.println(BeanUtils.getProperty(p1, "x").getClass().getName());
                  BeanUtils.setProperty(p1, "x", "9");
                  Method method= p1.getClass().getDeclaredMethod("getX",int.class);
                  method.setAccessible(true);
                  System.out.println(p1.getX());   //这段代码的问题getX报错,无权访问,问什么,我已经setsetAccessible(true);了呀,private int getX();
/*                           //  java7新特性
//                   Map map={name:"zxx",age:18};// jdk1.7新特性
//                   BeanUtils.setProperty(map, "name", "lhm");
*/                   
                    BeanUtils.setProperty(p1, "birthday.time", "111"        );
                      System.out.println(BeanUtils.getProperty(p1, "birthday.time"));
               
                      PropertyUtils.setProperty(p1, "x", 9);
                      System.out.println(PropertyUtils.getProperty(p1, "x").getClass().getName());

                }
        private static void setProperty(ReflectPoint p1, String properyName)
                        throws IntrospectionException, IllegalAccessException,
                        InvocationTargetException {
                PropertyDescriptor pd1=new PropertyDescriptor(properyName,p1.getClass());
               Method methodSetX=pd1.getWriteMethod();
               methodSetX.invoke(p1,7);
        }
        private static Object getProperty(ReflectPoint p1, String properyName)
                        throws IntrospectionException, IllegalAccessException,
                        InvocationTargetException {
                /* PropertyDescriptor pd=new PropertyDescriptor(properyName,p1.getClass());
         Method methodGetX=pd.getReadMethod();
          Object retVal=methodGetX.invoke(p1);
         return retVal;*/
                  BeanInfo beanInfo=Introspector.getBeanInfo(p1.getClass());
                  PropertyDescriptor []pds=beanInfo.getPropertyDescriptors()        ;
                  Object retVal=null;
                  for (PropertyDescriptor pd : pds) {
                          if(pd.getName().equals(properyName)){
                                  Method methodGetX=pd.getReadMethod();
                                  retVal=methodGetX.invoke(p1);
                                  break;
                          }
                }
                  return retVal;
                }       
        }

该贴已经同步到 张强+的微博

2 个回复

倒序浏览
有什么办法可以做到?
回复 使用道具 举报
本帖最后由 郭学文 于 2011-11-10 22:11 编辑

Method method= p1.getClass().getDeclaredMethod("getX",int.class);//getX是没有参数的,你这里是在得到一个带参数的getX(int x)的方法
                  method.setAccessible(true);
                  System.out.println(p1.getX());另外这里不可以是这样子的,你这样子是直接引用p1的一个私有方法,没有通过反射
应该是method.invoke(p1);


  1. BeanUtils.setProperty(p1, "x", 9);
  2.                 Method method=p1.getClass().getDeclaredMethod("getX");
  3.                 method.setAccessible(true);
  4.                 System.out.print(method.invoke(p1));
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马