@Override public boolean canConvert(final Class<?> src, final Class<?> dest) { // OK if the existing object can be casted return dest != null && Types.isAssignable(src, dest); }
@Override public boolean canConvert(final Class<?> src, final Class<?> dest) { // OK if the existing object can be casted return dest != null && Types.isAssignable(src, dest); }
/** @deprecated use {@link Types#isAssignable(Type, Type)} */ @Deprecated public static boolean canCast(final Class<?> c, final Class<?> type) { return Types.isAssignable(c, type); }
/** @deprecated use {@link Types#isAssignable(Type, Type)} */ @Deprecated public static boolean canCast(final Class<?> c, final Class<?> type) { return Types.isAssignable(c, type); }
private Collection<Object> createCollection(final Class<?> type) { // If we were given an interface or abstract class, and not a concrete // class, we attempt to make default implementations. if (type.isInterface() || Modifier.isAbstract(type.getModifiers())) { // We don't have a concrete class. If it's a set or a list, we use // the typical default implementation. Otherwise we won't convert. if (Types.isAssignable(type, List.class)) return new ArrayList<>(); if (Types.isAssignable(type, Set.class)) return new HashSet<>(); return null; } // Got a concrete type. Instantiate it. try { @SuppressWarnings("unchecked") final Collection<Object> c = (Collection<Object>) type.newInstance(); return c; } catch (final InstantiationException exc) { return null; } catch (final IllegalAccessException exc) { return null; } }
private boolean isCollection(final Type type) { return Types.isAssignable(Types.raw(type), Collection.class); }
private boolean isCollection(final Type type) { return Types.isAssignable(Types.raw(type), Collection.class); }
private Collection<Object> createCollection(final Class<?> type) { // If we were given an interface or abstract class, and not a concrete // class, we attempt to make default implementations. if (type.isInterface() || Modifier.isAbstract(type.getModifiers())) { // We don't have a concrete class. If it's a set or a list, we use // the typical default implementation. Otherwise we won't convert. if (Types.isAssignable(type, List.class)) return new ArrayList<>(); if (Types.isAssignable(type, Set.class)) return new HashSet<>(); return null; } // Got a concrete type. Instantiate it. try { @SuppressWarnings("unchecked") final Collection<Object> c = (Collection<Object>) type.newInstance(); return c; } catch (final InstantiationException exc) { return null; } catch (final IllegalAccessException exc) { return null; } }
private <T> ModuleItem<T> castItem(final ModuleItem<?> item, final Class<T> type) { final Class<?> itemType = item.getType(); // if (!type.isAssignableFrom(itemType)) { final Class<?> saneItemType = Types.box(itemType); if (!Types.isAssignable(type, saneItemType)) { throw new IllegalArgumentException("Type " + type.getName() + " is incompatible with item of type " + itemType.getName()); } @SuppressWarnings("unchecked") final ModuleItem<T> typedItem = (ModuleItem<T>) item; return typedItem; }
private <T> ModuleItem<T> castItem(final ModuleItem<?> item, final Class<T> type) { final Class<?> itemType = item.getType(); // if (!type.isAssignableFrom(itemType)) { final Class<?> saneItemType = Types.box(itemType); if (!Types.isAssignable(type, saneItemType)) { throw new IllegalArgumentException("Type " + type.getName() + " is incompatible with item of type " + itemType.getName()); } @SuppressWarnings("unchecked") final ModuleItem<T> typedItem = (ModuleItem<T>) item; return typedItem; }
private Constructor<?> getConstructor(final Class<?> type, final Class<?> argType) { for (final Constructor<?> ctor : type.getConstructors()) { final Class<?>[] params = ctor.getParameterTypes(); if (params.length == 1 && // Types.isAssignable(Types.box(argType), Types.box(params[0]))) { return ctor; } } return null; }
/** @deprecated use {@link Types#isAssignable} */ @Deprecated public static boolean canCast(final Class<?> src, final Class<?> dest) { if (dest == null) return false; if (src == null) return true; return Types.isAssignable(Types.box(src), Types.box(dest)); }
/** @deprecated use {@link Types#isAssignable} */ @Deprecated public static boolean canCast(final Class<?> src, final Class<?> dest) { if (dest == null) return false; if (src == null) return true; return Types.isAssignable(Types.box(src), Types.box(dest)); }
private Constructor<?> getConstructor(final Class<?> type, final Class<?> argType) { for (final Constructor<?> ctor : type.getConstructors()) { final Class<?>[] params = ctor.getParameterTypes(); if (params.length == 1 && // Types.isAssignable(Types.box(argType), Types.box(params[0]))) { return ctor; } } return null; }
@Override public boolean canConvert(final Class<?> src, final Class<?> dest) { if (src == null) return false; final Class<?> saneSrc = Types.box(src); final Class<?> saneDest = Types.box(dest); return Types.isAssignable(saneSrc, getInputType()) && Types.isAssignable(getOutputType(), saneDest); }
@Override public boolean canConvert(final Class<?> src, final Class<?> dest) { if (src == null) return false; final Class<?> saneSrc = Types.box(src); final Class<?> saneDest = Types.box(dest); return Types.isAssignable(saneSrc, getInputType()) && Types.isAssignable(getOutputType(), saneDest); }
/** Tests {@link Types#isAssignable(Type, Type)} from null to null. */ @Test(expected = NullPointerException.class) public void testIsAssignableNullToNull() { Types.isAssignable(null, null); }
/** Tests {@link Types#isAssignable(Type, Type)} from Class to null. */ @Test(expected = NullPointerException.class) public void testIsAssignableClassToNull() { Types.isAssignable(Object.class, null); }
/** Tests {@link Types#isAssignable(Type, Type)}. */ @Test public void testIsAssignable() { // check casting to superclass assertTrue(Types.isAssignable(String.class, Object.class)); // check casting to interface assertTrue(Types.isAssignable(ArrayList.class, Collection.class)); // casting numeric primitives is not supported assertFalse(Types.isAssignable(double.class, float.class)); assertFalse(Types.isAssignable(float.class, double.class)); // boxing is not reported to work // TODO: Consider changing this behavior. assertFalse(Types.isAssignable(int.class, Number.class)); // casting from null always works assertTrue(Types.isAssignable(null, Object.class)); assertTrue(Types.isAssignable(null, int[].class)); }
if (Types.isAssignable(Types.box(src), Number.class) && // (Types.isByte(dest) || Types.isDouble(dest) || Types.isFloat(dest) || Types.isInteger(dest) || Types.isLong(dest) || Types.isShort(dest))) if (Types.isAssignable(src, String.class)) {