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; } }
/** 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); } }
/** * 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"); }
/** * 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)); }
/** * 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); }
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 static ArrayProperty createArrayProperty(Property componentProperty, Class type) throws JAXBException { if (!type.isPrimitive()) return new ObjectArrayProperty(componentProperty, type); if (Double.TYPE.equals(type)) return DoubleArrayProperty.PROPERTY; if (Float.TYPE.equals(type)) return FloatArrayProperty.PROPERTY; if (Integer.TYPE.equals(type)) return IntegerArrayProperty.PROPERTY; if (Long.TYPE.equals(type)) return LongArrayProperty.PROPERTY; if (Boolean.TYPE.equals(type)) return BooleanArrayProperty.PROPERTY; if (Character.TYPE.equals(type)) return CharacterArrayProperty.PROPERTY; if (Short.TYPE.equals(type)) return ShortArrayProperty.PROPERTY; /* XXX if (Byte.TYPE.equals(type)) return ByteArrayProperty.PROPERTY; */ throw new JAXBException(L.l("{0} is neither primitive, nor non-primitive!", type)); }
/** * 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; }
/** * @param pageParameters * @param key * @param objectType * @see #putPageParameter(PageParameters, String, Object) */ public static Object getPageParameter( final PageParameters pageParameters, final String key, final Class<?> objectType) { if (objectType.isAssignableFrom(Date.class) == true) { final StringValue sval = pageParameters.get(key); if (sval.isNull() == true) { return null; } return new Date(sval.toLongObject()); } else if (objectType.isAssignableFrom(Boolean.class) == true) { return pageParameters.get(key).toBooleanObject(); } else if (objectType.isPrimitive() == true) { if (Boolean.TYPE.equals(objectType)) { return pageParameters.get(key).toBooleanObject(); } else if (Integer.TYPE.equals(objectType) == true) { return pageParameters.get(key).toInteger(); } else if (Long.TYPE.equals(objectType) == true) { return pageParameters.get(key).toLong(); } else if (Float.TYPE.equals(objectType) == true) { return new Float(pageParameters.get(key).toDouble()); } else if (Double.TYPE.equals(objectType) == true) { return pageParameters.get(key).toDouble(); } else if (Character.TYPE.equals(objectType) == true) { return pageParameters.get(key).toChar(); } else { log.warn( "Primitive objectType '" + objectType + "' not yet implemented. Parameter type '" + key + "' is ignored."); } } else if (Enum.class.isAssignableFrom(objectType) == true) { final StringValue sval = pageParameters.get(key); if (sval.isNull() == true) { return null; } final String sValue = sval.toString(); @SuppressWarnings({"unchecked", "rawtypes"}) final Enum<?> en = Enum.valueOf((Class<Enum>) objectType, sValue); return en; } else if (objectType.isAssignableFrom(Integer.class) == true) { final StringValue sval = pageParameters.get(key); if (sval.isNull() == true) { return null; } return sval.toInteger(); } else if (objectType.isAssignableFrom(String.class) == true) { return pageParameters.get(key).toString(); } else if (objectType.isAssignableFrom(TimePeriod.class) == true) { final String sValue = pageParameters.get(key).toString(); if (sValue == null) { return null; } final int pos = sValue.indexOf('-'); if (pos < 0) { log.warn( "PageParameter of type TimePeriod '" + objectType.getName() + "' in wrong format: " + sValue); return null; } final Long fromTime = NumberHelper.parseLong(sValue.substring(0, pos)); final Long toTime = pos < sValue.length() - 1 ? NumberHelper.parseLong(sValue.substring(pos + 1)) : null; return new TimePeriod( fromTime != null ? new Date(fromTime) : null, toTime != null ? new Date(toTime) : null); } else { log.error("PageParameter of type '" + objectType.getName() + "' not yet supported."); } return null; }