Top Description Inners Fields Constructors Methods
com.google.auto.value.processor

pack-priv final Class TypeEncoder

extends Object
Class Inheritance
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkState, java.util.stream.Collectors.toList

Encodes types so they can later be decoded to incorporate imports.

The idea is that types that appear in generated source code use encode, which will spell out a type like java.util.List<? extends java.lang.Number>, except that wherever a class name appears it is replaced by a special token. So the spelling might actually be `java.util.List`<? extends `java.lang.Number`>. Then once the entire class has been generated, #decode scans for these tokens to determine what classes need to be imported, and replaces the tokens with the correct spelling given the imports. So here, java.util.List would be imported, and the final spelling would be List<? extends Number> (knowing that Number is implicitly imported). The special token `import` marks where the imports should be, and decode replaces it with the correct list of imports.

The funky syntax for type annotations on qualified type names requires an adjustment to this scheme. `«java.util.Map` stands for java.util. and `»java.util.Map` stands for Map. If java.util.Map is imported, then `«java.util.Map` will eventually be empty, but if java.util.Map is not imported (perhaps because there is another Map in scope) then `«java.util.Map` will be java.util.. The end result is that the code can contain `«java.util.Map`@`javax.annotation.Nullable` `»java.util.Map`. That might decode to @Nullable Map or to java.util.@Nullable Map or even to java.util.@javax.annotation.Nullable Map.

Author
[email protected] (Éamonn McManus)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TypeEncoder.AnnotatedEncodingTypeVisitor

Like EncodingTypeVisitor except that annotations on the visited type are also included in the resultant string.

private static class
TypeEncoder.EncodingTypeVisitor

Converts a type into a string, using standard Java syntax, except that every class name is wrapped in backquotes, like `java.util.List`.

private static class
TypeEncoder.RawEncodingTypeVisitor

Like EncodingTypeVisitor except that type parameters are omitted from the result.

private static class

Field Summary

Modifier and TypeField and Description
private static final TypeEncoder.EncodingTypeVisitor
private static final TypeEncoder.RawEncodingTypeVisitor

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
appendAnnotations(List<? extends javax.lang.model.element.AnnotationMirror> annotationMirrors, StringBuilder sb)

private static void
appendTypeParameterWithBounds(javax.lang.model.element.TypeParameterElement typeParameter, StringBuilder sb)

private static String
className(javax.lang.model.type.DeclaredType declaredType)

pack-priv static String
decode(String
the text to be decoded.
text
,
javax.annotation.processing.ProcessingEnvironment processingEnv, String
the package of the generated class. Other classes in the same package do not need to be imported.
packageName
,
javax.lang.model.type.TypeMirror
a class or interface that the generated class inherits from. Nested classes in that type do not need to be imported, and if another class has the same name as one of those nested classes then it will need to be qualified.
baseType
)

Decodes the given string, respelling class names appropriately.

pack-priv static String
decode(String text, javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String pkg, javax.lang.model.type.TypeMirror baseType)

pack-priv static String
encode(javax.lang.model.type.TypeMirror type)

Returns the encoding for the given type, where class names are marked by special tokens.

pack-priv static String
encodeRaw(javax.lang.model.type.TypeMirror type)

Like encode, except that only the raw type is encoded.

pack-priv static String
encodeWithAnnotations(javax.lang.model.type.TypeMirror type)

Encodes the given type and its type annotations.

pack-priv static String
encodeWithAnnotations(javax.lang.model.type.TypeMirror type, ImmutableList<javax.lang.model.element.AnnotationMirror>
additional type annotations to include with the type
extraAnnotations
)

Encodes the given type and its type annotations.

pack-priv static String
encodeWithAnnotations(javax.lang.model.type.TypeMirror type, ImmutableList<javax.lang.model.element.AnnotationMirror>
additional type annotations to include with the type
extraAnnotations
,
Set<javax.lang.model.type.TypeMirror>
annotations not to include in the encoding. For example, if com.example.Nullable is in this set then the encoding will not include this @Nullable annotation.
excludedAnnotationTypes
)

Encodes the given type and its type annotations.

private static boolean
isUnannotatedJavaLangObject(javax.lang.model.type.TypeMirror type)

pack-priv static String
typeParametersString(List<? extends javax.lang.model.element.TypeParameterElement> typeParameters)

Returns a string representing the given type parameters as they would appear in a class declaration.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ENCODING_TYPE_VISITORback to summary
private static final TypeEncoder.EncodingTypeVisitor ENCODING_TYPE_VISITOR
RAW_ENCODING_TYPE_VISITORback to summary
private static final TypeEncoder.RawEncodingTypeVisitor RAW_ENCODING_TYPE_VISITOR

