/**
  * Reads a Number
  *
  * @return Number Number
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
 @Override
 public Number readNumber(Type target) {
   log.debug("readNumber - target: {}", target);
   Number v;
   if (currentDataType == AMF3.TYPE_NUMBER) {
     v = buf.getDouble();
   } else {
     // we are decoding an int
     v = readAMF3Integer();
   }
   log.debug("readNumber - value: {}", v);
   if (target instanceof Class && Number.class.isAssignableFrom((Class<?>) target)) {
     Class cls = (Class) target;
     if (!cls.isAssignableFrom(v.getClass())) {
       String value = v.toString();
       log.debug("readNumber - value class: {} str: {}", v.getClass(), value);
       if (value.indexOf(".") > 0) {
         if (Float.class == v.getClass()) {
           v = (Number) convertUtilsBean.convert(value, Float.class);
         } else {
           v = (Number) convertUtilsBean.convert(value, Double.class);
         }
       } else {
         v = (Number) convertUtilsBean.convert(value, cls);
       }
     }
   }
   return v;
 }
Пример #2
0
 private static ConvertUtilsBean createConverUtils() {
   DateConverter dateConverter = new DateConverter();
   dateConverter.setPattern("yyyy-MM-dd");
   ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
   convertUtilsBean.register(dateConverter, java.util.Date.class);
   convertUtilsBean.register(dateConverter, Timestamp.class);
   convertUtilsBean.register(dateConverter, java.sql.Date.class);
   return convertUtilsBean;
 }
Пример #3
0
  private static synchronized ConvertUtilsBean getConvertUtils() {
    if (convertUtils == null) {
      convertUtils = new ConvertUtilsBean();
      convertUtils.register(new DateConverter(), Date.class);
      convertUtils.register(new StringConverterAdapter(), String.class);
    }

    return convertUtils;
  }
Пример #4
0
 /**
  * 获取参数
  *
  * @param name 名称
  * @param type 类型
  * @param params 参数
  * @return 参数,若不存在则返回null
  */
 public static <T> T getParameter(String name, Class<T> type, Map<String, TemplateModel> params)
     throws TemplateModelException {
   Assert.hasText(name);
   Assert.notNull(type);
   Assert.notNull(params);
   TemplateModel templateModel = params.get(name);
   if (templateModel == null) {
     return null;
   }
   Object value = DeepUnwrap.unwrap(templateModel);
   return (T) convertUtils.convert(value, type);
 }
Пример #5
0
  static {
    ConvertUtilsBean convertUtilsBean =
        new ConvertUtilsBean() {
          @Override
          public String convert(Object value) {
            if (value != null) {
              Class<?> type = value.getClass();
              if (type.isEnum() && super.lookup(type) == null) {
                super.register(new EnumConverter(type), type);
              } else if (type.isArray() && type.getComponentType().isEnum()) {
                if (super.lookup(type) == null) {
                  ArrayConverter arrayConverter =
                      new ArrayConverter(type, new EnumConverter(type.getComponentType()), 0);
                  arrayConverter.setOnlyFirstToString(false);
                  super.register(arrayConverter, type);
                }
                Converter converter = super.lookup(type);
                return ((String) converter.convert(String.class, value));
              }
            }
            return super.convert(value);
          }

          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(String value, Class clazz) {
            if (clazz.isEnum() && super.lookup(clazz) == null) {
              super.register(new EnumConverter(clazz), clazz);
            }
            return super.convert(value, clazz);
          }

          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(String[] values, Class clazz) {
            if (clazz.isArray()
                && clazz.getComponentType().isEnum()
                && super.lookup(clazz.getComponentType()) == null) {
              super.register(new EnumConverter(clazz.getComponentType()), clazz.getComponentType());
            }
            return super.convert(values, clazz);
          }

          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Object value, Class targetType) {
            if (super.lookup(targetType) == null) {
              if (targetType.isEnum()) {
                super.register(new EnumConverter(targetType), targetType);
              } else if (targetType.isArray() && targetType.getComponentType().isEnum()) {
                ArrayConverter arrayConverter =
                    new ArrayConverter(
                        targetType, new EnumConverter(targetType.getComponentType()), 0);
                arrayConverter.setOnlyFirstToString(false);
                super.register(arrayConverter, targetType);
              }
            }
            return super.convert(value, targetType);
          }
        };

    DateConverter dateConverter = new DateConverter();
    dateConverter.setPatterns(CommonAttributes.DATE_PATTERNS);
    convertUtilsBean.register(dateConverter, Date.class);
    beanUtils = new BeanUtilsBean(convertUtilsBean);
  }
