TypeUtils { public static boolean isAssignable(final Type type, final Type toType) { return isAssignable(type, toType, null); } private  TypeUtils(); static boolean isAssignable(final Type type, final Type toType); static Type[] normalizeUpperBounds(final Type[] bounds); static boolean equals(final Type t1, final Type t2); static String toString(final Type type); static int[] add(int[] array, int element);  }
@SuppressWarnings({ "boxing", "JoinDeclarationAndAssignmentJava", "UnusedAssignment" }) @Test public void testIsAssignable() throws SecurityException, NoSuchMethodException, NoSuchFieldException { List list0 = null; List<Object> list1; List<Object> list3; List<String> list4; List<? extends String> list5; List[] list7 = null; List<Object>[] list8; List<?>[] list9; List<? super Object>[] list10; List<String>[] list11; List<? extends String>[] list12; List<? super String>[] list13; final Class<?> clazz = getClass(); final Method method = clazz.getMethod("dummyMethod", List.class, List.class, List.class, List.class, List.class, List.class, List.class, List[].class, List[].class, List[].class, List[].class, List[].class, List[].class, List[].class); final Type[] types = method.getGenericParameterTypes(); delegateBooleanAssertion(types, 0, 0, true); delegateBooleanAssertion(types, 0, 1, true); delegateBooleanAssertion(types, 1, 0, true); delegateBooleanAssertion(types, 0, 2, true); delegateBooleanAssertion(types, 2, 0, true); delegateBooleanAssertion(types, 0, 3, true); delegateBooleanAssertion(types, 3, 0, true); delegateBooleanAssertion(types, 0, 4, true); delegateBooleanAssertion(types, 4, 0, true); delegateBooleanAssertion(types, 0, 5, true); delegateBooleanAssertion(types, 5, 0, true); delegateBooleanAssertion(types, 0, 6, true); delegateBooleanAssertion(types, 6, 0, true); delegateBooleanAssertion(types, 1, 1, true); delegateBooleanAssertion(types, 1, 2, true); delegateBooleanAssertion(types, 2, 1, false); delegateBooleanAssertion(types, 1, 3, true); delegateBooleanAssertion(types, 3, 1, false); delegateBooleanAssertion(types, 1, 4, false); delegateBooleanAssertion(types, 4, 1, false); delegateBooleanAssertion(types, 1, 5, false); delegateBooleanAssertion(types, 5, 1, false); delegateBooleanAssertion(types, 1, 6, true); delegateBooleanAssertion(types, 6, 1, false); delegateBooleanAssertion(types, 2, 2, true); delegateBooleanAssertion(types, 2, 3, false); delegateBooleanAssertion(types, 3, 2, true); delegateBooleanAssertion(types, 2, 4, false); delegateBooleanAssertion(types, 4, 2, true); delegateBooleanAssertion(types, 2, 5, false); delegateBooleanAssertion(types, 5, 2, true); delegateBooleanAssertion(types, 2, 6, false); delegateBooleanAssertion(types, 6, 2, true); delegateBooleanAssertion(types, 3, 3, true); delegateBooleanAssertion(types, 3, 4, false); delegateBooleanAssertion(types, 4, 3, false); delegateBooleanAssertion(types, 3, 5, false); delegateBooleanAssertion(types, 5, 3, false); delegateBooleanAssertion(types, 3, 6, true); delegateBooleanAssertion(types, 6, 3, false); delegateBooleanAssertion(types, 4, 4, true); delegateBooleanAssertion(types, 4, 5, true); delegateBooleanAssertion(types, 5, 4, false); delegateBooleanAssertion(types, 4, 6, true); delegateBooleanAssertion(types, 6, 4, false); delegateBooleanAssertion(types, 5, 5, true); delegateBooleanAssertion(types, 5, 6, false); delegateBooleanAssertion(types, 6, 5, false); delegateBooleanAssertion(types, 6, 6, true); delegateBooleanAssertion(types, 7, 7, true); delegateBooleanAssertion(types, 7, 8, true); delegateBooleanAssertion(types, 8, 7, true); delegateBooleanAssertion(types, 7, 9, true); delegateBooleanAssertion(types, 9, 7, true); delegateBooleanAssertion(types, 7, 10, true); delegateBooleanAssertion(types, 10, 7, true); delegateBooleanAssertion(types, 7, 11, true); delegateBooleanAssertion(types, 11, 7, true); delegateBooleanAssertion(types, 7, 12, true); delegateBooleanAssertion(types, 12, 7, true); delegateBooleanAssertion(types, 7, 13, true); delegateBooleanAssertion(types, 13, 7, true); delegateBooleanAssertion(types, 8, 8, true); delegateBooleanAssertion(types, 8, 9, true); delegateBooleanAssertion(types, 9, 8, false); delegateBooleanAssertion(types, 8, 10, true); delegateBooleanAssertion(types, 10, 8, false); delegateBooleanAssertion(types, 8, 11, false); delegateBooleanAssertion(types, 11, 8, false); delegateBooleanAssertion(types, 8, 12, false); delegateBooleanAssertion(types, 12, 8, false); delegateBooleanAssertion(types, 8, 13, true); delegateBooleanAssertion(types, 13, 8, false); delegateBooleanAssertion(types, 9, 9, true); delegateBooleanAssertion(types, 9, 10, false); delegateBooleanAssertion(types, 10, 9, true); delegateBooleanAssertion(types, 9, 11, false); delegateBooleanAssertion(types, 11, 9, true); delegateBooleanAssertion(types, 9, 12, false); delegateBooleanAssertion(types, 12, 9, true); delegateBooleanAssertion(types, 9, 13, false); delegateBooleanAssertion(types, 13, 9, true); delegateBooleanAssertion(types, 10, 10, true); delegateBooleanAssertion(types, 10, 11, false); delegateBooleanAssertion(types, 11, 10, false); delegateBooleanAssertion(types, 10, 12, false); delegateBooleanAssertion(types, 12, 10, false); delegateBooleanAssertion(types, 10, 13, true); delegateBooleanAssertion(types, 13, 10, false); delegateBooleanAssertion(types, 11, 11, true); delegateBooleanAssertion(types, 11, 12, true); delegateBooleanAssertion(types, 12, 11, false); delegateBooleanAssertion(types, 11, 13, true); delegateBooleanAssertion(types, 13, 11, false); delegateBooleanAssertion(types, 12, 12, true); delegateBooleanAssertion(types, 12, 13, false); delegateBooleanAssertion(types, 13, 12, false); delegateBooleanAssertion(types, 13, 13, true); final Type disType = getClass().getField("dis").getGenericType(); final Type datType = getClass().getField("dat").getGenericType(); final Type daType = getClass().getField("da").getGenericType(); final Type uhderType = getClass().getField("uhder").getGenericType(); final Type dingType = getClass().getField("ding").getGenericType(); final Type testerType = getClass().getField("tester").getGenericType(); final Type tester2Type = getClass().getField("tester2").getGenericType(); final Type dat2Type = getClass().getField("dat2").getGenericType(); final Type dat3Type = getClass().getField("dat3").getGenericType(); dis = dat; assertTrue(TypeUtils.isAssignable(datType, disType)); assertFalse(TypeUtils.isAssignable(daType, disType)); dis = uhder; assertTrue(TypeUtils.isAssignable(uhderType, disType)); dis = ding; assertFalse(TypeUtils.isAssignable(dingType, disType)); dis = tester; assertTrue(TypeUtils.isAssignable(testerType, disType)); assertFalse(TypeUtils.isAssignable(tester2Type, disType)); assertFalse(TypeUtils.isAssignable(dat2Type, datType)); assertFalse(TypeUtils.isAssignable(datType, dat2Type)); assertFalse(TypeUtils.isAssignable(dat3Type, datType)); final char ch = 0; final boolean bo = false; final byte by = 0; final short sh = 0; int in = 0; long lo = 0; final float fl = 0; double du = 0; du = ch; assertTrue(TypeUtils.isAssignable(char.class, double.class)); du = by; assertTrue(TypeUtils.isAssignable(byte.class, double.class)); du = sh; assertTrue(TypeUtils.isAssignable(short.class, double.class)); du = in; assertTrue(TypeUtils.isAssignable(int.class, double.class)); assertTrue(TypeUtils.isAssignable(long.class, double.class)); du = fl; assertTrue(TypeUtils.isAssignable(float.class, double.class)); assertTrue(TypeUtils.isAssignable(int.class, long.class)); assertTrue(TypeUtils.isAssignable(Integer.class, long.class)); assertFalse(TypeUtils.isAssignable(int.class, Long.class)); assertFalse(TypeUtils.isAssignable(Integer.class, Long.class)); assertTrue(TypeUtils.isAssignable(Integer.class, int.class)); assertTrue(TypeUtils.isAssignable(int.class, Integer.class)); assertTrue(TypeUtils.isAssignable(int.class, Number.class)); assertTrue(TypeUtils.isAssignable(int.class, Object.class)); final Type intComparableType = getClass().getField("intComparable").getGenericType(); intComparable = 1; assertTrue(TypeUtils.isAssignable(int.class, intComparableType)); assertTrue(TypeUtils.isAssignable(int.class, Comparable.class)); final Serializable ser = 1; assertTrue(TypeUtils.isAssignable(int.class, Serializable.class)); final Type longComparableType = getClass().getField("longComparable").getGenericType(); assertFalse(TypeUtils.isAssignable(int.class, longComparableType)); assertFalse(TypeUtils.isAssignable(Integer.class, longComparableType)); assertFalse(TypeUtils.isAssignable(int[].class, long[].class)); final Integer[] ia = null; final Type caType = getClass().getField("intWildcardComparable").getGenericType(); assertTrue(TypeUtils.isAssignable(Integer[].class, caType)); assertFalse(TypeUtils.isAssignable(Integer[].class, int[].class)); final int[] ina = null; Object[] oa; assertFalse(TypeUtils.isAssignable(int[].class, Object[].class)); oa = new Integer[0]; assertTrue(TypeUtils.isAssignable(Integer[].class, Object[].class)); final Type bClassType = AClass.class.getField("bClass").getGenericType(); final Type cClassType = AClass.class.getField("cClass").getGenericType(); final Type dClassType = AClass.class.getField("dClass").getGenericType(); final Type eClassType = AClass.class.getField("eClass").getGenericType(); final Type fClassType = AClass.class.getField("fClass").getGenericType(); final AClass aClass = new AClass(new AAClass<>()); aClass.bClass = aClass.cClass; assertTrue(TypeUtils.isAssignable(cClassType, bClassType)); aClass.bClass = aClass.dClass; assertTrue(TypeUtils.isAssignable(dClassType, bClassType)); aClass.bClass = aClass.eClass; assertTrue(TypeUtils.isAssignable(eClassType, bClassType)); aClass.bClass = aClass.fClass; assertTrue(TypeUtils.isAssignable(fClassType, bClassType)); aClass.cClass = aClass.dClass; assertTrue(TypeUtils.isAssignable(dClassType, cClassType)); aClass.cClass = aClass.eClass; assertTrue(TypeUtils.isAssignable(eClassType, cClassType)); aClass.cClass = aClass.fClass; assertTrue(TypeUtils.isAssignable(fClassType, cClassType)); aClass.dClass = aClass.eClass; assertTrue(TypeUtils.isAssignable(eClassType, dClassType)); aClass.dClass = aClass.fClass; assertTrue(TypeUtils.isAssignable(fClassType, dClassType)); aClass.eClass = aClass.fClass; assertTrue(TypeUtils.isAssignable(fClassType, eClassType)); }
@Test public void testFailingWildcard() throws Exception { final Type fromType = ClassWithSuperClassWithGenericType.class.getDeclaredMethod("methodWithGenericReturnType") .getGenericReturnType(); final WildcardType failingToType = new WildcardTypeImpl(null, new Type[] { ClassWithSuperClassWithGenericType.class }); assertTrue(TypeUtils.isAssignable(fromType, failingToType)); }