コード例 #1
0
ファイル: JSONEncoder.java プロジェクト: rmichela/MCStats2
  private static Encoder getEncoder(final Class<?> type) throws IntrospectionException {
    if (CharSequence.class.isAssignableFrom(type)
        || Character.class.equals(type)
        || Character.TYPE.equals(type)
        || UUID.class.equals(type)) {

      return StringEncoder.INSTANCE;
    } else if (Number.class.isAssignableFrom(type)
        || Byte.TYPE.equals(type)
        || Short.TYPE.equals(type)
        || Integer.TYPE.equals(type)
        || Long.TYPE.equals(type)
        || Boolean.TYPE.equals(type)
        || Boolean.class.equals(type)) {

      return PlainFormEncoder.INSTANCE;
    } else if (type.isArray()) {
      return new ArrayEncoder(getEncoder(type.getComponentType()));
    } else if (Iterable.class.isAssignableFrom(type)) {
      return IterableEncoder.INSTANCE;
    } else if (Map.class.isAssignableFrom(type)) {
      return MapEncoder.INSTANCE;
    } else if (Date.class.isAssignableFrom(type)) {
      return DateEncoder.INSTANCE;
    } else {
      ObjectEncoder encoder = encoderCache.get(type);

      if (encoder == null) {
        encoder = new ObjectEncoder(type);
        encoderCache.put(type, encoder);
      }

      return encoder;
    }
  }
コード例 #2
0
  /** Write a class using safe encoding to workaround java 1.3 serialization bug. */
  private void writeAnyClass(Class clazz, ObjectOutputStream out) throws IOException {
    // safely write out any class
    int primitiveType = 0;
    if (Boolean.TYPE.equals(clazz)) {
      primitiveType = BOOLEAN_TYPE;
    } else if (Byte.TYPE.equals(clazz)) {
      primitiveType = BYTE_TYPE;
    } else if (Character.TYPE.equals(clazz)) {
      primitiveType = CHAR_TYPE;
    } else if (Double.TYPE.equals(clazz)) {
      primitiveType = DOUBLE_TYPE;
    } else if (Float.TYPE.equals(clazz)) {
      primitiveType = FLOAT_TYPE;
    } else if (Integer.TYPE.equals(clazz)) {
      primitiveType = INT_TYPE;
    } else if (Long.TYPE.equals(clazz)) {
      primitiveType = LONG_TYPE;
    } else if (Short.TYPE.equals(clazz)) {
      primitiveType = SHORT_TYPE;
    }

    if (primitiveType == 0) {
      // then it's not a primitive type
      out.writeBoolean(false);
      out.writeObject(clazz);
    } else {
      // we'll write out a constant instead
      out.writeBoolean(true);
      out.writeInt(primitiveType);
    }
  }
コード例 #3
0
ファイル: TypeConverter.java プロジェクト: jabelardo/zk-dl
 /**
  * Tries to convert a string value to a data type given as a parameter. There are few cases, that
  * can occure.
  *
  * <ol>
  *   <li>Given value is null or an empty string. In such case the null is also returned.
  *   <li>Defined class is a java primitive like int, double etc. For these instances the object
  *       representation is returned. It means that for int it returns Integer, for double Double
  *       etc.
  *   <li>Value is well-known object primitive, like Integer, Date etc. In these cases the string
  *       is parsed into given object and returned.
  *   <li>Defined class is complex. In such cases the original string value is returned because
  *       there is not known converter.
  * </ol>
  *
  * <p>Note: When the given class is inherited from some java primitive then the returned object is
  * that primitive type. For example this can occure for {@link java.sql.Date} which inherits from
  * {@link java.util.Date}
  *
  * @param <T> desired type to be converted to
  * @param value string representation of converted value
  * @param type target class of the value
  * @return converted value if the conversion is defined
  * @throws ParseException this occures when the date is in bad format. The format can be redefined
  *     in {@link #dateFormat}.
  * @throws NumberFormatException conversion of string to number has failed. It occures when the
  *     input string is not a number but the target class is.
  */
 public static <T> Object tryToConvertTo(final String value, final Class<T> type)
     throws ParseException, NumberFormatException {
   if (value == null || "".equals(value)) {
     return null;
   }
   if (java.util.Date.class.isAssignableFrom(type)) {
     return (java.util.Date)
         (new java.text.SimpleDateFormat(dateFormat, java.util.Locale.getDefault()).parse(value));
   } else if (String.class.isAssignableFrom(type)) {
     return type.cast(value);
   } else if (Integer.class.isAssignableFrom(type) || Integer.TYPE.equals(type)) {
     return Integer.valueOf(value);
   } else if (Double.class.isAssignableFrom(type) || Double.TYPE.equals(type)) {
     return Double.valueOf(value);
   } else if (Long.class.isAssignableFrom(type) || Long.TYPE.equals(type)) {
     return Long.valueOf(value);
   } else if (Boolean.class.isAssignableFrom(type) || Boolean.TYPE.equals(type)) {
     return (BooleanHelper.isTrue(value) ? Boolean.TRUE : (Boolean.valueOf(value)));
   } else if (Byte.class.isAssignableFrom(type) || Byte.TYPE.equals(type)) {
     return Byte.valueOf(value);
   } else if (type.isEnum()) {
     for (T enumValue : type.getEnumConstants()) {
       if (enumValue.toString().equals(value)) {
         return enumValue;
       }
     }
     return null;
   } else {
     return value;
   }
 }
