Java Code Examples for javax.lang.model.util.SimpleTypeVisitor9

Following code examples demonstrate how to use javax.lang.model.util.SimpleTypeVisitor9from java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use javax.lang.model.util.SimpleTypeVisitor9and various code implementation of this class.

    public static TypeDesc fromType(TypeMirror type) {
        TypeVisitor<TypeDesc, Void> v = new SimpleTypeVisitor9<TypeDesc, Void>() {
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitArray(ArrayType t, Void p) {
                return new ArrayTypeDesc(t.getComponentType().accept(this, p));
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitDeclared(DeclaredType t, Void p) {
                return new ReferenceTypeDesc(((ClassType) t).tsym.flatName().toString());
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitNoType(NoType t, Void p) {
                return new PrimitiveTypeDesc(TypeKind.VOID);
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitTypeVariable(TypeVariable t, Void p) {
                return new TypeVarTypeDesc(t.toString());
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitPrimitive(PrimitiveType t, Void p) {
                return new PrimitiveTypeDesc(t.getKind());
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public TypeDesc visitError(ErrorType t, Void p) {
                return new ReferenceTypeDesc("<error type>");
            }
        };

        TypeDesc td = v.visit(type);
        if (td == null)
            throw new AssertionError("Unhandled type mirror: " + type + " (" + type.getClass() + ")");
        return td;
    } 


    String qualifiedTypeName(TypeMirror type) {
        TypeVisitor<Name, Void> v = new SimpleTypeVisitor9<Name, Void>() {
            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public Name visitArray(ArrayType t, Void p) {
                return t.getComponentType().accept(this, p);
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public Name visitDeclared(DeclaredType t, Void p) {
                return ((TypeElement) t.asElement()).getQualifiedName();
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public Name visitPrimitive(PrimitiveType t, Void p) {
                return elems.getName(t.toString());
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public Name visitNoType(NoType t, Void p) {
                if (t.getKind() == TypeKind.VOID)
                    return elems.getName("void");
                return defaultAction(t, p);
            }

            @Override @DefinedBy(Api.LANGUAGE_MODEL)
            public Name visitTypeVariable(TypeVariable t, Void p) {
                return t.getUpperBound().accept(this, p);
            }
        };
        return v.visit(type).toString();
    } 

Advertisement
Javadoc
A simple visitor of types with default behavior appropriate for the SourceVersion#RELEASE_9 RELEASE_

9 source version. Visit methods corresponding to RELEASE_9 and earlier language constructs call #defaultAction defaultAction, passing their arguments to defaultAction's corresponding parameters.

Methods in this class may be overridden subject to their general contract. Note that annotating methods in concrete subclasses with java.lang.Override @Override will help ensure that methods are overridden as intended.

WARNING: The TypeVisitor interface implemented by this class may have methods added to it in the future to accommodate new, currently unknown, language structures added to future versions of the Java™ programming language. Therefore, methods whose names begin with "visit" may be added to this class in the future; to avoid incompatibilities, classes which extend this class should not declare any instance methods with names beginning with "visit".

When such a new visit method is added, the default implementation in this class will be to call the #visitUnknown visitUnknown method. A new simple type visitor class will also be introduced to correspond to the new language level; this visitor will have different default behavior for the visit method in question. When the new visitor is introduced, all or portions of this visitor may be deprecated. @param the return type of this visitor's methods. Use Void for visitors that do not need to return results. @param the type of the additional parameter to this visitor's methods. Use Void for visitors that do not need an additional parameter. @see SimpleTypeVisitor6 @see SimpleTypeVisitor7 @since 9

Read More
Advertisement