Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.PropertyUtils


  public static void  populate(Object bean,HttpRequest request, Map<?, ?> m)
  {
    ListOrderedMap orderedMap = new ListOrderedMap();
    Map<?, ?> map=request.getParameterMap();
    orderedMap.putAll(map);
    PropertyUtils propertyUtils = new PropertyUtils();
    @SuppressWarnings("unchecked")
    List<String> list=orderedMap.asList();
    for (String string : list)
    {
      String[] name = StringUtils.split(string, ".");
     
      if (name.length==1)
      {
        String objectString= (String) m.get(string);
        if (propertyUtils.isReadable(bean, string)&&propertyUtils.isWriteable(bean, string)&&objectString!=null)
        {
          try {
            if (StringUtils.containsIgnoreCase(objectString, "java.lang.String"))
            {
              //设置String
              propertyUtils.setProperty(bean, string, request.getParameter(string));
            }else if (StringUtils.containsIgnoreCase(objectString, "java.lang.Integer")
                ||StringUtils.containsIgnoreCase(objectString, "java.lang.Short")
                ||StringUtils.containsIgnoreCase(objectString, "java.lang.Long")
                ||StringUtils.containsIgnoreCase(objectString, "java.lang.Float")
                ||StringUtils.containsIgnoreCase(objectString, "java.lang.Double")
                ||StringUtils.containsIgnoreCase(objectString, "java.lang.Byte")
                ||StringUtils.containsIgnoreCase(objectString, "java.math.BigInteger")
                ||StringUtils.containsIgnoreCase(objectString, "java.math.BigDecimal"))
            {
              if (NumberUtils.isNumber(request.getParameter(string))) {
                //设置Number
                propertyUtils.setProperty(bean, string, NumberUtils.createNumber(request.getParameter(string)));
              }
             
            }else if (StringUtils.containsIgnoreCase(objectString, "java.util.Date"))
            {
              String s = request.getParameter(string);
              if (StringUtils.isNotEmpty(s))
              {
                //设置时间
                String[] parsePatterns ={"yyyy/MM/dd","yyyy/MM/dd HH:mm:ss",
                    "yyyy.MM.dd","yyyy.MM.dd HH:mm:ss",
                    "yyyy-MM-dd","yyyy-MM-dd HH:mm:ss",
                    "HH:mm:ss"};
                Date parsedDate = null;
                try {
                  parsedDate= DateUtils.parseDate(s,
                      parsePatterns);
                } catch (ParseException e ) {
                  log.error("{}不是正确的时间格式",s);
                }
                propertyUtils.setProperty(bean, string, parsedDate);
             
            }
           
          } catch (IllegalAccessException e)
          {log.error("给类{}赋值错误!",bean);}
          catch (InvocationTargetException e)
          {log.error("给类{}赋值错误!",bean);}
          catch ( NoSuchMethodException e)
          {
            log.error("给类{}赋值错误!",bean);
          }
        }
      }else if (name.length==2)
      {
        String objectString= (String) m.get(name[0]);
        if (StringUtils.isNotEmpty(objectString))
        {
          try {
       
            Object object=Class.forName(objectString).newInstance();
            if (propertyUtils.isReadable(bean, name[0])&&propertyUtils.isWriteable(bean, name[0]))
            {
              if (PropertyUtils.getProperty(bean, name[0]) == null)
                      {
                //如果没有初始化,求初始
                        PropertyUtils.setProperty(bean, name[0], object);
                      }
                      ClassParsing cp = new ClassParsing();
                      ClassLoader classloader = object.getClass().getClassLoader();
                      if (classloader != null) {
                        URL url = classloader.getResource(StringUtils.replace(object.getClass().getName(), ".", "/") + ".class");
                        File file = FileUtils.toFile(url);
                        if (file != null)
                        {
                          ClassReader cr = new ClassReader(FileUtils.openInputStream(file));
                          cr.accept(cp, 0);
                        }
                      }
              Map<?, ?> ma=cp.getMap();
              String ob= (String) ma.get(name[1]);
              if (propertyUtils.isReadable(bean, string)&&propertyUtils.isWriteable(bean, string)&&ob!=null)
              {
                if (StringUtils.containsIgnoreCase(ob, "java.lang.String"))
                {
                  propertyUtils.setProperty(bean, string, request.getParameter(string));
                }else if (StringUtils.containsIgnoreCase(ob, "java.lang.Integer")
                    ||StringUtils.containsIgnoreCase(ob, "java.lang.Short")
                    ||StringUtils.containsIgnoreCase(ob, "java.lang.Long")
                    ||StringUtils.containsIgnoreCase(ob, "java.lang.Float")
                    ||StringUtils.containsIgnoreCase(ob, "java.lang.Double")
                    ||StringUtils.containsIgnoreCase(ob, "java.lang.Byte")
                    ||StringUtils.containsIgnoreCase(ob, "java.math.BigInteger")
                    ||StringUtils.containsIgnoreCase(ob, "java.math.BigDecimal"))
                {
                  if (NumberUtils.isNumber(request.getParameter(string))) {
                    propertyUtils.setProperty(bean, string, NumberUtils.createNumber(request.getParameter(string)));
                  }
                 
                }else if (StringUtils.containsIgnoreCase(ob, "java.util.Date"))
                {
                  String s = request.getParameter(string);
                  if (StringUtils.isNotEmpty(s))
                  {
                    String[] parsePatterns ={"yyyy/MM/dd","yyyy/MM/dd HH:mm:ss",
                        "yyyy.MM.dd","yyyy.MM.dd HH:mm:ss",
                        "yyyy-MM-dd","yyyy-MM-dd HH:mm:ss",
                        "HH:mm:ss"};
                    Date parsedDate = null;
                    try {
                      parsedDate= DateUtils.parseDate(s,
                          parsePatterns);
                    } catch (ParseException e ) {
                      log.error("{}不是正确的时间格式",s);
                    }
                    propertyUtils.setProperty(bean, string, parsedDate);
                 
                }
              }
             
             
View Full Code Here


  public static void copyProperties(Object dest, Object orig){
    try {
      if (orig != null && dest != null){
        BeanUtils.copyProperties(dest, orig);
       
        PropertyUtils putils = new PropertyUtils();
              PropertyDescriptor origDescriptors[] = putils.getPropertyDescriptors(orig);

              for (int i = 0; i < origDescriptors.length; i++) {
                  String name = origDescriptors[i].getName();
                  if ("class".equals(name)) {
                      continue; // No point in trying to set an object's class
                  }
                 
                  Class propertyType = origDescriptors[i].getPropertyType();
          if (! Boolean.class.equals(propertyType) && ! (boolean.class.equals(propertyType) ))
                    continue;
                 
                  if (!putils.isReadable(orig, name)){ //because of bad convention
                    Method m = orig.getClass().getMethod("is" + name.substring(0,1).toUpperCase() + name.substring(1, null);
                    Object value = m.invoke(orig, null);
                   
                    if (m!=null && putils.isWriteable(dest, name)){
                      BeanUtilsBean.getInstance().copyProperty(dest, name, value);
                    }
                  }
                 
              }             
View Full Code Here

  public static void copyProperties(Object dest, Object orig){
    try {
      if (orig != null && dest != null){
        BeanUtils.copyProperties(dest, orig);
       
        PropertyUtils putils = new PropertyUtils();
              PropertyDescriptor origDescriptors[] = putils.getPropertyDescriptors(orig);

              for (int i = 0; i < origDescriptors.length; i++) {
                  String name = origDescriptors[i].getName();
                  if ("class".equals(name)) {
                      continue; // No point in trying to set an object's class
                  }
                 
                  Class propertyType = origDescriptors[i].getPropertyType();
          if (! Boolean.class.equals(propertyType) && ! (boolean.class.equals(propertyType) ))
                    continue;
                 
                  if (!putils.isReadable(orig, name)){ //because of bad convention
                    Method m = orig.getClass().getMethod("is" + name.substring(0,1).toUpperCase() + name.substring(1, null);
                    Object value = m.invoke(orig, null);
                   
                    if (m!=null && putils.isWriteable(dest, name)){
                      BeanUtilsBean.getInstance().copyProperty(dest, name, value);
                    }
                  }
                 
              }             
View Full Code Here

TOP

Related Classes of org.apache.commons.beanutils.PropertyUtils

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.