zoukankan      html  css  js  c++  java
  • 对javabean的内省操作

    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 {

     /**
      * @param args
      */
     public static void main(String[] args) throws Exception {
      // TODO Auto-generated method stub
      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(BeanUtils.getProperty(pt1, "x").getClass().getName());
      BeanUtils.setProperty(pt1, "x", "9");
      System.out.println(pt1.getX());
      /*
      //java7的新特性
      Map map = {name:"zxx",age:18};
      BeanUtils.setProperty(map, "name", "lhm");
      */
      BeanUtils.setProperty(pt1, "birthday.time", "111");
      System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
      
      PropertyUtils.setProperty(pt1, "x", 9);
      System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());
      
     }

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

     private static Object getProperty(Object pt1, String propertyName)
       throws IntrospectionException, IllegalAccessException,
       InvocationTargetException {
      /*PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
      Method methodGetX = pd.getReadMethod();
      Object retVal = methodGetX.invoke(pt1);*/
      
      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;
     }

    }

  • 相关阅读:
    常用模块汇总
    day 16 常用模块 time 和 datetime(1)
    二分法 函数式 模块
    day 15 模块 (3)
    vue require.context 检索文件夹内文件路径
    Node.js搭建本地服务,读取css/js/img/html等各种格式文件
    Nodejs搭建web服务器
    el-table横向滚动条固定在屏幕最下方显示
    IE浏览器 backspace键使浏览器回退
    vue ElementUI el-input 键盘enter事件 导致刷新表单问题
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3235647.html
Copyright © 2011-2022 走看看