Constructor Detail

TypeEncoderback to summary
private TypeEncoder()

Method Detail

appendAnnotationsback to summary
private static void appendAnnotations(List<? extends javax.lang.model.element.AnnotationMirror> annotationMirrors, StringBuilder sb)
appendTypeParameterWithBoundsback to summary
private static void appendTypeParameterWithBounds(javax.lang.model.element.TypeParameterElement typeParameter, StringBuilder sb)
classNameback to summary
private static String className(javax.lang.model.type.DeclaredType declaredType)
decodeback to summary
pack-priv static String decode(String text, javax.annotation.processing.ProcessingEnvironment processingEnv, String packageName, javax.lang.model.type.TypeMirror baseType)

Decodes the given string, respelling class names appropriately. The text is scanned for tokens like `java.util.Locale` or `«java.util.Locale` to determine which classes are referenced. An appropriate set of imports is computed based on the set of those types. If the special token `import` appears in text then it will be replaced by this set of import statements. Then all of the tokens are replaced by the class names they represent, spelled appropriately given the import statements.

Parameters
text:String

the text to be decoded.

packageName:String

the package of the generated class. Other classes in the same package do not need to be imported.

baseType:javax.lang.model.type.TypeMirror

a class or interface that the generated class inherits from. Nested classes in that type do not need to be imported, and if another class has the same name as one of those nested classes then it will need to be qualified.

decodeback to summary
pack-priv static String decode(String text, javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String pkg, javax.lang.model.type.TypeMirror baseType)
encodeback to summary
pack-priv static String encode(javax.lang.model.type.TypeMirror type)

Returns the encoding for the given type, where class names are marked by special tokens. The encoding for int will be int, but the encoding for java.util.List<java.lang.Integer> will be `java.util.List`<`java.lang.Integer`>.

encodeRawback to summary
pack-priv static String encodeRaw(javax.lang.model.type.TypeMirror type)

Like encode, except that only the raw type is encoded. So if the given type is java.util.List<java.lang.Integer> the result will be `java.util.List`.

encodeWithAnnotationsback to summary
pack-priv static String encodeWithAnnotations(javax.lang.model.type.TypeMirror type)

Encodes the given type and its type annotations. The class comment for TypeEncoder covers the details of annotation encoding.

encodeWithAnnotationsback to summary
pack-priv static String encodeWithAnnotations(javax.lang.model.type.TypeMirror type, ImmutableList<javax.lang.model.element.AnnotationMirror> extraAnnotations)

Encodes the given type and its type annotations. The class comment for TypeEncoder covers the details of annotation encoding.

Parameters
extraAnnotations:ImmutableList<javax.lang.model.element.AnnotationMirror>

additional type annotations to include with the type

encodeWithAnnotationsback to summary
pack-priv static String encodeWithAnnotations(javax.lang.model.type.TypeMirror type, ImmutableList<javax.lang.model.element.AnnotationMirror> extraAnnotations, Set<javax.lang.model.type.TypeMirror> excludedAnnotationTypes)

Encodes the given type and its type annotations. The class comment for TypeEncoder covers the details of annotation encoding.

Parameters
extraAnnotations:ImmutableList<javax.lang.model.element.AnnotationMirror>

additional type annotations to include with the type

excludedAnnotationTypes:Set<javax.lang.model.type.TypeMirror>

annotations not to include in the encoding. For example, if com.example.Nullable is in this set then the encoding will not include this @Nullable annotation.

isUnannotatedJavaLangObjectback to summary
private static boolean isUnannotatedJavaLangObject(javax.lang.model.type.TypeMirror type)
typeParametersStringback to summary
pack-priv static String typeParametersString(List<? extends javax.lang.model.element.TypeParameterElement> typeParameters)

Returns a string representing the given type parameters as they would appear in a class declaration. For example, if we have @AutoValue abstract class Foo<T extends SomeClass> then if we call TypeElement#getTypeParameters() on the representation of Foo, this method will return an encoding of <T extends SomeClass>. Likewise it will return an encoding of the angle-bracket part of:
Foo<SomeClass>
Foo<T extends Number>
Foo<E extends Enum<E>>
Foo<K, V extends Comparable<? extends K>>.

The encoding is simply that classes in the "extends" part are marked, so the examples will actually look something like this:
<`bar.baz.SomeClass`>
<T extends `java.lang.Number`>
<E extends `java.lang.Enum`<E>>
<K, V extends `java.lang.Comparable`<? extends K>>.