Пример #6
0
  public static void copyModeltoModel(Object src, Object des) {
    PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(des); // 得到to对象的字段属性集合
    for (int i = 0; i < pds.length; i++) {
      try {
        // 得到to对象的属性的类型
        String toClassName = ClassUtils.getShortClassName(pds[i].getPropertyType());
        // 根据此字段名去from对象查找 如果找不到则跳过 继续下一个to字段的赋值
        PropertyDescriptor descriptor = null;
        try {
          descriptor = PropertyUtils.getPropertyDescriptor(src, pds[i].getDisplayName());
          if (descriptor == null) {
            continue; // Skip this property setter
          }
        } catch (NoSuchMethodException e) {
          continue; // Skip this property setter
        }
        // 得到form对象该字段的类型
        String fromClassName = ClassUtils.getShortClassName(descriptor.getPropertyType());

        if ("Class".equals(toClassName)) {
          continue;
        } else {
          // 得到form字段该字段的值 如果有值则赋值 否则继续下一个
          // System.out.println("\n\n\n pds[i].getDisplayName()=" +
          // pds[i].getDisplayName() + "\n\n\n");
          Object o = PropertyUtils.getProperty(src, pds[i].getDisplayName());
          if (o != null) {
            if (toClassName.equals("Date") && fromClassName.equals("String")) { // 如果form该字段是字符串
              // o = Helper.switchDate((String) o);
              // BeanUtils.setProperty(des,
              // pds[i].getDisplayName(), o);
              ConvertUtilsBean convertUtils = new ConvertUtilsBean();
              convertUtils.register(new DateConverter(), Date.class);
              BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils, new PropertyUtilsBean());
              o = Helper.switchDate((String) o);
              beanUtils.setProperty(des, pds[i].getDisplayName(), o);
            } else if (toClassName.equals("String") && fromClassName.equals("Date")) {
              o = DateUtil.toStringByFormat((Date) o, "yyyy-MM-dd HH:mm:ss");
              if (org.apache.commons.lang.StringUtils.contains((String) o, "00:00:00")) {
                o =
                    org.apache.commons.lang.StringUtils.substringBeforeLast((String) o, "00:00:00")
                        .trim();
              }
              org.apache.commons.beanutils.BeanUtils.setProperty(des, pds[i].getDisplayName(), o);
            } else if (toClassName.equals("Date") && fromClassName.equals("Date")) {
              ConvertUtilsBean convertUtils = new ConvertUtilsBean();
              convertUtils.register(new DateConverter(), Date.class);
              BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils, new PropertyUtilsBean());
              beanUtils.setProperty(des, pds[i].getDisplayName(), o);
              // BeanUtils.setProperty(des,
              // pds[i].getDisplayName(), o);
            } else {
              org.apache.commons.beanutils.BeanUtils.setProperty(des, pds[i].getDisplayName(), o);
            }
            // org.apache.commons.beanutils.BeanUtils.setProperty(des,pds[i].getDisplayName(),o);
          }
        }
      } catch (Exception ex) {
        // System.out.println("赋值失败:" + ex);
      }
    }
  }
Пример #7
0
  static {
    /** Configure BeanUtilsBean to use our converters and resolver. */
    ConvertUtilsBean convertUtils = new ConvertUtilsBean();

    // Register bean type converters
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof StringType) {
              return AwayState.forValue(value.toString());
            } else {
              return null;
            }
          }
        },
        AwayState.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof StringType) {
              return HvacMode.forValue(value.toString());
            } else {
              return null;
            }
          }
        },
        HvacMode.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof StringType) {
              return BatteryHealth.forValue(value.toString());
            } else {
              return null;
            }
          }
        },
        BatteryHealth.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof StringType) {
              return AlarmState.forValue(value.toString());
            } else {
              return null;
            }
          }
        },
        AlarmState.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof StringType) {
              return ColorState.forValue(value.toString());
            } else {
              return null;
            }
          }
        },
        ColorState.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof DecimalType) {
              return ((DecimalType) value).intValue();
            } else {
              return null;
            }
          }
        },
        Integer.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            if (value instanceof OnOffType) {
              return ((OnOffType) value) == OnOffType.ON;
            } else {
              return null;
            }
          }
        },
        Boolean.class);
    convertUtils.register(
        new Converter() {
          @SuppressWarnings("rawtypes")
          @Override
          public Object convert(Class type, Object value) {
            return value.toString();
          }
        },
        String.class);

    propertyUtils = new PropertyUtilsBean();
    propertyUtils.setResolver(new DataModelPropertyResolver());
    beanUtils = new BeanUtilsBean(convertUtils, propertyUtils);
  }
