@Rule public CompilationRule compilationRule = new CompilationRule();
@Test
public void equivalence() {
Types types = compilationRule.getTypes();
Elements elements = compilationRule.getElements();
TypeMirror objectType = elements.getTypeElement(Object.class.getCanonicalName()).asType();
TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
TypeElement mapElement = elements.getTypeElement(Map.class.getCanonicalName());
TypeElement setElement = elements.getTypeElement(Set.class.getCanonicalName());
TypeElement enumElement = elements.getTypeElement(Enum.class.getCanonicalName());
TypeElement funkyBounds = elements.getTypeElement(FunkyBounds.class.getCanonicalName());
DeclaredType mapOfObjectToObjectType =
types.getDeclaredType(mapElement, objectType, objectType);
TypeMirror mapType = mapElement.asType();
WildcardType wildcard = types.getWildcardType(null, null);
EquivalenceTester<TypeMirror> tester = EquivalenceTester.<TypeMirror>of(MoreTypes.equivalence())
.addEquivalenceGroup(types.getNullType())
.addEquivalenceGroup(types.getNoType(NONE))
.addEquivalenceGroup(types.getNoType(VOID))
.addEquivalenceGroup(objectType)
.addEquivalenceGroup(stringType)
.addEquivalenceGroup(funkyBounds.asType())
// Enum<E extends Enum<E>>
.addEquivalenceGroup(enumElement.asType())
// Map<K, V>
.addEquivalenceGroup(mapType)
.addEquivalenceGroup(mapOfObjectToObjectType)
// Map<?, ?>
.addEquivalenceGroup(types.getDeclaredType(mapElement, wildcard, wildcard))
// Map
.addEquivalenceGroup(types.erasure(mapType), types.erasure(mapOfObjectToObjectType))
.addEquivalenceGroup(types.getDeclaredType(mapElement, objectType, stringType))
.addEquivalenceGroup(types.getDeclaredType(mapElement, stringType, objectType))
.addEquivalenceGroup(types.getDeclaredType(mapElement, stringType, stringType))
.addEquivalenceGroup(wildcard)
// ? extends Object
.addEquivalenceGroup(types.getWildcardType(objectType, null))
// ? extends String
.addEquivalenceGroup(types.getWildcardType(stringType, null))
// ? super String
.addEquivalenceGroup(types.getWildcardType(null, stringType))
// Map<String, Map<String, Set<Object>>>
.addEquivalenceGroup(types.getDeclaredType(mapElement, stringType,
types.getDeclaredType(mapElement, stringType,
types.getDeclaredType(setElement, objectType))))
.addEquivalenceGroup(FAKE_ERROR_TYPE)
;
for (TypeKind kind : TypeKind.values()) {
if (kind.isPrimitive()) {
PrimitiveType primitiveType = types.getPrimitiveType(kind);
TypeMirror boxedPrimitiveType = types.boxedClass(primitiveType).asType();
tester.addEquivalenceGroup(primitiveType, types.unboxedType(boxedPrimitiveType));
tester.addEquivalenceGroup(boxedPrimitiveType);
tester.addEquivalenceGroup(types.getArrayType(primitiveType));
tester.addEquivalenceGroup(types.getArrayType(boxedPrimitiveType));
}
}
ImmutableSet<Class<?>> testClasses = ImmutableSet.of(
ExecutableElementsGroupA.class,
ExecutableElementsGroupB.class,
ExecutableElementsGroupC.class,
ExecutableElementsGroupD.class,
ExecutableElementsGroupE.class);
for (Class<?> testClass : testClasses) {
ImmutableList<TypeMirror> equivalenceGroup = FluentIterable.from(
elements.getTypeElement(testClass.getCanonicalName()).getEnclosedElements())
.transform(new Function<Element, TypeMirror>() {
@Override public TypeMirror apply(Element input) {
return input.asType();
}
})