コード例 #4
0
ファイル: ObjectCreator.java プロジェクト: dewafer/toolbox
 protected Class<?> autoBoxing(Class<?> c) {
   if (!c.isPrimitive()) {
     return c;
   }
   // 8 elements
   // Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE,
   // Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE
   if (Boolean.TYPE.equals(c)) {
     return Boolean.class;
   } else if (Character.TYPE.equals(c)) {
     return Character.class;
   } else if (Byte.TYPE.equals(c)) {
     return Byte.class;
   } else if (Short.TYPE.equals(c)) {
     return Short.class;
   } else if (Integer.TYPE.equals(c)) {
     return Integer.class;
   } else if (Long.TYPE.equals(c)) {
     return Long.class;
   } else if (Float.TYPE.equals(c)) {
     return Float.class;
   } else if (Double.TYPE.equals(c)) {
     return Double.class;
   } else {
     return c;
   }
 }
コード例 #5
0
 /**
  * Retrieves the SNMP data type corresponding to an object instance.
  *
  * @param object
  * @return a class
  */
 private static Class<?> retrieveDataTypeFromObject(final Object object) {
   if (object instanceof String || String.class.equals(object)) {
     return OctetString.class;
   } else if (Number.class.isInstance(object)) {
     return Integer32.class;
   } else if (object instanceof Class) {
     Class<?> clz = (Class<?>) object;
     if (clz.isPrimitive()) {
       if (Integer.TYPE.equals(clz)
           || Long.TYPE.equals(clz)
           || Byte.TYPE.equals(clz)
           || Short.TYPE.equals(clz)) {
         return Integer32.class;
       }
     } else {
       if (Integer.class.equals(clz)
           || Long.class.equals(clz)
           || Byte.class.equals(clz)
           || Short.class.equals(clz)) {
         return Integer32.class;
       }
     }
   }
   return null;
 }
