public static ImmutableSet<TypeElement> referencedTypes(TypeMirror type) {
        checkNotNull(type);
        ImmutableSet.Builder<TypeElement> elements = ImmutableSet.builder();
        type.accept(new SimpleTypeVisitor6<Void, ImmutableSet.Builder<TypeElement>>() {
            @Override
            public Void visitArray(ArrayType t, ImmutableSet.Builder<TypeElement> p) {
                t.getComponentType().accept(this, p);
                return null;
            }

            @Override
            public Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
                p.add(MoreElements.asType(t.asElement()));
                for (TypeMirror typeArgument : t.getTypeArguments()) {
                    typeArgument.accept(this, p);
                }
                return null;
            }

            @Override
            public Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder<TypeElement> p) {
                t.getLowerBound().accept(this, p);
                t.getUpperBound().accept(this, p);
                return null;
            }

            @Override
            public Void visitWildcard(WildcardType t, ImmutableSet.Builder<TypeElement> p) {
                TypeMirror extendsBound = t.getExtendsBound();
                if (extendsBound != null) {
                    extendsBound.accept(this, p);
                }
                TypeMirror superBound = t.getSuperBound();
                if (superBound != null) {
                    superBound.accept(this, p);
                }
                return null;
            }
        }, elements);
        return elements.build();
    }
Utilities related to {@link TypeMirror} instances.

@author Gregory Kick
@since 2.0
android/annotation_processors/permission-requester-compiler/src/main/java/github/tornaco/permission/compiler/common/MoreTypes.java