/** @deprecated Use {@link Types#fieldType(Field, Class)} instead. */ @Deprecated public static Type getGenericType(final Field field, final Class<?> type) { return Types.fieldType(field, type); }
/** @deprecated Use {@link Types#fieldType(Field, Class)} instead. */ @Deprecated public static Type getFieldType(final Field field, final Class<?> type) { return Types.fieldType(field, type); }
/** @deprecated Use {@link Types#fieldType(Field, Class)} instead. */ @Deprecated public static Type getGenericType(final Field field, final Class<?> type) { return Types.fieldType(field, type); }
/** @deprecated Use {@link Types#fieldType(Field, Class)} instead. */ @Deprecated public static Type getFieldType(final Field field, final Class<?> type) { return Types.fieldType(field, type); }
/** * @deprecated Use {@link Types#fieldType(Field, Class)} and {@link Types#raws} * instead. */ @Deprecated public static List<Class<?>> getTypes(final Field field, final Class<?> type) { return Types.raws(Types.fieldType(field, type)); }
/** * @deprecated Use {@link Types#fieldType(Field, Class)} and {@link Types#raws} * instead. */ @Deprecated public static List<Class<?>> getFieldClasses(final Field field, final Class<?> type) { return Types.raws(Types.fieldType(field, type)); }
/** * @deprecated Use {@link Types#fieldType(Field, Class)} and {@link Types#raws} * instead. */ @Deprecated public static List<Class<?>> getFieldClasses(final Field field, final Class<?> type) { return Types.raws(Types.fieldType(field, type)); }
/** * @deprecated Use {@link Types#fieldType(Field, Class)} and {@link Types#raws} * instead. */ @Deprecated public static List<Class<?>> getTypes(final Field field, final Class<?> type) { return Types.raws(Types.fieldType(field, type)); }
@Override public Type getGenericType() { return Types.fieldType(field, getDelegateClass()); }
@Override public Type getGenericType() { return Types.fieldType(field, getDelegateClass()); }
/** * Sets the given field's value of the specified object instance. * * @throws IllegalArgumentException if the value cannot be set. */ // FIXME: Move to ConvertService and deprecate this signature. public static void setValue(final Field field, final Object instance, final Object value) { try { field.setAccessible(true); final Object compatibleValue; if (value == null || field.getType().isInstance(value)) { // the given value is compatible with the field compatibleValue = value; } else { // the given value needs to be converted to a compatible type final Type fieldType = Types.fieldType(field, instance.getClass()); @SuppressWarnings("deprecation") final Object convertedValue = ConversionUtils.convert(value, fieldType); compatibleValue = convertedValue; } field.set(instance, compatibleValue); } catch (final IllegalAccessException e) { throw new IllegalArgumentException("No access to field: " + field.getName(), e); } }
@Override public Class<T> getType() { final Class<?> type = Types.raw(Types.fieldType(field, getDelegateClass())); @SuppressWarnings("unchecked") final Class<T> typedType = (Class<T>) type; return typedType; }
/** * Sets the given field's value of the specified object instance. * * @throws IllegalArgumentException if the value cannot be set. */ // FIXME: Move to ConvertService and deprecate this signature. public static void setValue(final Field field, final Object instance, final Object value) { try { field.setAccessible(true); final Object compatibleValue; if (value == null || field.getType().isInstance(value)) { // the given value is compatible with the field compatibleValue = value; } else { // the given value needs to be converted to a compatible type final Type fieldType = Types.fieldType(field, instance.getClass()); @SuppressWarnings("deprecation") final Object convertedValue = ConversionUtils.convert(value, fieldType); compatibleValue = convertedValue; } field.set(instance, compatibleValue); } catch (final IllegalAccessException e) { throw new IllegalArgumentException("No access to field: " + field.getName(), e); } }
@Override public Class<T> getType() { final Class<?> type = Types.raw(Types.fieldType(field, getDelegateClass())); @SuppressWarnings("unchecked") final Class<T> typedType = (Class<T>) type; return typedType; }
/** Tests {@link Types#fieldType(Field, Class)}. */ @Test public void testFieldType() { final Field field = Types.field(Thing.class, "thing"); // T final Type tType = Types.fieldType(field, Thing.class); assertEquals("capture of ?", tType.toString()); // N extends Number final Type nType = Types.fieldType(field, NumberThing.class); assertEquals("capture of ?", nType.toString()); // Integer final Type iType = Types.fieldType(field, IntegerThing.class); assertSame(Integer.class, iType); }
@Test public void testCanConvertToGenericCollection() { final CastingConverter cc = new CastingConverter(); final Field destField = Types.field(getClass(), "collection"); final Type destType = Types.fieldType(destField, getClass()); assertTrue(cc.canConvert(ArrayList.class, destType)); }
/** Tests {@link Types#raws}. */ @Test public void testRaws() { final Field field = Types.field(Thing.class, "thing"); // Object assertAllTheSame(Types.raws(Types.fieldType(field, Thing.class)), Object.class); // N extends Number assertAllTheSame(Types.raws(Types.fieldType(field, NumberThing.class)), Number.class); // Integer assertAllTheSame(Types.raws(Types.fieldType(field, IntegerThing.class)), Integer.class); // Serializable & Cloneable assertAllTheSame(Types.raws(Types.fieldType(field, ComplexThing.class)), Serializable.class, Cloneable.class); }