コード例 #6
0
ファイル: ClassUtils.java プロジェクト: PAMSE/APGBench
 /**
  * Checks if one <code>Class</code> can be assigned to a variable of another <code>Class</code>.
  *
  * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this method takes into
  * account widenings of primitive classes and <code>null</code>s.
  *
  * <p>Primitive widenings allow an int to be assigned to a long, float or double. This method
  * returns the correct result for these cases.
  *
  * <p><code>Null</code> may be assigned to any reference type. This method will return <code>true
  * </code> if <code>null</code> is passed in and the toClass is non-primitive.
  *
  * <p>Specifically, this method tests whether the type represented by the specified <code>Class
  * </code> parameter can be converted to the type represented by this <code>Class</code> object
  * via an identity conversion widening primitive or widening reference conversion. See <em><a
  * href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></em>, sections
  * 5.1.1, 5.1.2 and 5.1.4 for details.
  *
  * @param cls the Class to check, may be null
  * @param toClass the Class to try to assign into, returns false if null
  * @return <code>true</code> if assignment possible
  */
 public static boolean isAssignable(Class cls, Class toClass) {
   if (toClass == null) {
     return false;
   }
   // have to check for null, as isAssignableFrom doesn't
   if (cls == null) {
     return !(toClass.isPrimitive());
   }
   if (cls.equals(toClass)) {
     return true;
   }
   if (cls.isPrimitive()) {
     if (toClass.isPrimitive() == false) {
       return false;
     }
     if (Integer.TYPE.equals(cls)) {
       return Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Long.TYPE.equals(cls)) {
       return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
     }
     if (Boolean.TYPE.equals(cls)) {
       return false;
     }
     if (Double.TYPE.equals(cls)) {
       return false;
     }
     if (Float.TYPE.equals(cls)) {
       return Double.TYPE.equals(toClass);
     }
     if (Character.TYPE.equals(cls)) {
       return Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Short.TYPE.equals(cls)) {
       return Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Byte.TYPE.equals(cls)) {
       return Short.TYPE.equals(toClass)
           || Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     // should never get here
     return false;
   }
   return toClass.isAssignableFrom(cls);
 }
コード例 #7
0
  /**
   * Converts the specified value to the target class. If the class is a primitive type
   * (Integer.TYPE, Boolean.TYPE, etc) the value returned will use the wrapper type (Integer.class,
   * Boolean.class, etc).
   *
   * @param cls the target class of the converted value
   * @param value the value to convert
   * @param params optional parameters used for the conversion
   * @return the converted value
   * @throws ConversionException if the value is not compatible with the requested type
   * @since 1.5
   */
  static Object to(Class<?> cls, Object value, Object[] params) throws ConversionException {
    if (cls.isInstance(value)) {
      return value; // no conversion needed
    }

    if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
      return toBoolean(value);
    } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) {
      return toCharacter(value);
    } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
      if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
        return toInteger(value);
      } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
        return toLong(value);
      } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
        return toByte(value);
      } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
        return toShort(value);
      } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
        return toFloat(value);
      } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
        return toDouble(value);
      } else if (BigInteger.class.equals(cls)) {
        return toBigInteger(value);
      } else if (BigDecimal.class.equals(cls)) {
        return toBigDecimal(value);
      }
    } else if (Date.class.equals(cls)) {
      return toDate(value, (String) params[0]);
    } else if (Calendar.class.equals(cls)) {
      return toCalendar(value, (String) params[0]);
    } else if (URL.class.equals(cls)) {
      return toURL(value);
    } else if (Locale.class.equals(cls)) {
      return toLocale(value);
    } else if (isEnum(cls)) {
      return convertToEnum(cls, value);
    } else if (Color.class.equals(cls)) {
      return toColor(value);
    } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
      return toInternetAddress(value);
    } else if (InetAddress.class.isAssignableFrom(cls)) {
      return toInetAddress(value);
    }

    throw new ConversionException(
        "The value '"
            + value
            + "' ("
            + value.getClass()
            + ")"
            + " can't be converted to a "
            + cls.getName()
            + " object");
  }
コード例 #8
0
  /**
   * Is the given {@code object} a primitive type or wrapper for a primitive type?
   *
   * @param object The object to check for primitive-ness.
   * @return {@code true} if {@code object} is a primitive type or wrapper for a primitive type,
   *     {@code false} otherwise.
   */
  static boolean isPrimitive(Object object) {
    if (object == null) return false;

    Class<?> type = object.getClass();

    return (object instanceof Integer || Integer.TYPE.equals(type))
        || (object instanceof Boolean || Boolean.TYPE.equals(type))
        || (object instanceof Long || Long.TYPE.equals(type))
        || (object instanceof Double || Double.TYPE.equals(type))
        || (object instanceof Float || Float.TYPE.equals(type))
        || (object instanceof Byte || Byte.TYPE.equals(type))
        || (object instanceof Short || Short.TYPE.equals(type))
        || (object instanceof Character || Character.TYPE.equals(type));
  }