com.google.auto.value.processor back to summary

private Class TypeEncoder.AnnotatedEncodingTypeVisitor

extends EncodingTypeVisitor
Class Inheritance

Like EncodingTypeVisitor except that annotations on the visited type are also included in the resultant string. Class names in those annotations are also encoded using the `java.util.List` form.

Field Summary

Modifier and TypeField and Description
private final Set<javax.lang.model.type.TypeMirror>
private final Function<javax.lang.model.type.TypeMirror, List<? extends javax.lang.model.element.AnnotationMirror>>

Constructor Summary

AccessConstructor and Description
pack-priv
AnnotatedEncodingTypeVisitor(Set<javax.lang.model.type.TypeMirror> excludedAnnotationTypes, Function<javax.lang.model.type.TypeMirror, List<? extends javax.lang.model.element.AnnotationMirror>> getTypeAnnotations)

Method Summary

Modifier and TypeMethod and Description
private void
appendAnnotationsWithExclusions(List<? extends javax.lang.model.element.AnnotationMirror> annotations, StringBuilder sb)

public StringBuilder
visitArray(javax.lang.model.type.ArrayType type, StringBuilder sb)

Overrides com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor.visitArray.

The result respects the Java syntax, whereby Foo @Bar [] is an annotation on the array type itself, while @Bar Foo[] would be an annotation on the component type.

public StringBuilder
public StringBuilder
visitPrimitive(javax.lang.model.type.PrimitiveType type, StringBuilder sb)

public StringBuilder
visitTypeVariable(javax.lang.model.type.TypeVariable type, StringBuilder sb)

Inherited from com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor:
appendTypeArgumentsappendTypeNamedefaultActionvisit2visitErrorvisitWildcard

Field Detail

excludedAnnotationTypesback to summary
private final Set<javax.lang.model.type.TypeMirror> excludedAnnotationTypes
getTypeAnnotationsback to summary
private final Function<javax.lang.model.type.TypeMirror, List<? extends javax.lang.model.element.AnnotationMirror>> getTypeAnnotations

Constructor Detail

AnnotatedEncodingTypeVisitorback to summary
pack-priv AnnotatedEncodingTypeVisitor(Set<javax.lang.model.type.TypeMirror> excludedAnnotationTypes, Function<javax.lang.model.type.TypeMirror, List<? extends javax.lang.model.element.AnnotationMirror>> getTypeAnnotations)

Method Detail

appendAnnotationsWithExclusionsback to summary
private void appendAnnotationsWithExclusions(List<? extends javax.lang.model.element.AnnotationMirror> annotations, StringBuilder sb)
visitArrayback to summary
public StringBuilder visitArray(javax.lang.model.type.ArrayType type, StringBuilder sb)

Overrides com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor.visitArray.

The result respects the Java syntax, whereby Foo @Bar [] is an annotation on the array type itself, while @Bar Foo[] would be an annotation on the component type.

Annotations
@Override
visitDeclaredback to summary
public StringBuilder visitDeclared(javax.lang.model.type.DeclaredType type, StringBuilder sb)

Overrides com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor.visitDeclared.

Annotations
@Override
visitPrimitiveback to summary
public StringBuilder visitPrimitive(javax.lang.model.type.PrimitiveType type, StringBuilder sb)
Annotations
@Override
visitTypeVariableback to summary
public StringBuilder visitTypeVariable(javax.lang.model.type.TypeVariable type, StringBuilder sb)
Annotations
@Override
com.google.auto.value.processor back to summary

private Class TypeEncoder.EncodingTypeVisitor

extends javax.lang.model.util.SimpleTypeVisitor8
Class Inheritance
  • java.lang.Object
  • ⋱
  • javax.lang.model.util.SimpleTypeVisitor8
  • com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor
Known Direct Subclasses
com.google.auto.value.processor.TypeEncoder.RawEncodingTypeVisitor, com.google.auto.value.processor.TypeEncoder.AnnotatedEncodingTypeVisitor

Converts a type into a string, using standard Java syntax, except that every class name is wrapped in backquotes, like `java.util.List`.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
appendTypeArguments(javax.lang.model.type.DeclaredType type, StringBuilder sb)

pack-priv void
appendTypeName(javax.lang.model.type.DeclaredType type, StringBuilder sb)

protected StringBuilder
defaultAction(javax.lang.model.type.TypeMirror type, StringBuilder sb)

pack-priv StringBuilder
visit2(javax.lang.model.type.TypeMirror type, StringBuilder sb)

Equivalent to visit(type, sb) or type.accept(sb), except that it fixes a bug with javac versions up to JDK 8, whereby if the type is a DeclaredType then the visitor is called with a version of the type where any annotations have been lost.

