final class TypeSimplifier
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
TypeSimplifier.CastingUncheckedVisitor
Visitor that tells whether a type is erased, in the sense of
isCastingUnchecked(javax.lang.model.type.TypeMirror) . |
private static class |
TypeSimplifier.ReferencedClassTypeVisitor |
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.
|
private class |
TypeSimplifier.ToStringRawTypeVisitor |
private class |
TypeSimplifier.ToStringTypeVisitor
Visitor that produces a string representation of a type for use in generated code.
|
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,TypeSimplifier.Spelling> |
imports |
private TypeSimplifier.ToStringTypeVisitor |
toStringRawTypeVisitor |
private TypeSimplifier.ToStringTypeVisitor |
toStringTypeVisitor |
private javax.lang.model.util.Types |
typeUtils |
Constructor and Description |
---|
TypeSimplifier(javax.lang.model.util.Types typeUtils,
java.lang.String packageName,
java.util.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.
|
Modifier and Type | Method and Description |
---|---|
(package private) static java.lang.String |
actualTypeParametersString(javax.lang.model.element.TypeElement type) |
private static java.util.Set<java.lang.String> |
ambiguousNames(javax.lang.model.util.Types typeUtils,
java.util.Set<javax.lang.model.type.TypeMirror> types) |
private void |
appendTypeParameterWithBounds(java.lang.StringBuilder sb,
javax.lang.model.element.TypeParameterElement typeParameter) |
(package private) static java.lang.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 java.util.Map<java.lang.String,TypeSimplifier.Spelling> |
findImports(javax.lang.model.util.Types typeUtils,
java.lang.String packageName,
java.util.Set<javax.lang.model.type.TypeMirror> referenced,
java.util.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.
|
(package private) java.lang.String |
formalTypeParametersString(javax.lang.model.element.TypeElement type) |
(package private) 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 java.util.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. |
(package private) static java.lang.String |
packageNameOf(javax.lang.model.element.TypeElement type)
Returns the name of the package that the given type is in.
|
private static java.util.Set<javax.lang.model.type.TypeMirror> |
referencedClassTypes(javax.lang.model.util.Types typeUtil,
java.util.Set<javax.lang.model.type.TypeMirror> types)
Finds all declared types (classes and interfaces) that are referenced in the given
Set<TypeMirror> . |
(package private) static java.lang.String |
simpleNameOf(java.lang.String s) |
(package private) java.lang.String |
simplify(javax.lang.model.type.TypeMirror type)
Returns a string that can be used to refer to the given type given the imports defined by
typesToImport() . |
(package private) java.lang.String |
simplifyRaw(javax.lang.model.type.TypeMirror type)
Returns a string that can be used to refer to the given raw type given the imports defined by
typesToImport() . |
private static javax.lang.model.element.TypeElement |
topLevelType(javax.lang.model.element.TypeElement type) |
(package private) com.google.common.collect.ImmutableSortedSet<java.lang.String> |
typesToImport()
Returns the set of types to import.
|
private final javax.lang.model.util.Types typeUtils
private final java.util.Map<java.lang.String,TypeSimplifier.Spelling> imports
private final TypeSimplifier.ToStringTypeVisitor toStringTypeVisitor
private final TypeSimplifier.ToStringTypeVisitor toStringRawTypeVisitor
TypeSimplifier(javax.lang.model.util.Types typeUtils, java.lang.String packageName, java.util.Set<javax.lang.model.type.TypeMirror> types, javax.lang.model.type.TypeMirror base)
typeUtils
- the result of ProcessingEnvironment.getTypeUtils()
for the current
annotation processing environment.packageName
- 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
- the types that will be referenced.base
- 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.MissingTypeException
- if one of the input types contains an error (typically,
is undefined). This may be something like UndefinedClass
, or something more subtle
like Set<UndefinedClass<?>>
.com.google.common.collect.ImmutableSortedSet<java.lang.String> typesToImport()
java.lang.String simplify(javax.lang.model.type.TypeMirror type)
typesToImport()
.java.lang.String simplifyRaw(javax.lang.model.type.TypeMirror type)
typesToImport()
. The difference between this and simplify(javax.lang.model.type.TypeMirror)
is that the string
returned here will not include type parameters.java.lang.String formalTypeParametersString(javax.lang.model.element.TypeElement type)
static java.lang.String actualTypeParametersString(javax.lang.model.element.TypeElement type)
private void appendTypeParameterWithBounds(java.lang.StringBuilder sb, javax.lang.model.element.TypeParameterElement typeParameter)
static java.lang.String classNameOf(javax.lang.model.element.TypeElement type)
private static javax.lang.model.element.TypeElement topLevelType(javax.lang.model.element.TypeElement type)
static java.lang.String packageNameOf(javax.lang.model.element.TypeElement type)
static java.lang.String simpleNameOf(java.lang.String s)
private static java.util.Map<java.lang.String,TypeSimplifier.Spelling> findImports(javax.lang.model.util.Types typeUtils, java.lang.String packageName, java.util.Set<javax.lang.model.type.TypeMirror> referenced, java.util.Set<javax.lang.model.type.TypeMirror> defined)
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.
packageName
- 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
- The complete set of declared types (classes and interfaces) that will be
referenced in the generated code.defined
- 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)private static java.util.Set<javax.lang.model.type.TypeMirror> referencedClassTypes(javax.lang.model.util.Types typeUtil, java.util.Set<javax.lang.model.type.TypeMirror> types)
Set<TypeMirror>
. This includes classes and interfaces that appear directly in the set,
but also ones that appear in type parameters and the like. For example, if the set contains
java.util.List<? extends java.lang.Number>
then both java.util.List
and
java.lang.Number
will be in the resulting set.
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
.
private static java.util.Set<javax.lang.model.type.TypeMirror> nonPrivateDeclaredTypes(javax.lang.model.util.Types typeUtils, javax.lang.model.type.TypeMirror type)
TypeMirror
,
any class in its superclass chain, or any interface it implements.private static java.util.Set<java.lang.String> ambiguousNames(javax.lang.model.util.Types typeUtils, java.util.Set<javax.lang.model.type.TypeMirror> types)
static boolean isCastingUnchecked(javax.lang.model.type.TypeMirror type)
List<String>
produce such warnings. There will be
no warning if the type's only generic parameters are simple wildcards, as in Map<?, ?>
.