コード例 #9
0
ファイル: Reflect.java プロジェクト: mnar53/xlloop
 public static void setField(Field f, Object o, String t)
     throws NumberFormatException, IllegalArgumentException, IllegalAccessException {
   Class type = f.getType();
   if (String.class.equals(type)) {
     f.set(o, t);
   } else if (Double.class.equals(type)) {
     f.set(o, Double.valueOf(t));
   } else if (Float.class.equals(type)) {
     f.set(o, Float.valueOf(t));
   } else if (Long.class.equals(type)) {
     f.set(o, Long.valueOf(t));
   } else if (Integer.class.equals(type)) {
     f.set(o, Integer.valueOf(t));
   } else if (Character.class.equals(type)) {
     f.set(o, Character.valueOf(t.charAt(0)));
   } else if (Short.class.equals(type)) {
     f.set(o, Short.valueOf(t));
   } else if (Byte.class.equals(type)) {
     f.set(o, Byte.valueOf(t));
   } else if (Boolean.class.equals(type)) {
     f.set(o, Boolean.valueOf(t));
   } else if (Double.TYPE.equals(type)) {
     f.setDouble(o, Double.parseDouble(t));
   } else if (Float.TYPE.equals(type)) {
     f.setFloat(o, Float.parseFloat(t));
   } else if (Long.TYPE.equals(type)) {
     f.setLong(o, Long.parseLong(t));
   } else if (Integer.TYPE.equals(type)) {
     f.setInt(o, Integer.parseInt(t));
   } else if (Character.TYPE.equals(type)) {
     f.setChar(o, t.charAt(0));
   } else if (Short.TYPE.equals(type)) {
     f.setShort(o, Short.parseShort(t));
   } else if (Byte.TYPE.equals(type)) {
     f.setByte(o, Byte.parseByte(t));
   } else if (Boolean.TYPE.equals(type)) {
     f.setBoolean(o, Boolean.parseBoolean(t));
   }
 }
コード例 #10
0
 /** @see https://github.com/wordnik/swagger-core/wiki/datatypes */
 protected static SwaggerDataType convertToSwaggerType(SwaggerModel models, Class<?> type) {
   if (Void.TYPE.equals(type)) {
     return new SwaggerDataType("void");
   } else if (Integer.TYPE.equals(type) || Integer.class.isAssignableFrom(type)) {
     return new SwaggerDataType("integer", "int32");
   } else if (Long.TYPE.equals(type) || Long.class.isAssignableFrom(type)) {
     return new SwaggerDataType("integer", "int64");
   } else if (Float.TYPE.equals(type) || Float.class.isAssignableFrom(type)) {
     return new SwaggerDataType("number", "float");
   } else if (Double.TYPE.equals(type) || Double.class.isAssignableFrom(type)) {
     return new SwaggerDataType("number", "double");
   } else if (Byte.TYPE.equals(type) || Byte.class.isAssignableFrom(type)) {
     return new SwaggerDataType("string", "byte");
   } else if (Boolean.TYPE.equals(type) || Boolean.class.isAssignableFrom(type)) {
     return new SwaggerDataType("boolean");
   } else if (Number.class.isAssignableFrom(type)) {
     return new SwaggerDataType("number");
   } else if (String.class.equals(type)) {
     return new SwaggerDataType("string");
   } else if (Date.class.equals(type)) {
     return new SwaggerDataType("string", "date-time");
   } else if (type.isEnum()) {
     return new SwaggerDataType("string");
   } else if (type.isArray() || Collection.class.isAssignableFrom(type)) {
     return new SwaggerDataType("array");
   } else {
     // it's a custom type, we need to create a model for it (if it does not already exist)
     String typeName = type.getName();
     if (!models.containsKey(typeName)) {
       // Reserve a spot for this type, avoids circular references to cause a StackOverflow, see
       // AMDATUWEB-10...
       models.put(typeName, null);
       // Overwrite the item with the actual model definition...
       models.put(typeName, convertToSwaggerModel(models, type));
     }
     return new SwaggerDataType(type.getName());
   }
 }
