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

pack-priv final Class TypeSimplifier

extends Object
Class Inheritance
Static Imports
java.util.stream.Collectors.joining, .Collectors.toCollection, javax.lang.model.element.Modifier.PRIVATE

Takes a set of types and a package and determines which of those types can be imported, and how to spell any of the types in the set given those imports.
Author
[email protected] (Éamonn McManus)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TypeSimplifier.Spelling

The spelling that should be used to refer to a given class, and an indication of whether it should be imported.

Field Summary

Modifier and TypeField and Description
private static final javax.lang.model.type.TypeVisitor<Boolean, Void>
private final Map<String, TypeSimplifier.Spelling>

Constructor Summary

AccessConstructor and Description
pack-priv
TypeSimplifier(javax.lang.model.util.Elements
the result of ProcessingEnvironment.getElementUtils() for the current annotation processing environment.
elementUtils
,
javax.lang.model.util.Types
the result of ProcessingEnvironment.getTypeUtils() for the current annotation processing environment.
typeUtils
,
String
the name of the package from which classes will be referenced. Classes that are in the same package do not need to be imported.
packageName
,
Set<javax.lang.model.type.TypeMirror>
the types that will be referenced.
types
,
javax.lang.model.type.TypeMirror
a base class that the class containing the references will extend. This is needed because nested classes in that class or one of its ancestors are in scope in the generated subclass, so a reference to another class with the same name as one of them is ambiguous.
base
)

Makes a new simplifier for the given package and set of types.

Method Summary

Modifier and TypeMethod and Description
pack-priv static String
actualTypeParametersString(javax.lang.model.element.TypeElement type)

private static Set<String>
ambiguousNames(javax.lang.model.util.Types typeUtils, Set<javax.lang.model.type.TypeMirror> types)

pack-priv static String
classNameOf(javax.lang.model.element.TypeElement type)

Returns the name of the given type, including any enclosing types but not the package.

private static Map<String, TypeSimplifier.Spelling>

Returns:

a map where the keys are fully-qualified types and the corresponding values indicate whether the type should be imported, and how the type should be spelled in the source code.
findImports
(javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String
The name of the package where the class containing these references is defined. Other classes within the same package do not need to be imported.
codePackageName
,
Set<javax.lang.model.type.TypeMirror>
The complete set of declared types (classes and interfaces) that will be referenced in the generated code.
referenced
,
Set<javax.lang.model.type.TypeMirror>
The complete set of declared types (classes and interfaces) that are defined within the scope of the generated class (i.e. nested somewhere in its superclass chain, or in its interface set)
defined
)

Given a set of referenced types, works out which of them should be imported and what the resulting spelling of each one is.

pack-priv static boolean
isCastingUnchecked(javax.lang.model.type.TypeMirror type)

Returns true if casting to the given type will elicit an unchecked warning from the compiler.

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

private static String
javaLangSpelling(javax.lang.model.util.Elements elementUtils, String codePackageName, javax.lang.model.element.TypeElement typeElement)

Handles the tricky case where the class being referred to is in java.lang, but the package of the referring code contains another class of the same name.

private static Set<javax.lang.model.type.TypeMirror>
nonPrivateDeclaredTypes(javax.lang.model.util.Types typeUtils, javax.lang.model.type.TypeMirror type)

Finds all types that are declared with non private visibility by the given TypeMirror, any class in its superclass chain, or any interface it implements.

pack-priv static String
packageNameOf(javax.lang.model.element.TypeElement type)

Returns the name of the package that the given type is in. If the type is in the default (unnamed) package then the name is the empty string.

pack-priv static String
pack-priv String
simplifiedClassName(javax.lang.model.type.DeclaredType type)

private static javax.lang.model.element.TypeElement
topLevelType(javax.lang.model.element.TypeElement type)

private static Set<javax.lang.model.type.TypeMirror>
topLevelTypes(javax.lang.model.util.Types typeUtil, Set<javax.lang.model.type.TypeMirror> types)

Finds the top-level types for all the declared types (classes and interfaces) in the given Set<TypeMirror>.

pack-priv ImmutableSortedSet<String>
typesToImport()

Returns the set of types to import.

private static boolean
uncheckedTypeArgument(javax.lang.model.type.TypeMirror arg)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CASTING_UNCHECKED_VISITORback to summary
private static final javax.lang.model.type.TypeVisitor<Boolean, Void> CASTING_UNCHECKED_VISITOR
importsback to summary
private final Map<String, TypeSimplifier.Spelling> imports

Constructor Detail

TypeSimplifierback to summary
pack-priv TypeSimplifier(javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String packageName, Set<javax.lang.model.type.TypeMirror> types, javax.lang.model.type.TypeMirror base)

Makes a new simplifier for the given package and set of types.

Parameters
elementUtils:javax.lang.model.util.Elements

the result of ProcessingEnvironment.getElementUtils() for the current annotation processing environment.

typeUtils:javax.lang.model.util.Types

the result of ProcessingEnvironment.getTypeUtils() for the current annotation processing environment.

packageName:String

the name of the package from which classes will be referenced. Classes that are in the same package do not need to be imported.

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

the types that will be referenced.

base:javax.lang.model.type.TypeMirror

a base class that the class containing the references will extend. This is needed because nested classes in that class or one of its ancestors are in scope in the generated subclass, so a reference to another class with the same name as one of them is ambiguous.