Пример #8
0
 public static void registerConverters(ConvertUtilsBean convertUtils, String[] datePatterns) {
   convertUtils.register(new StringConverter(), String.class);
   // date
   convertUtils.register(
       ConvertRegisterHelper.setPatterns(new DateConverter(null), datePatterns),
       java.util.Date.class);
   convertUtils.register(
       ConvertRegisterHelper.setPatterns(new SqlDateConverter(null), datePatterns),
       java.sql.Date.class);
   convertUtils.register(
       ConvertRegisterHelper.setPatterns(new SqlTimeConverter(null), datePatterns), Time.class);
   convertUtils.register(
       ConvertRegisterHelper.setPatterns(new SqlTimestampConverter(null), datePatterns),
       Timestamp.class);
   // number
   convertUtils.register(new BooleanConverter(null), Boolean.class);
   convertUtils.register(new ShortConverter(null), Short.class);
   convertUtils.register(new IntegerConverter(null), Integer.class);
   convertUtils.register(new LongConverter(null), Long.class);
   convertUtils.register(new FloatConverter(null), Float.class);
   convertUtils.register(new DoubleConverter(null), Double.class);
   convertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
   convertUtils.register(new BigIntegerConverter(null), BigInteger.class);
 }
Пример #9
0
 public static <S, D> D convertType(Object obj, Class<S> srcClass, Class<D> destClass) {
   Converter converter = convertUtils.lookup(srcClass, destClass);
   return converter.convert(destClass, obj);
 }
Пример #10
0
  /**
   * Creates a BeanUtilsBean that is configured to use type converters that throw a
   * ConversionException instead of using the default value when something goes wrong.
   *
   * @return a configured BeanUtilsBean
   */
  private static BeanUtilsBean createBeanUtilsBean() {
    final ConvertUtilsBean cub = new ConvertUtilsBean();
    // TODO: is there a smarter way to tell beanutils not to use defaults?
    cub.register(new BooleanConverter(), Boolean.TYPE);
    cub.register(new BooleanConverter(), Boolean.class);
    cub.register(new ArrayConverter(boolean[].class, new BooleanConverter()), boolean[].class);
    cub.register(new ByteConverter(), Byte.TYPE);
    cub.register(new ByteConverter(), Byte.class);
    cub.register(new ArrayConverter(byte[].class, new ByteConverter()), byte[].class);
    cub.register(new CharacterConverter(), Character.TYPE);
    cub.register(new CharacterConverter(), Character.class);
    cub.register(new ArrayConverter(char[].class, new CharacterConverter()), char[].class);
    cub.register(new DoubleConverter(), Double.TYPE);
    cub.register(new DoubleConverter(), Double.class);
    cub.register(new ArrayConverter(double[].class, new DoubleConverter()), double[].class);
    cub.register(new FloatConverter(), Float.TYPE);
    cub.register(new FloatConverter(), Float.class);
    cub.register(new ArrayConverter(float[].class, new FloatConverter()), float[].class);
    cub.register(new IntegerConverter(), Integer.TYPE);
    cub.register(new IntegerConverter(), Integer.class);
    cub.register(new ArrayConverter(int[].class, new IntegerConverter()), int[].class);
    cub.register(new LongConverter(), Long.TYPE);
    cub.register(new LongConverter(), Long.class);
    cub.register(new ArrayConverter(long[].class, new LongConverter()), long[].class);
    cub.register(new ShortConverter(), Short.TYPE);
    cub.register(new ShortConverter(), Short.class);
    cub.register(new ArrayConverter(short[].class, new ShortConverter()), short[].class);
    cub.register(new RelaxedStringArrayConverter(), String[].class);

    // BigDecimal, BigInteger, Class, Date, String, Time, TimeStamp
    // do not use defaults in the default configuration of ConvertUtilsBean

    return new BeanUtilsBean(cub, new PropertyUtilsBean());
  }