コード例 #11
0
ファイル: ClassUtils.java プロジェクト: PAMSE/APGBench
 /**
  * Checks if one <code>Class</code> can be assigned to a variable of another <code>Class</code>.
  *
  * <p>Unlike the {@link Class#isAssignableFrom(java.lang.Class)} method, this method takes into
  * account widenings of primitive classes and <code>null</code>s.
  *
  * <p>Primitive widenings allow an int to be assigned to a long, float or double. This method
  * returns the correct result for these cases.
  *
  * <p><code>Null</code> may be assigned to any reference type. This method will return <code>true
  * </code> if <code>null</code> is passed in and the toClass is non-primitive.
  *
  * <p>Specifically, this method tests whether the type represented by the specified <code>Class
  * </code> parameter can be converted to the type represented by this <code>Class</code> object
  * via an identity conversion widening primitive or widening reference conversion. See <em><a
  * href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></em>, sections
  * 5.1.1, 5.1.2 and 5.1.4 for details.
  *
  * @param cls the Class to check, may be null
  * @param toClass the Class to try to assign into, returns false if null
  * @param autoboxing whether to use implicit autoboxing/unboxing between primitives and wrappers
  * @return <code>true</code> if assignment possible
  */
 public static boolean isAssignable(Class<?> cls, Class<?> toClass, boolean autoboxing) {
   if (toClass == null) {
     return false;
   }
   // have to check for null, as isAssignableFrom doesn't
   if (cls == null) {
     return !(toClass.isPrimitive());
   }
   // autoboxing:
   if (autoboxing) {
     if (cls.isPrimitive() && !toClass.isPrimitive()) {
       cls = primitiveToWrapper(cls);
       if (cls == null) {
         return false;
       }
     }
     if (toClass.isPrimitive() && !cls.isPrimitive()) {
       cls = wrapperToPrimitive(cls);
       if (cls == null) {
         return false;
       }
     }
   }
   if (cls.equals(toClass)) {
     return true;
   }
   if (cls.isPrimitive()) {
     if (toClass.isPrimitive() == false) {
       return false;
     }
     if (Integer.TYPE.equals(cls)) {
       return Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Long.TYPE.equals(cls)) {
       return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
     }
     if (Boolean.TYPE.equals(cls)) {
       return false;
     }
     if (Double.TYPE.equals(cls)) {
       return false;
     }
     if (Float.TYPE.equals(cls)) {
       return Double.TYPE.equals(toClass);
     }
     if (Character.TYPE.equals(cls)) {
       return Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Short.TYPE.equals(cls)) {
       return Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     if (Byte.TYPE.equals(cls)) {
       return Short.TYPE.equals(toClass)
           || Integer.TYPE.equals(toClass)
           || Long.TYPE.equals(toClass)
           || Float.TYPE.equals(toClass)
           || Double.TYPE.equals(toClass);
     }
     // should never get here
     return false;
   }
   return toClass.isAssignableFrom(cls);
 }
コード例 #12
0
 /** @generated */
 protected Object getValidNewValue(EAttribute feature, Object value) {
   EClassifier type = feature.getEType();
   if (type instanceof EDataType) {
     Class iClass = type.getInstanceClass();
     if (Boolean.TYPE.equals(iClass)) {
       if (value instanceof Boolean) {
         // ok
       } else if (value instanceof String) {
         value = Boolean.valueOf((String) value);
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Character.TYPE.equals(iClass)) {
       if (value instanceof Character) {
         // ok
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           value = new Character(s.charAt(0));
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Byte.TYPE.equals(iClass)) {
       if (value instanceof Byte) {
         // ok
       } else if (value instanceof Number) {
         value = new Byte(((Number) value).byteValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Byte.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Short.TYPE.equals(iClass)) {
       if (value instanceof Short) {
         // ok
       } else if (value instanceof Number) {
         value = new Short(((Number) value).shortValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Short.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Integer.TYPE.equals(iClass)) {
       if (value instanceof Integer) {
         // ok
       } else if (value instanceof Number) {
         value = new Integer(((Number) value).intValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Integer.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Long.TYPE.equals(iClass)) {
       if (value instanceof Long) {
         // ok
       } else if (value instanceof Number) {
         value = new Long(((Number) value).longValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Long.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Float.TYPE.equals(iClass)) {
       if (value instanceof Float) {
         // ok
       } else if (value instanceof Number) {
         value = new Float(((Number) value).floatValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Float.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (Double.TYPE.equals(iClass)) {
       if (value instanceof Double) {
         // ok
       } else if (value instanceof Number) {
         value = new Double(((Number) value).doubleValue());
       } else if (value instanceof String) {
         String s = (String) value;
         if (s.length() == 0) {
           value = null;
         } else {
           try {
             value = Double.valueOf(s);
           } catch (NumberFormatException nfe) {
             value =
                 new InvalidValue(
                     NLS.bind(Messages.AbstractParser_WrongStringConversion, iClass.getName()));
           }
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, iClass.getName()));
       }
     } else if (type instanceof EEnum) {
       if (value instanceof String) {
         EEnumLiteral literal = ((EEnum) type).getEEnumLiteralByLiteral((String) value);
         if (literal == null) {
           value = new InvalidValue(NLS.bind(Messages.AbstractParser_UnknownLiteral, value));
         } else {
           value = literal.getInstance();
         }
       } else {
         value =
             new InvalidValue(
                 NLS.bind(Messages.AbstractParser_UnexpectedValueType, String.class.getName()));
       }
     }
   }
   return value;
 }
コード例 #13
0
  @Override
  public void begin(MessagePartInfo part) {
    Class<?> clazz = part.getTypeClass();
    if (clazz == null) {
      return;
    }

    if (Exception.class.isAssignableFrom(clazz)) {
      // exceptions are handled special, make sure we mark it
      part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
    }
    boolean isFromWrapper = part.getMessageInfo().getOperation().isUnwrapped();
    if (isFromWrapper && !Boolean.TRUE.equals(part.getProperty("messagepart.isheader"))) {
      UnwrappedOperationInfo uop = (UnwrappedOperationInfo) part.getMessageInfo().getOperation();
      OperationInfo op = uop.getWrappedOperation();
      MessageInfo inf = null;
      if (uop.getInput() == part.getMessageInfo()) {
        inf = op.getInput();
      } else if (uop.getOutput() == part.getMessageInfo()) {
        inf = op.getOutput();
      }
      if (inf != null && inf.getMessagePart(0).getTypeClass() != null) {
        // if the wrapper has a type class, we don't need to do anything
        // as everything would have been discovered when walking the
        // wrapper type (unless it's a header which wouldn't be in the wrapper)
        return;
      }
    }
    if (isFromWrapper && clazz.isArray() && !Byte.TYPE.equals(clazz.getComponentType())) {
      clazz = clazz.getComponentType();
    }

    Annotation[] a = (Annotation[]) part.getProperty("parameter.annotations");
    checkForAdapter(clazz, a);

    Type genericType = (Type) part.getProperty("generic.type");
    if (genericType != null) {
      boolean isList = Collection.class.isAssignableFrom(clazz);
      if (isFromWrapper) {
        if (genericType instanceof Class && ((Class<?>) genericType).isArray()) {
          Class<?> cl2 = (Class<?>) genericType;
          if (cl2.isArray() && !Byte.TYPE.equals(cl2.getComponentType())) {
            genericType = cl2.getComponentType();
          }
          addType(genericType);
        } else if (!isList) {
          addType(genericType);
        }
      } else {
        addType(genericType, true);
      }

      if (isList && genericType instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) genericType;
        if (pt.getActualTypeArguments().length > 0
            && pt.getActualTypeArguments()[0] instanceof Class) {

          Class<? extends Object> arrayCls =
              Array.newInstance((Class<?>) pt.getActualTypeArguments()[0], 0).getClass();
          clazz = arrayCls;
          part.setTypeClass(clazz);
          if (isFromWrapper) {
            addType(clazz.getComponentType(), true);
          }
        } else if (pt.getActualTypeArguments().length > 0
            && pt.getActualTypeArguments()[0] instanceof GenericArrayType) {
          GenericArrayType gat = (GenericArrayType) pt.getActualTypeArguments()[0];
          gat.getGenericComponentType();
          Class<? extends Object> arrayCls =
              Array.newInstance((Class<?>) gat.getGenericComponentType(), 0).getClass();
          clazz = Array.newInstance(arrayCls, 0).getClass();
          part.setTypeClass(clazz);
          if (isFromWrapper) {
            addType(clazz.getComponentType(), true);
          }
        }
      }
      if (isFromWrapper && isList) {
        clazz = null;
      }
    }
    if (clazz != null) {
      if (!isFromWrapper
          && clazz.getAnnotation(XmlRootElement.class) == null
          && clazz.getAnnotation(XmlType.class) != null
          && StringUtils.isEmpty(clazz.getAnnotation(XmlType.class).name())) {
        Object ref = JAXBClassLoaderUtils.createTypeReference(part.getName(), clazz);
        if (ref != null) {
          typeReferences.add(ref);
        }
      }

      addClass(clazz);
    }
  }