/** * 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; } }
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; } }
/** 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); } }
/** * 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; }
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; } }
/** * 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); }
static { UIBindingsUtils.BOXED2PRIMITIVE.put(Boolean.class.getName(), Boolean.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Character.class.getName(), Character.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Byte.class.getName(), Byte.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Short.class.getName(), Short.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Integer.class.getName(), Integer.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Long.class.getName(), Long.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Float.class.getName(), Float.TYPE.getName()); UIBindingsUtils.BOXED2PRIMITIVE.put(Double.class.getName(), Double.TYPE.getName()); }
/** Tests if Class represents a primitive number or wrapper.<br> */ public static boolean isNumber(Class clazz) { return clazz != null && (Byte.TYPE.isAssignableFrom(clazz) || Short.TYPE.isAssignableFrom(clazz) || Integer.TYPE.isAssignableFrom(clazz) || Long.TYPE.isAssignableFrom(clazz) || Float.TYPE.isAssignableFrom(clazz) || Double.TYPE.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz)); }
/** * 是否数值 * * @param clazz * @return */ public static boolean isNumber(Class<?> clazz) { return (clazz != null) && ((Byte.TYPE.isAssignableFrom(clazz)) || (Short.TYPE.isAssignableFrom(clazz)) || (Integer.TYPE.isAssignableFrom(clazz)) || (Long.TYPE.isAssignableFrom(clazz)) || (Float.TYPE.isAssignableFrom(clazz)) || (Double.TYPE.isAssignableFrom(clazz)) || (Number.class.isAssignableFrom(clazz))); }
static { primitiveTypes.put(Boolean.TYPE.getName(), Boolean.TYPE); primitiveTypes.put(Byte.TYPE.getName(), Byte.TYPE); primitiveTypes.put(Short.TYPE.getName(), Short.TYPE); primitiveTypes.put(Character.TYPE.getName(), Character.TYPE); primitiveTypes.put(Integer.TYPE.getName(), Integer.TYPE); primitiveTypes.put(Long.TYPE.getName(), Long.TYPE); primitiveTypes.put(Float.TYPE.getName(), Float.TYPE); primitiveTypes.put(Double.TYPE.getName(), Double.TYPE); }
/** * 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"); }
static { PRIMITIVE_TYPES.put(Boolean.TYPE.getName(), Boolean.TYPE); PRIMITIVE_TYPES.put(Character.TYPE.getName(), Character.TYPE); PRIMITIVE_TYPES.put(Byte.TYPE.getName(), Byte.TYPE); PRIMITIVE_TYPES.put(Short.TYPE.getName(), Short.TYPE); PRIMITIVE_TYPES.put(Integer.TYPE.getName(), Integer.TYPE); PRIMITIVE_TYPES.put(Long.TYPE.getName(), Long.TYPE); PRIMITIVE_TYPES.put(Float.TYPE.getName(), Float.TYPE); PRIMITIVE_TYPES.put(Double.TYPE.getName(), Double.TYPE); PRIMITIVE_TYPES.put(Void.TYPE.getName(), Void.TYPE); }
/** * 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)); }
public static void addAll(Map map) { IntegerSerializer integerserializer = new IntegerSerializer(); map.put(java / lang / Integer.getName(), integerserializer); map.put(Integer.TYPE.getName(), integerserializer); map.put(java / lang / Long.getName(), LongSerializer.instance); map.put(Long.TYPE.getName(), LongSerializer.instance); map.put(java / lang / Byte.getName(), IntLikeSerializer.instance); map.put(Byte.TYPE.getName(), IntLikeSerializer.instance); map.put(java / lang / Short.getName(), IntLikeSerializer.instance); map.put(Short.TYPE.getName(), IntLikeSerializer.instance); map.put(java / lang / Float.getName(), FloatSerializer.instance); map.put(Float.TYPE.getName(), FloatSerializer.instance); map.put(java / lang / Double.getName(), DoubleSerializer.instance); map.put(Double.TYPE.getName(), DoubleSerializer.instance); }
protected Class loadClass(String className) { Class valueType = String.class; if (null != className && 0 < className.length()) { if (className.equals(Boolean.TYPE.getName())) { valueType = Boolean.TYPE; } else if (className.equals(Byte.TYPE.getName())) { valueType = Byte.TYPE; } else if (className.equals(Double.TYPE.getName())) { valueType = Double.TYPE; } else if (className.equals(Float.TYPE.getName())) { valueType = Float.TYPE; } else if (className.equals(Integer.TYPE.getName())) { valueType = Integer.TYPE; } else if (className.equals(Character.TYPE.getName())) { valueType = Character.TYPE; } else if (className.equals(Short.TYPE.getName())) { valueType = Short.TYPE; } else if (className.equals(Long.TYPE.getName())) { valueType = Long.TYPE; } else { try { valueType = Util.loadClass(className, this); } catch (ClassNotFoundException cnfe) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_NOT_FOUND_ERROR_ID, className, beanInfo.getName()); throw new ManagedBeanPreProcessingException(message, cnfe); } catch (NoClassDefFoundError ncdfe) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_DEPENDENCY_NOT_FOUND_ERROR_ID, className, beanInfo.getName(), ncdfe.getMessage()); throw new ManagedBeanPreProcessingException(message, ncdfe); } } } return valueType; }
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)); } }
/** @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()); } }
public Coercer<Byte> accept(final Class<?> clazz) { if (Byte.class.isAssignableFrom(clazz) || Byte.TYPE.isAssignableFrom(clazz)) { return DefaultCoercibles.BYTE_COERCER; } return null; }
/** * 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); }
/** @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; }
static { try { valueParsers.put( Boolean.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getBoolean", String.class)); valueParsers.put( Boolean.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getBoolean", String.class)); valueParsers.put( Byte.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getByte", String.class)); valueParsers.put( Byte.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getByte", String.class)); valueParsers.put( Character.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getChar", String.class)); valueParsers.put( Character.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getChar", String.class)); valueParsers.put( Short.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getShort", String.class)); valueParsers.put( Short.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getShort", String.class)); valueParsers.put( Integer.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getInt", String.class)); valueParsers.put( Integer.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getInt", String.class)); valueParsers.put( Long.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getLong", String.class)); valueParsers.put( Long.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getLong", String.class)); valueParsers.put( Float.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getFloat", String.class)); valueParsers.put( Float.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getFloat", String.class)); valueParsers.put( Double.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getDouble", String.class)); valueParsers.put( Double.TYPE.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getDouble", String.class)); valueParsers.put( Locale.class.getCanonicalName(), SimpleAnnotationBasedHandler.class.getDeclaredMethod("getLocale", String.class)); } catch (Exception e) { log.error("Error setting up value parsers", e); throw new RuntimeException("Error setting up value parsers", e); } }
private boolean compare_PrimitiveArray( int operation, Class type, Object primarray, Object value2) { if (Integer.TYPE.isAssignableFrom(type)) { int[] array = (int[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Integer(operation, array[i], value2)) { return true; } } return false; } if (Long.TYPE.isAssignableFrom(type)) { long[] array = (long[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Long(operation, array[i], value2)) { return true; } } return false; } if (Byte.TYPE.isAssignableFrom(type)) { byte[] array = (byte[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Byte(operation, array[i], value2)) { return true; } } return false; } if (Short.TYPE.isAssignableFrom(type)) { short[] array = (short[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Short(operation, array[i], value2)) { return true; } } return false; } if (Character.TYPE.isAssignableFrom(type)) { char[] array = (char[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Character(operation, array[i], value2)) { return true; } } return false; } if (Float.TYPE.isAssignableFrom(type)) { float[] array = (float[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Float(operation, array[i], value2)) { return true; } } return false; } if (Double.TYPE.isAssignableFrom(type)) { double[] array = (double[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Double(operation, array[i], value2)) { return true; } } return false; } if (Boolean.TYPE.isAssignableFrom(type)) { boolean[] array = (boolean[]) primarray; for (int i = 0, size = array.length; i < size; i++) { if (compare_Boolean(operation, array[i], value2)) { return true; } } return false; } return false; }
@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); } }
private static Map<String, Class<?>> createCommonJavaClassesMap() { Map<String, Class<?>> m = new HashMap<>(); m.put("java.lang.String", java.lang.String.class); m.put("String", java.lang.String.class); m.put("java.lang.CharSequence", java.lang.CharSequence.class); m.put("CharSequence", java.lang.CharSequence.class); m.put("java.sql.Timestamp", java.sql.Timestamp.class); m.put("Timestamp", java.sql.Timestamp.class); m.put("java.sql.Time", java.sql.Time.class); m.put("Time", java.sql.Time.class); m.put("java.sql.Date", java.sql.Date.class); m.put("Date", java.sql.Date.class); m.put("java.util.Locale", java.util.Locale.class); m.put("java.util.TimeZone", java.util.TimeZone.class); m.put("java.lang.Byte", java.lang.Byte.class); m.put("java.lang.Character", java.lang.Character.class); m.put("java.lang.Integer", java.lang.Integer.class); m.put("Integer", java.lang.Integer.class); m.put("java.lang.Long", java.lang.Long.class); m.put("Long", java.lang.Long.class); m.put("java.lang.Short", java.lang.Short.class); m.put("java.lang.Float", java.lang.Float.class); m.put("Float", java.lang.Float.class); m.put("java.lang.Double", java.lang.Double.class); m.put("Double", java.lang.Double.class); m.put("java.math.BigDecimal", java.math.BigDecimal.class); m.put("BigDecimal", java.math.BigDecimal.class); m.put("java.math.BigInteger", java.math.BigInteger.class); m.put("BigInteger", java.math.BigInteger.class); m.put("java.lang.Boolean", java.lang.Boolean.class); m.put("Boolean", java.lang.Boolean.class); m.put("java.lang.Object", java.lang.Object.class); m.put("Object", java.lang.Object.class); m.put("java.sql.Blob", java.sql.Blob.class); m.put("Blob", java.sql.Blob.class); m.put("java.nio.ByteBuffer", java.nio.ByteBuffer.class); m.put("java.sql.Clob", java.sql.Clob.class); m.put("Clob", java.sql.Clob.class); m.put("java.util.Date", java.util.Date.class); m.put("java.util.Collection", java.util.Collection.class); m.put("Collection", java.util.Collection.class); m.put("java.util.List", java.util.List.class); m.put("List", java.util.List.class); m.put("java.util.ArrayList", java.util.ArrayList.class); m.put("ArrayList", java.util.ArrayList.class); m.put("java.util.Map", java.util.Map.class); m.put("Map", java.util.Map.class); m.put("java.util.HashMap", java.util.HashMap.class); m.put("java.util.Set", java.util.Set.class); m.put("Set", java.util.Set.class); m.put("java.util.HashSet", java.util.HashSet.class); m.put("groovy.util.Node", groovy.util.Node.class); m.put("Node", groovy.util.Node.class); m.put("org.moqui.util.MNode", org.moqui.util.MNode.class); m.put("MNode", org.moqui.util.MNode.class); m.put(Boolean.TYPE.getName(), Boolean.TYPE); m.put(Short.TYPE.getName(), Short.TYPE); m.put(Integer.TYPE.getName(), Integer.TYPE); m.put(Long.TYPE.getName(), Long.TYPE); m.put(Float.TYPE.getName(), Float.TYPE); m.put(Double.TYPE.getName(), Double.TYPE); m.put(Byte.TYPE.getName(), Byte.TYPE); m.put(Character.TYPE.getName(), Character.TYPE); m.put("long[]", long[].class); m.put("char[]", char[].class); return m; }
private boolean compare_PrimitiveArray( int operation, Class<?> type, Object primarray, Object value2) { if (Integer.TYPE.isAssignableFrom(type)) { int[] array = (int[]) primarray; for (int value1 : array) { if (compare_Integer(operation, value1, value2)) { return true; } } return false; } if (Long.TYPE.isAssignableFrom(type)) { long[] array = (long[]) primarray; for (long value1 : array) { if (compare_Long(operation, value1, value2)) { return true; } } return false; } if (Byte.TYPE.isAssignableFrom(type)) { byte[] array = (byte[]) primarray; for (byte value1 : array) { if (compare_Byte(operation, value1, value2)) { return true; } } return false; } if (Short.TYPE.isAssignableFrom(type)) { short[] array = (short[]) primarray; for (short value1 : array) { if (compare_Short(operation, value1, value2)) { return true; } } return false; } if (Character.TYPE.isAssignableFrom(type)) { char[] array = (char[]) primarray; for (char value1 : array) { if (compare_Character(operation, value1, value2)) { return true; } } return false; } if (Float.TYPE.isAssignableFrom(type)) { float[] array = (float[]) primarray; for (float value1 : array) { if (compare_Float(operation, value1, value2)) { return true; } } return false; } if (Double.TYPE.isAssignableFrom(type)) { double[] array = (double[]) primarray; for (double value1 : array) { if (compare_Double(operation, value1, value2)) { return true; } } return false; } if (Boolean.TYPE.isAssignableFrom(type)) { boolean[] array = (boolean[]) primarray; for (boolean value1 : array) { if (compare_Boolean(operation, value1, value2)) { return true; } } return false; } return false; }
/** * {@inheritDoc} * * @see org.teiid.designer.core.properties.PropertyDefinition#isValidValue(java.lang.String) */ @Override public String isValidValue(String newValue) { // if empty must have a value or a default value if required if (StringUtilities.isEmpty(newValue)) { // invalid if required and no default value if (isRequired() && StringUtilities.isEmpty(getDefaultValue())) { return Util.getString("invalidNullPropertyValue", getDisplayName()); // $NON-NLS-1$ } // OK to be null/empty return null; } if (Boolean.class.getName().equals(this.className) || Boolean.TYPE.getName().equals(this.className)) { if (!newValue.equalsIgnoreCase(Boolean.TRUE.toString()) && !newValue.equalsIgnoreCase(Boolean.FALSE.toString())) { return Util.getString( "invalidPropertyValueForType", newValue, Boolean.TYPE.getName()); // $NON-NLS-1$ } } else if (Character.class.getName().equals(this.className) || Character.TYPE.getName().equals(this.className)) { if (newValue.length() != 1) { return Util.getString( "invalidPropertyValueForType", newValue, Character.TYPE.getName()); // $NON-NLS-1$ } } else if (Byte.class.getName().equals(this.className) || Byte.TYPE.getName().equals(this.className)) { try { Byte.parseByte(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Byte.TYPE.getName()); // $NON-NLS-1$ } } else if (Short.class.getName().equals(this.className) || Short.TYPE.getName().equals(this.className)) { try { Short.parseShort(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Short.TYPE.getName()); // $NON-NLS-1$ } } else if (Integer.class.getName().equals(this.className) || Integer.TYPE.getName().equals(this.className)) { try { Integer.parseInt(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Integer.TYPE.getName()); // $NON-NLS-1$ } } else if (Long.class.getName().equals(this.className) || Long.TYPE.getName().equals(this.className)) { try { Long.parseLong(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Long.TYPE.getName()); // $NON-NLS-1$ } } else if (Float.class.getName().equals(this.className) || Float.TYPE.getName().equals(this.className)) { try { Float.parseFloat(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Float.TYPE.getName()); // $NON-NLS-1$ } } else if (Double.class.getName().equals(this.className) || Double.TYPE.getName().equals(this.className)) { try { Double.parseDouble(newValue); } catch (Exception e) { return Util.getString( "invalidPropertyValueForType", newValue, Double.TYPE.getName()); // $NON-NLS-1$ } } else if (!String.class.getName().equals(this.className)) { return Util.getString( "unknownPropertyType", this.displayName, this.className); // $NON-NLS-1$ } // valid value return null; }
private static void assertExpectedPrimitiveArrays( String message, Object expected, Object actual, Class expectedInnerType, Class actualInnerType) { if (Boolean.TYPE.isAssignableFrom(expectedInnerType)) { if (Boolean.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (boolean[]) expected, (boolean[]) actual); } else if (Boolean.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (boolean[]) expected, (Boolean[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (boolean[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Byte.TYPE.isAssignableFrom(expectedInnerType)) { if (Byte.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (byte[]) expected, (byte[]) actual); } else if (Byte.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (byte[]) expected, (Byte[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (byte[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Short.TYPE.isAssignableFrom(expectedInnerType)) { if (Short.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (short[]) expected, (short[]) actual); } else if (Short.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (short[]) expected, (Short[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (short[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Integer.TYPE.isAssignableFrom(expectedInnerType)) { if (Integer.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (int[]) expected, (int[]) actual); } else if (Integer.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (int[]) expected, (Integer[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (int[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Long.TYPE.isAssignableFrom(expectedInnerType)) { if (Long.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (long[]) expected, (long[]) actual); } else if (Long.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (long[]) expected, (Long[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (long[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Float.TYPE.isAssignableFrom(expectedInnerType)) { if (Float.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (float[]) expected, (float[]) actual); } else if (Float.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (float[]) expected, (Float[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (float[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Double.TYPE.isAssignableFrom(expectedInnerType)) { if (Double.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (double[]) expected, (double[]) actual); } else if (Double.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (double[]) expected, (Double[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (double[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } else if (Character.TYPE.isAssignableFrom(expectedInnerType)) { if (Character.TYPE.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (char[]) expected, (char[]) actual); } else if (Character.class.isAssignableFrom(actualInnerType)) { ArrayAssertions.assertEquals(message, (char[]) expected, (Character[]) actual); } else if (!actualInnerType.isPrimitive()) { ArrayAssertions.assertEquals(message, (char[]) expected, (Object[]) actual); } else { failNotEquals(message, expected, actual); } } }