@Test public void testNonObjectSuperclass() { Types types = compilationRule.getTypes(); Elements elements = compilationRule.getElements(); TypeMirror numberType = elements.getTypeElement(Number.class.getCanonicalName()).asType(); TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType(); TypeMirror integerType = elements.getTypeElement(Integer.class.getCanonicalName()).asType(); TypeElement parent = elements.getTypeElement(Parent.class.getCanonicalName()); TypeElement childA = elements.getTypeElement(ChildA.class.getCanonicalName()); TypeElement childB = elements.getTypeElement(ChildB.class.getCanonicalName()); TypeElement genericChild = elements.getTypeElement(GenericChild.class.getCanonicalName()); TypeMirror genericChildOfNumber = types.getDeclaredType(genericChild, numberType); TypeMirror genericChildOfInteger = types.getDeclaredType(genericChild, integerType); TypeMirror objectType = elements.getTypeElement(Object.class.getCanonicalName()).asType(); TypeMirror interfaceType = elements.getTypeElement(InterfaceType.class.getCanonicalName()).asType(); assertThat(MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) objectType)) .isAbsent(); assertThat(MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) interfaceType)) .isAbsent(); assertThat(MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) parent.asType())) .isAbsent(); Optional<DeclaredType> parentOfChildA = MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) childA.asType()); Optional<DeclaredType> parentOfChildB = MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) childB.asType()); Optional<DeclaredType> parentOfGenericChild = MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) genericChild.asType()); Optional<DeclaredType> parentOfGenericChildOfNumber = MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) genericChildOfNumber); Optional<DeclaredType> parentOfGenericChildOfInteger = MoreTypes.nonObjectSuperclass(types, elements, (DeclaredType) genericChildOfInteger); EquivalenceTester<TypeMirror> tester = EquivalenceTester.<TypeMirror>of(MoreTypes.equivalence()) .addEquivalenceGroup(parentOfChildA.get(), types.getDeclaredType(parent, numberType), parentOfGenericChildOfNumber.get()) .addEquivalenceGroup(parentOfChildB.get(), types.getDeclaredType(parent, stringType)) .addEquivalenceGroup(parentOfGenericChild.get(), parent.asType()) .addEquivalenceGroup(parentOfGenericChildOfInteger.get(), types.getDeclaredType(parent, integerType)); tester.test(); }
public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type) { checkNotNull(types); checkNotNull(elements); checkNotNull(type); TypeMirror superclassType = asTypeElement(type).getSuperclass(); if (!isType(superclassType)) { return Optional.absent(); } DeclaredType superclass = asDeclared(superclassType); if (isObjectType(superclass)) { return Optional.absent(); } if (superclass.getTypeArguments().isEmpty()) { return Optional.of(superclass); } return Optional.of(asDeclared(types.directSupertypes(type).get(0))); }
MoreTypes { public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type) { checkNotNull(types); checkNotNull(elements); checkNotNull(type); TypeMirror superclassType = asTypeElement(type).getSuperclass(); if (!isType(superclassType)) { return Optional.absent(); } DeclaredType superclass = asDeclared(superclassType); if (isObjectType(superclass)) { return Optional.absent(); } if (superclass.getTypeArguments().isEmpty()) { return Optional.of(superclass); } return Optional.of(asDeclared(types.directSupertypes(type).get(0))); } }
MoreTypes { public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type) { checkNotNull(types); checkNotNull(elements); checkNotNull(type); TypeMirror superclassType = asTypeElement(type).getSuperclass(); if (!isType(superclassType)) { return Optional.absent(); } DeclaredType superclass = asDeclared(superclassType); if (isObjectType(superclass)) { return Optional.absent(); } if (superclass.getTypeArguments().isEmpty()) { return Optional.of(superclass); } return Optional.of(asDeclared(types.directSupertypes(type).get(0))); } private  MoreTypes(); }
MoreTypes { public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type) { checkNotNull(types); checkNotNull(elements); checkNotNull(type); TypeMirror superclassType = asTypeElement(type).getSuperclass(); if (!isType(superclassType)) { return Optional.absent(); } DeclaredType superclass = asDeclared(superclassType); if (isObjectType(superclass)) { return Optional.absent(); } if (superclass.getTypeArguments().isEmpty()) { return Optional.of(superclass); } return Optional.of(asDeclared(types.directSupertypes(type).get(0))); } private  MoreTypes(); static Equivalence<TypeMirror> equivalence(); static ImmutableSet<TypeElement> referencedTypes(TypeMirror type); static Element asElement(TypeMirror typeMirror); static TypeElement asTypeElement(TypeMirror mirror); static ImmutableSet<TypeElement> asTypeElements(Iterable<? extends TypeMirror> mirrors); static ArrayType asArray(TypeMirror maybeArrayType); static DeclaredType asDeclared(TypeMirror maybeDeclaredType); static ErrorType asError(TypeMirror maybeErrorType); static ExecutableType asExecutable(TypeMirror maybeExecutableType); static IntersectionType asIntersection(TypeMirror maybeIntersectionType); static NoType asNoType(TypeMirror maybeNoType); static NullType asNullType(TypeMirror maybeNullType); static PrimitiveType asPrimitiveType(TypeMirror maybePrimitiveType); static TypeVariable asTypeVariable(TypeMirror maybeTypeVariable); static WildcardType asWildcard(TypeMirror maybeWildcardType); static boolean isType(TypeMirror type); static boolean isTypeOf(final Class<?> clazz, TypeMirror type); static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements,
      DeclaredType type); static TypeMirror asMemberOf(Types types, DeclaredType container,
      VariableElement variable); static boolean isConversionFromObjectUnchecked(TypeMirror type); }
MoreTypes { public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements, DeclaredType type) { checkNotNull(types); checkNotNull(elements); checkNotNull(type); TypeMirror superclassType = asTypeElement(type).getSuperclass(); if (!isType(superclassType)) { return Optional.absent(); } DeclaredType superclass = asDeclared(superclassType); if (isObjectType(superclass)) { return Optional.absent(); } if (superclass.getTypeArguments().isEmpty()) { return Optional.of(superclass); } return Optional.of(asDeclared(types.directSupertypes(type).get(0))); } private  MoreTypes(); static Equivalence<TypeMirror> equivalence(); static ImmutableSet<TypeElement> referencedTypes(TypeMirror type); static Element asElement(TypeMirror typeMirror); static TypeElement asTypeElement(TypeMirror mirror); static ImmutableSet<TypeElement> asTypeElements(Iterable<? extends TypeMirror> mirrors); static ArrayType asArray(TypeMirror maybeArrayType); static DeclaredType asDeclared(TypeMirror maybeDeclaredType); static ErrorType asError(TypeMirror maybeErrorType); static ExecutableType asExecutable(TypeMirror maybeExecutableType); static IntersectionType asIntersection(TypeMirror maybeIntersectionType); static NoType asNoType(TypeMirror maybeNoType); static NullType asNullType(TypeMirror maybeNullType); static PrimitiveType asPrimitiveType(TypeMirror maybePrimitiveType); static TypeVariable asTypeVariable(TypeMirror maybeTypeVariable); static WildcardType asWildcard(TypeMirror maybeWildcardType); static boolean isType(TypeMirror type); static boolean isTypeOf(final Class<?> clazz, TypeMirror type); static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements,
      DeclaredType type); static TypeMirror asMemberOf(Types types, DeclaredType container,
      VariableElement variable); static boolean isConversionFromObjectUnchecked(TypeMirror type);  }