public StringBuilder
visitArray(javax.lang.model.type.ArrayType type, StringBuilder sb)

public StringBuilder
visitDeclared(javax.lang.model.type.DeclaredType type, StringBuilder sb)

public StringBuilder
visitError(javax.lang.model.type.ErrorType t, StringBuilder p)

public StringBuilder
visitWildcard(javax.lang.model.type.WildcardType type, StringBuilder sb)

Constructor Detail

EncodingTypeVisitorback to summary
private EncodingTypeVisitor()

Method Detail

appendTypeArgumentsback to summary
pack-priv void appendTypeArguments(javax.lang.model.type.DeclaredType type, StringBuilder sb)
appendTypeNameback to summary
pack-priv void appendTypeName(javax.lang.model.type.DeclaredType type, StringBuilder sb)
defaultActionback to summary
protected StringBuilder defaultAction(javax.lang.model.type.TypeMirror type, StringBuilder sb)
Annotations
@Override
visit2back to summary
pack-priv StringBuilder visit2(javax.lang.model.type.TypeMirror type, StringBuilder sb)

Equivalent to visit(type, sb) or type.accept(sb), except that it fixes a bug with javac versions up to JDK 8, whereby if the type is a DeclaredType then the visitor is called with a version of the type where any annotations have been lost. We can't override visit because it is final.

visitArrayback to summary
public StringBuilder visitArray(javax.lang.model.type.ArrayType type, StringBuilder sb)
Annotations
@Override
visitDeclaredback to summary
public StringBuilder visitDeclared(javax.lang.model.type.DeclaredType type, StringBuilder sb)
Annotations
@Override
visitErrorback to summary
public StringBuilder visitError(javax.lang.model.type.ErrorType t, StringBuilder p)
Annotations
@Override
visitWildcardback to summary
public StringBuilder visitWildcard(javax.lang.model.type.WildcardType type, StringBuilder sb)
Annotations
@Override
com.google.auto.value.processor back to summary

private Class TypeEncoder.RawEncodingTypeVisitor

extends EncodingTypeVisitor
Class Inheritance

Like EncodingTypeVisitor except that type parameters are omitted from the result.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
Inherited from com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor:
appendTypeNamedefaultActionvisit2visitArrayvisitDeclaredvisitErrorvisitWildcard

Constructor Detail

RawEncodingTypeVisitorback to summary
private RawEncodingTypeVisitor()

Method Detail

appendTypeArgumentsback to summary
pack-priv void appendTypeArguments(javax.lang.model.type.DeclaredType type, StringBuilder sb)

Overrides com.google.auto.value.processor.TypeEncoder.EncodingTypeVisitor.appendTypeArguments.

Annotations
@Override
com.google.auto.value.processor back to summary

private Class TypeEncoder.TypeRewriter

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final javax.lang.model.type.TypeMirror
private final javax.lang.model.util.Elements
private final String
private final JavaScanner
private final String
private final int
private final javax.lang.model.util.Types

Constructor Summary

AccessConstructor and Description
pack-priv
TypeRewriter(String text, javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String pkg, javax.lang.model.type.TypeMirror baseType)

Method Summary

Modifier and TypeMethod and Description
private javax.lang.model.type.DeclaredType
classForName(String className)

private String
classNameAt(int token)

private void
decode(StringBuilder output, TypeSimplifier typeSimplifier, int token)

private OptionalInt
private Set<javax.lang.model.type.TypeMirror>
pack-priv String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

baseTypeback to summary
private final javax.lang.model.type.TypeMirror baseType
elementUtilsback to summary
private final javax.lang.model.util.Elements elementUtils
packageNameback to summary
private final String packageName
scannerback to summary
private final JavaScanner scanner
textback to summary
private final String text
textLengthback to summary
private final int textLength
typeUtilsback to summary
private final javax.lang.model.util.Types typeUtils

Constructor Detail

TypeRewriterback to summary
pack-priv TypeRewriter(String text, javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String pkg, javax.lang.model.type.TypeMirror baseType)

Method Detail

classForNameback to summary
private javax.lang.model.type.DeclaredType classForName(String className)
classNameAtback to summary
private String classNameAt(int token)
decodeback to summary
private void decode(StringBuilder output, TypeSimplifier typeSimplifier, int token)
findImportMarkerback to summary
private OptionalInt findImportMarker()
findReferencedClassesback to summary
private Set<javax.lang.model.type.TypeMirror> findReferencedClasses()
rewriteback to summary
pack-priv String rewrite()