Exceptions
MissingTypeException:
if one of the input types contains an error (typically, is undefined).

Method Detail

actualTypeParametersStringback to summary
pack-priv static String actualTypeParametersString(javax.lang.model.element.TypeElement type)
ambiguousNamesback to summary
private static Set<String> ambiguousNames(javax.lang.model.util.Types typeUtils, Set<javax.lang.model.type.TypeMirror> types)
classNameOfback to summary
pack-priv static String classNameOf(javax.lang.model.element.TypeElement type)

Returns the name of the given type, including any enclosing types but not the package.

findImportsback to summary
private static Map<String, TypeSimplifier.Spelling> findImports(javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, String codePackageName, Set<javax.lang.model.type.TypeMirror> referenced, Set<javax.lang.model.type.TypeMirror> defined)

Given a set of referenced types, works out which of them should be imported and what the resulting spelling of each one is.

This method operates on a Set<TypeMirror> rather than just a Set<String> because it is not strictly possible to determine what part of a fully-qualified type name is the package and what part is the top-level class. For example, java.util.Map.Entry is a class called Map.Entry in a package called java.util assuming Java conventions are being followed, but it could theoretically also be a class called Entry in a package called java.util.Map. Since we are operating as part of the compiler, our goal should be complete correctness, and the only way to achieve that is to operate on the real representations of types.

Parameters
codePackageName:String

The name of the package where the class containing these references is defined. Other classes within the same package do not need to be imported.

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

The complete set of declared types (classes and interfaces) that will be referenced in the generated code.

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

The complete set of declared types (classes and interfaces) that are defined within the scope of the generated class (i.e. nested somewhere in its superclass chain, or in its interface set)

Returns:Map<String, TypeSimplifier.Spelling>

a map where the keys are fully-qualified types and the corresponding values indicate whether the type should be imported, and how the type should be spelled in the source code.

isCastingUncheckedback to summary
pack-priv static boolean isCastingUnchecked(javax.lang.model.type.TypeMirror type)

Returns true if casting to the given type will elicit an unchecked warning from the compiler. Only generic types such as List<String> produce such warnings. There will be no warning if the type's only generic parameters are simple wildcards, as in Map<?, ?>.

isJavaLangObjectback to summary
private static boolean isJavaLangObject(javax.lang.model.type.TypeMirror type)
javaLangSpellingback to summary
private static String javaLangSpelling(javax.lang.model.util.Elements elementUtils, String codePackageName, javax.lang.model.element.TypeElement typeElement)

Handles the tricky case where the class being referred to is in java.lang, but the package of the referring code contains another class of the same name. For example, if the current package is foo.bar and there is a foo.bar.Compiler, then we will refer to java.lang.Compiler by its full name. The plain name Compiler would reference foo.bar.Compiler in this case. We need to write java.lang.Compiler even if the other Compiler class is not being considered here, so the ambiguousNames logic is not enough. We have to look to see if the class exists.

nonPrivateDeclaredTypesback to summary
private static Set<javax.lang.model.type.TypeMirror> nonPrivateDeclaredTypes(javax.lang.model.util.Types typeUtils, javax.lang.model.type.TypeMirror type)

Finds all types that are declared with non private visibility by the given TypeMirror, any class in its superclass chain, or any interface it implements.

packageNameOfback to summary
pack-priv static String packageNameOf(javax.lang.model.element.TypeElement type)

Returns the name of the package that the given type is in. If the type is in the default (unnamed) package then the name is the empty string.

simpleNameOfback to summary
pack-priv static String simpleNameOf(String s)
simplifiedClassNameback to summary
pack-priv String simplifiedClassName(javax.lang.model.type.DeclaredType type)
topLevelTypeback to summary
private static javax.lang.model.element.TypeElement topLevelType(javax.lang.model.element.TypeElement type)
topLevelTypesback to summary
private static Set<javax.lang.model.type.TypeMirror> topLevelTypes(javax.lang.model.util.Types typeUtil, Set<javax.lang.model.type.TypeMirror> types)

Finds the top-level types for all the declared types (classes and interfaces) in the given Set<TypeMirror>.

The returned set contains only top-level types. If we reference java.util.Map.Entry then the returned set will contain java.util.Map. This is because we want to write Map.Entry everywhere rather than Entry.

typesToImportback to summary
pack-priv ImmutableSortedSet<String> typesToImport()

Returns the set of types to import. We import every type that is neither in java.lang nor in the package containing the AutoValue class, provided that the result refers to the type unambiguously. For example, if there is a property of type java.util.Map.Entry then we will import java.util.Map.Entry and refer to the property as Entry. We could also import just java.util.Map in this case and refer to Map.Entry, but currently we never do that.

uncheckedTypeArgumentback to summary
private static boolean uncheckedTypeArgument(javax.lang.model.type.TypeMirror arg)
com.google.auto.value.processor back to summary

private Class TypeSimplifier.Spelling

extends Object
Class Inheritance

The spelling that should be used to refer to a given class, and an indication of whether it should be imported.

Field Summary

Modifier and TypeField and Description
pack-priv final boolean
pack-priv final String

Constructor Summary

AccessConstructor and Description
pack-priv
Spelling(String spelling, boolean importIt)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

importItback to summary
pack-priv final boolean importIt
spellingback to summary
pack-priv final String spelling

Constructor Detail

Spellingback to summary
pack-priv Spelling(String spelling, boolean importIt)