@Override public final boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof TypeLiteral)) { return false; } final TypeLiteral<?> other = (TypeLiteral<?>) obj; return TypeUtils.equals(value, other.value); }
/** * Learn whether {@code t1} equals {@code t2}. * @param t1 LHS * @param t2 RHS * @return boolean * @since 3.2 */ private static boolean equals(final Type[] t1, final Type[] t2) { if (t1.length == t2.length) { for (int i = 0; i < t1.length; i++) { if (!equals(t1[i], t2[i])) { return false; } } return true; } return false; }
/** * Learn whether {@code t} equals {@code a}. * @param a LHS * @param t RHS * @return boolean * @since 3.2 */ private static boolean equals(final GenericArrayType a, final Type t) { return t instanceof GenericArrayType && equals(a.getGenericComponentType(), ((GenericArrayType) t).getGenericComponentType()); }
/** * Check equality of types. * * @param t1 the first type * @param t2 the second type * @return boolean * @since 3.2 */ public static boolean equals(final Type t1, final Type t2) { if (Objects.equals(t1, t2)) { return true; } if (t1 instanceof ParameterizedType) { return equals((ParameterizedType) t1, t2); } if (t1 instanceof GenericArrayType) { return equals((GenericArrayType) t1, t2); } if (t1 instanceof WildcardType) { return equals((WildcardType) t1, t2); } return false; }
/** * Learn whether {@code t} equals {@code p}. * @param p LHS * @param t RHS * @return boolean * @since 3.2 */ private static boolean equals(final ParameterizedType p, final Type t) { if (t instanceof ParameterizedType) { final ParameterizedType other = (ParameterizedType) t; if (equals(p.getRawType(), other.getRawType()) && equals(p.getOwnerType(), other.getOwnerType())) { return equals(p.getActualTypeArguments(), other.getActualTypeArguments()); } } return false; }
/** * Learn whether {@code t} equals {@code w}. * @param w LHS * @param t RHS * @return boolean * @since 3.2 */ private static boolean equals(final WildcardType w, final Type t) { if (t instanceof WildcardType) { final WildcardType other = (WildcardType) t; return equals(getImplicitLowerBounds(w), getImplicitLowerBounds(other)) && equals(getImplicitUpperBounds(w), getImplicitUpperBounds(other)); } return false; }
final Type childType = TypeUtils.unrollVariables(typeArguments, method.getGenericParameterTypes()[i]); final Type parentType = TypeUtils.unrollVariables(typeArguments, m.getGenericParameterTypes()[i]); if (!TypeUtils.equals(childType, parentType)) { continue hierarchyTraversal;
@Test public void testLang1114() throws Exception { final Type nonWildcardType = getClass().getDeclaredField("wildcardComparable").getGenericType(); final Type wildcardType = ((ParameterizedType)nonWildcardType).getActualTypeArguments()[0]; assertFalse(TypeUtils.equals(wildcardType, nonWildcardType)); assertFalse(TypeUtils.equals(nonWildcardType, wildcardType)); }
@Test public void testParameterize() throws Exception { final ParameterizedType stringComparableType = TypeUtils.parameterize(Comparable.class, String.class); assertTrue(TypeUtils.equals(getClass().getField("stringComparable").getGenericType(), stringComparableType)); assertEquals("java.lang.Comparable<java.lang.String>", stringComparableType.toString()); }
@Test public void testParameterizeWithOwner() throws Exception { final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class); final ParameterizedType dat2Type = TypeUtils.parameterizeWithOwner(owner, That.class, String.class, String.class); assertTrue(TypeUtils.equals(getClass().getField("dat2").getGenericType(), dat2Type)); }
@Test public void testWildcardType() throws Exception { final WildcardType simpleWildcard = TypeUtils.wildcardType().withUpperBounds(String.class).build(); final Field cClass = AClass.class.getField("cClass"); assertTrue(TypeUtils.equals(((ParameterizedType) cClass.getGenericType()).getActualTypeArguments()[0], simpleWildcard)); assertEquals(String.format("? extends %s", String.class.getName()), TypeUtils.toString(simpleWildcard)); assertEquals(String.format("? extends %s", String.class.getName()), simpleWildcard.toString()); }
@Test public void testTyped() { final Typed<String> stringType = new TypeLiteral<String>() {}; assertTrue(TypeUtils.equals(String.class, stringType.getType())); final Typed<List<String>> listOfStringType = new TypeLiteral<List<String>>() {}; assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), listOfStringType.getType())); }
@Test public void testGetOverrideHierarchyExcludingInterfaces() { final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class); final Iterator<MethodDescriptor> expected = Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class), new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0])) .iterator(); for (final Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.EXCLUDE)) { assertTrue(expected.hasNext()); final MethodDescriptor md = expected.next(); assertEquals(md.declaringClass, m.getDeclaringClass()); assertEquals(md.name, m.getName()); assertEquals(md.parameterTypes.length, m.getParameterTypes().length); for (int i = 0; i < md.parameterTypes.length; i++) { assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i])); } } assertFalse(expected.hasNext()); }
@Test public void testGenericArrayType() throws Exception { final Type expected = getClass().getField("intWildcardComparable").getGenericType(); final GenericArrayType actual = TypeUtils.genericArrayType(TypeUtils.parameterize(Comparable.class, TypeUtils.wildcardType() .withUpperBounds(Integer.class).build())); assertTrue(TypeUtils.equals(expected, actual)); assertEquals("java.lang.Comparable<? extends java.lang.Integer>[]", actual.toString()); }
@Test public void testGetOverrideHierarchyIncludingInterfaces() { final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class); final Iterator<MethodDescriptor> expected = Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class), new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0]), new MethodDescriptor(GenericConsumer.class, "consume", GenericConsumer.class.getTypeParameters()[0])) .iterator(); for (final Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.INCLUDE)) { assertTrue(expected.hasNext()); final MethodDescriptor md = expected.next(); assertEquals(md.declaringClass, m.getDeclaringClass()); assertEquals(md.name, m.getName()); assertEquals(md.parameterTypes.length, m.getParameterTypes().length); for (int i = 0; i < md.parameterTypes.length; i++) { assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i])); } } assertFalse(expected.hasNext()); }
@Test public void testWrap() { final Type t = getClass().getTypeParameters()[0]; assertTrue(TypeUtils.equals(t, TypeUtils.wrap(t).getType())); assertEquals(String.class, TypeUtils.wrap(String.class).getType()); }
@Test public void testUnboundedWildcardType() { final WildcardType unbounded = TypeUtils.wildcardType().withLowerBounds((Type) null).withUpperBounds().build(); assertTrue(TypeUtils.equals(TypeUtils.WILDCARD_ALL, unbounded)); assertArrayEquals(new Type[] { Object.class }, TypeUtils.getImplicitUpperBounds(unbounded)); assertArrayEquals(new Type[] { null }, TypeUtils.getImplicitLowerBounds(unbounded)); assertEquals("?", TypeUtils.toString(unbounded)); assertEquals("?", unbounded.toString()); }
@Override public final boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof TypeLiteral)) { return false; } final TypeLiteral<?> other = (TypeLiteral<?>) obj; return TypeUtils.equals(value, other.value); }
/** * Learn whether {@code t} equals {@code a}. * @param a LHS * @param t RHS * @return boolean * @since 3.2 */ private static boolean equals(final GenericArrayType a, final Type t) { return t instanceof GenericArrayType && equals(a.getGenericComponentType(), ((GenericArrayType) t).getGenericComponentType()); }