/** * 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); }
/** 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); } }
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; } }
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; } }
/** * 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; }
/** * 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)); }
/** * Tests for equality. * * @param o the object to test. * @return A boolean. */ public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof AbstractObjectDescription)) { return false; } final AbstractObjectDescription abstractObjectDescription = (AbstractObjectDescription) o; if (Short.TYPE.equals(abstractObjectDescription.getObjectClass())) { return true; } if (Short.class.equals(abstractObjectDescription.getObjectClass())) { return true; } return false; }
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)); } }
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; }