public class AnnotatedTypeFactory extends @NonNull Object
AnnotatedTypeMirror
. The methods are:
getAnnotatedType(ClassTree)
getAnnotatedType(MethodTree)
getAnnotatedType(Tree)
getAnnotatedTypeFromTypeTree(Tree)
getAnnotatedType(TypeElement)
getAnnotatedType(ExecutableElement)
getAnnotatedType(Element)
annotateImplicit(Element, AnnotatedTypeMirror)
and annotateImplicit(Tree, AnnotatedTypeMirror)
.Modifier and Type | Class and Description |
---|---|
protected static class |
AnnotatedTypeFactory.InheritedFromClassAnnotator
A singleton utility class for pulling annotations down from a class
type.
|
Modifier and Type | Field and Description |
---|---|
protected AnnotationUtils |
annotations
The factory to use for creating annotations.
|
protected AnnotatedTypes |
atypes
Utility class for manipulating annotated types.
|
protected Elements |
elements
Utility class for working with
Element s. |
protected ProcessingEnvironment |
env
The processing environment to use for accessing compiler internals.
|
protected Map<@Nullable Tree,@Mutable AnnotatedTypeMirror> |
fromTreeCache
Mapping from a Tree to its annotated type; before implicits are applied,
just what the programmer wrote.
|
protected QualifierHierarchy |
qualHierarchy
Represent the annotation relations.
|
protected @Nullable CompilationUnitTree |
root
Optional! The AST of the source file being operated on.
|
protected Trees |
trees
The
Trees instance to use for tree node path finding. |
protected Types |
types
Utility class for working with
TypeMirror s. |
int |
uid
Unique ID of the current object; for debugging purposes.
|
protected VisitorState |
visitorState
The state of the visitor.
|
Constructor and Description |
---|
AnnotatedTypeFactory(SourceChecker checker,
QualifierHierarchy qualHierarchy,
@Nullable CompilationUnitTree root)
Constructs a factory from the given
ProcessingEnvironment
instance and syntax tree root. |
Modifier and Type | Method and Description |
---|---|
protected void |
addAliasedAnnotation(Class<?> clazz,
@Nullable AnnotationMirror type)
Add the annotation clazz as an alias for the annotation type.
|
@Nullable AnnotationMirror |
aliasedAnnotation(@Nullable AnnotationMirror a)
Returns the canonical annotation for the passed annotation if it is
an alias of a canonical one in the framework.
|
protected void |
annotateImplicit(@Nullable Element elt,
@Mutable AnnotatedTypeMirror type)
Adds implicit annotations to a type obtained from a
Element . |
protected void |
annotateImplicit(@Nullable Tree tree,
@Mutable AnnotatedTypeMirror type)
Adds implicit annotations to a type obtained from a
Tree . |
protected void |
annotateInheritedFromClass(@Mutable AnnotatedTypeMirror type)
Adds annotations to the type based on the annotations from its class
type if and only if no annotations are already present on the type.
|
boolean |
canHaveAnnotatedTypeParameters()
For an annotated type parameter or wildcard (e.g.
|
Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<@Mutable AnnotatedTypeMirror>> |
constructorFromUse(NewClassTree tree)
Determines the
AnnotatedTypeMirror.AnnotatedExecutableType of a constructor
invocation. |
protected static <K extends @NonNull Object,V extends @NonNull Object> |
createLRUCache(int size)
A Utility method for creating LRU cache
|
protected @Nullable Tree |
declarationFromElement(@Nullable Element elt)
Gets the declaration tree for the element, if the source is available.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromClass(@Nullable ClassTree tree)
Determines the annotated type of a class from its declaration.
|
@Mutable AnnotatedTypeMirror |
fromElement(@Nullable Element elt)
Determines the annotated type of an element.
|
AnnotatedTypeMirror.AnnotatedExecutableType |
fromElement(@NonNull ExecutableElement elt) |
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromElement(TypeElement elt) |
@Mutable AnnotatedTypeMirror |
fromExpression(ExpressionTree tree)
Determines the annotated type of an expression.
|
@Mutable AnnotatedTypeMirror |
fromMember(@Nullable Tree tree)
Determines the annotated type of a variable or method declaration.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromNewClass(NewClassTree tree) |
@Mutable AnnotatedTypeMirror |
fromTypeTree(@Nullable Tree tree)
Determines the annotated type from a type in tree form.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(@Nullable ClassTree tree) |
@Mutable AnnotatedTypeMirror |
getAnnotatedType(@Nullable Element elt)
Determines the annotated type of an element using
fromElement(Element) . |
AnnotatedTypeMirror.AnnotatedExecutableType |
getAnnotatedType(@NonNull ExecutableElement elt) |
AnnotatedTypeMirror.AnnotatedExecutableType |
getAnnotatedType(@Nullable MethodTree tree) |
AnnotatedTypeMirror.AnnotatedArrayType |
getAnnotatedType(NewArrayTree tree) |
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(NewClassTree tree) |
@Mutable AnnotatedTypeMirror |
getAnnotatedType(@Nullable Tree tree)
Determines the annotated type of an AST node.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(TypeElement elt) |
@Mutable AnnotatedTypeMirror |
getAnnotatedTypeFromTypeTree(@Nullable Tree tree)
Determines the annotated type from a type in tree form.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getBoxedType(AnnotatedTypeMirror.AnnotatedPrimitiveType type)
returns the annotated boxed type of the given primitive type.
|
protected @Nullable ClassTree |
getCurrentClassTree(@Nullable Tree tree)
Returns the current class type being visited by the visitor.
|
protected AnnotatedTypeMirror.AnnotatedDeclaredType |
getCurrentClassType(@Nullable Tree tree) |
protected AnnotatedTypeMirror.AnnotatedDeclaredType |
getCurrentMethodReceiver(@Nullable Tree tree)
Returns the receiver type of the current method being visited, and
returns null if the visited tree is not within a method.
|
@Nullable AnnotationMirror |
getDeclAnnotation(@Nullable Element elt,
Class<? extends Annotation> anno)
Returns the actual annotation mirror used to annotate this type,
whose name equals the passed annotationName if one exists, null otherwise.
|
@Mutable AnnotatedTypeMirror |
getDefaultedAnnotatedType(@Nullable Tree tree)
Get the defaulted type of a variable, without considering
flow inference from the initializer expression.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getEnclosingType(TypeElement element,
@Nullable Tree tree)
Determine the type of the most enclosing class of the given tree that
is a subtype of the given element.
|
protected AnnotatedTypeMirror.AnnotatedDeclaredType |
getImplicitReceiverType(ExpressionTree tree)
Return the implicit receiver type of an expression tree.
|
@Nullable TreePath |
getPath(@Nullable Tree node)
Gets the path for the given
Tree under the current root by
checking from the visitor's current path, and only using
Trees.getPath(CompilationUnitTree, Tree) (which is much slower)
only if node is not found on the current path. |
QualifierHierarchy |
getQualifierHierarchy() |
@Mutable AnnotatedTypeMirror |
getReceiverType(ExpressionTree expression)
Returns the receiver type of the expression tree, or null if it does not exist.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getSelfType(@Nullable Tree tree)
Returns the type of
this in the current location, which can
be used if this has a special semantics (e.g. |
AnnotatedTypeMirror.AnnotatedPrimitiveType |
getUnboxedType(AnnotatedTypeMirror.AnnotatedDeclaredType type)
returns the annotated primitive type of the given declared type
if it is a boxed declared type.
|
VisitorState |
getVisitorState()
Returns the VisitorState instance used by the factory to infer types
|
boolean |
isAnyEnclosingThisDeref(ExpressionTree tree)
Does this expression have (the innermost or an outer) "this" as receiver?
Note that the receiver can be either explicit or implicit.
|
boolean |
isMostEnclosingThisDeref(ExpressionTree tree)
Determine whether the tree dereferences the most enclosing "this" object.
|
boolean |
isSupportedQualifier(@Nullable AnnotationMirror a)
Determines whether the given annotation is a part of the type system
under which this type factory operates.
|
protected boolean |
isWithinConstructor(@Nullable Tree tree) |
Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<@Mutable AnnotatedTypeMirror>> |
methodFromUse(MethodInvocationTree tree)
Determines the type of the invoked method based on the passed method
invocation tree.
|
protected void |
postAsMemberOf(@Mutable AnnotatedTypeMirror type,
@Mutable AnnotatedTypeMirror owner,
@Nullable Element element)
A callback method for the AnnotatedTypeFactory subtypes to customize
AnnotatedTypes.asMemberOf().
|
protected void |
postDirectSuperTypes(@Mutable AnnotatedTypeMirror type,
List<? extends @Mutable AnnotatedTypeMirror> supertypes)
A callback method for the AnnotatedTypeFactory subtypes to customize
directSuperTypes().
|
protected void |
postInit()
Actions that logically belong in the constructor, but need to run
after the subclass constructor has completed.
|
@Mutable AnnotatedTypeMirror |
toAnnotatedType(TypeMirror t)
A convenience method that converts a
TypeMirror to an AnnotatedTypeMirror using AnnotatedTypeMirror.createType(javax.lang.model.type.TypeMirror, javax.annotation.processing.ProcessingEnvironment, checkers.types.AnnotatedTypeFactory) . |
@ReadOnly String |
toString() |
List<AnnotatedTypeMirror.AnnotatedTypeVariable> |
typeVariablesFromUse(AnnotatedTypeMirror.AnnotatedDeclaredType type,
TypeElement element)
Adapt the upper bounds of the type variables of a class relative
to the type instantiation.
|
protected Collection<@Nullable AnnotationMirror> |
unify(Collection<@Nullable AnnotationMirror> c1,
Collection<@Nullable AnnotationMirror> c2)
Returns the type qualifiers that are least upper bound for c1 and c2
qualifiers.
|
protected final @Nullable CompilationUnitTree root
protected final ProcessingEnvironment env
protected final AnnotationUtils annotations
protected final Types types
TypeMirror
s.protected final AnnotatedTypes atypes
protected final VisitorState visitorState
protected final QualifierHierarchy qualHierarchy
public final int uid
protected final Map<@Nullable Tree,@Mutable AnnotatedTypeMirror> fromTreeCache
public AnnotatedTypeFactory(SourceChecker checker, QualifierHierarchy qualHierarchy, @Nullable CompilationUnitTree root)
ProcessingEnvironment
instance and syntax tree root. (These parameters are required so that
the factory may conduct the appropriate annotation-gathering analyses on
certain tree types.)
Root can be null
if the factory does not operate on trees.
A subclass must call postInit at the end of its constructor.checker
- the SourceChecker
to which this factory belongsroot
- the root of the syntax tree that this factory produces
annotated types forIllegalArgumentException
- if either argument is null
protected void postInit()
public boolean canHaveAnnotatedTypeParameters()
<@Nullable T>
, it returns
true
if the annotation should target the type parameter itself,
otherwise the annotation should target the extends clause, i.e.
the declaration should be treated as <T extends @Nullable Object>
public @Mutable AnnotatedTypeMirror getAnnotatedType(@Nullable Element elt)
fromElement(Element)
.elt
- the elementelt
IllegalArgumentException
- if elt
is nullfromElement(Element)
public @Mutable AnnotatedTypeMirror getAnnotatedType(@Nullable Tree tree)
The type is determined as follows:
tree
is a class declaration, determine its type via
fromClass(com.sun.source.tree.ClassTree)
tree
is a method or variable declaration, determine its
type via fromMember(Tree)
tree
is an ExpressionTree
, determine its type
via fromExpression(ExpressionTree)
UnsupportedOperationException
tree
- the AST nodetree
fromClass(ClassTree)
,
fromMember(Tree)
,
fromExpression(ExpressionTree)
public @Mutable AnnotatedTypeMirror getDefaultedAnnotatedType(@Nullable Tree tree)
public @Mutable AnnotatedTypeMirror getAnnotatedTypeFromTypeTree(@Nullable Tree tree)
tree
- the type treepublic @Mutable AnnotatedTypeMirror fromElement(@Nullable Element elt)
elt
- the elementpublic AnnotatedTypeMirror.AnnotatedDeclaredType fromClass(@Nullable ClassTree tree)
tree
- the class declarationpublic @Mutable AnnotatedTypeMirror fromMember(@Nullable Tree tree)
tree
- the variable or method declarationIllegalArgumentException
- if tree
is not a method or
variable declarationpublic @Mutable AnnotatedTypeMirror fromExpression(ExpressionTree tree)
tree
- an expressionpublic @Mutable AnnotatedTypeMirror fromTypeTree(@Nullable Tree tree)
tree
- the type treeprotected void annotateImplicit(@Nullable Tree tree, @Mutable AnnotatedTypeMirror type)
Tree
. By
default, this method does nothing. Subclasses should use this method to
implement implicit annotations specific to their type systems.tree
- an AST nodetype
- the type obtained from tree
protected void annotateImplicit(@Nullable Element elt, @Mutable AnnotatedTypeMirror type)
Element
. By
default, this method does nothing. Subclasses should use this method to
implement implicit annotations specific to their type systems.elt
- an elementtype
- the type obtained from elt
protected void postDirectSuperTypes(@Mutable AnnotatedTypeMirror type, List<? extends @Mutable AnnotatedTypeMirror> supertypes)
type
annotations to
supertypes
. This allows the type
and its supertypes
to have the qualifiers, e.g. the supertypes of an Immutable
type are also Immutable
.type
- the type whose supertypes are desiredsupertypes
- the supertypes as specified by the base AnnotatedTypeFactoryprotected void postAsMemberOf(@Mutable AnnotatedTypeMirror type, @Mutable AnnotatedTypeMirror owner, @Nullable Element element)
type
- the annotated type of the elementowner
- the annotated type of the receiver of the accessing treeelement
- the element of the field or methodpublic List<AnnotatedTypeMirror.AnnotatedTypeVariable> typeVariablesFromUse(AnnotatedTypeMirror.AnnotatedDeclaredType type, TypeElement element)
class C<X extends @Peer Object>then the instantiation
@Rep C<@Rep Object>is legal. The upper bounds of class C have to be adapted by the main modifier.
TODO: ensure that this method is consistently used instead of directly querying the type variables.
type
- The use of the typeelement
- The corresponding elementprotected void annotateInheritedFromClass(@Mutable AnnotatedTypeMirror type)
type
- the type for which class annotations will be inherited if
there are no annotations already presentprotected AnnotatedTypeMirror.AnnotatedDeclaredType getImplicitReceiverType(ExpressionTree tree)
tree
- The expression that might have an implicit receiver.public final boolean isMostEnclosingThisDeref(ExpressionTree tree)
tree
- The tree to check.public final boolean isAnyEnclosingThisDeref(ExpressionTree tree)
tree
- The tree to test.public AnnotatedTypeMirror.AnnotatedDeclaredType getSelfType(@Nullable Tree tree)
this
in the current location, which can
be used if this
has a special semantics (e.g. this
is non-null).
The parameter is an arbitrary tree and does not have to mention "this",
neither explicitly nor implicitly.
This method should be overridden for type-system specific behavior.
TODO: in 1.3, handle all receiver type annotations.
TODO: handle enclosing classes correctly.public AnnotatedTypeMirror.AnnotatedDeclaredType getEnclosingType(TypeElement element, @Nullable Tree tree)
public final @Mutable AnnotatedTypeMirror getReceiverType(ExpressionTree expression)
expression
- The expression for which to determine the receiver typepublic Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<@Mutable AnnotatedTypeMirror>> methodFromUse(MethodInvocationTree tree)
AnnotatedTypes.asMemberOf(AnnotatedTypeMirror, Element)
, and
customization based on receiver type should be in accordance to its
specification.
The return type is a pair of the type of the invoked method and
the (inferred) type arguments.
Note that neither the explicitly passed nor the inferred type arguments
are guaranteed to be subtypes of the corresponding upper bounds.
See method
BaseTypeVisitor.checkTypeArguments(Tree, List, List, List)
for the checks of type argument well-formedness.tree
- the method invocation treepublic Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<@Mutable AnnotatedTypeMirror>> constructorFromUse(NewClassTree tree)
AnnotatedTypeMirror.AnnotatedExecutableType
of a constructor
invocation. Note that this is different than calling
getAnnotatedType(Tree)
or
fromExpression(ExpressionTree)
on the constructor invocation;
those determine the type of the result of invoking the
constructor, which is probably an AnnotatedTypeMirror.AnnotatedDeclaredType
.
TODO: Should the result of getAnnotatedType be the return type
from the AnnotatedExecutableType computed here?tree
- the constructor invocation treepublic AnnotatedTypeMirror.AnnotatedDeclaredType fromNewClass(NewClassTree tree)
public AnnotatedTypeMirror.AnnotatedDeclaredType getBoxedType(AnnotatedTypeMirror.AnnotatedPrimitiveType type)
type
- the primitive typepublic AnnotatedTypeMirror.AnnotatedPrimitiveType getUnboxedType(AnnotatedTypeMirror.AnnotatedDeclaredType type) throws IllegalArgumentException
type
- the declared typeIllegalArgumentException
- if the type given has no unbox conversionpublic VisitorState getVisitorState()
public final AnnotatedTypeMirror.AnnotatedDeclaredType getAnnotatedType(@Nullable ClassTree tree)
getAnnotatedType(Tree)
public final AnnotatedTypeMirror.AnnotatedDeclaredType getAnnotatedType(NewClassTree tree)
getAnnotatedType(Tree)
public final AnnotatedTypeMirror.AnnotatedArrayType getAnnotatedType(NewArrayTree tree)
getAnnotatedType(Tree)
public final AnnotatedTypeMirror.AnnotatedExecutableType getAnnotatedType(@Nullable MethodTree tree)
getAnnotatedType(Tree)
public final AnnotatedTypeMirror.AnnotatedDeclaredType getAnnotatedType(TypeElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedExecutableType getAnnotatedType(@NonNull ExecutableElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedDeclaredType fromElement(TypeElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedExecutableType fromElement(@NonNull ExecutableElement elt)
getAnnotatedType(Element)
public boolean isSupportedQualifier(@Nullable AnnotationMirror a)
a
- any annotationprotected void addAliasedAnnotation(Class<?> clazz, @Nullable AnnotationMirror type)
public @Nullable AnnotationMirror aliasedAnnotation(@Nullable AnnotationMirror a)
public final @Mutable AnnotatedTypeMirror toAnnotatedType(TypeMirror t)
TypeMirror
to an AnnotatedTypeMirror
using AnnotatedTypeMirror.createType(javax.lang.model.type.TypeMirror, javax.annotation.processing.ProcessingEnvironment, checkers.types.AnnotatedTypeFactory)
.t
- the TypeMirror
AnnotatedTypeMirror
that has t
as its
underlying typeprotected Collection<@Nullable AnnotationMirror> unify(Collection<@Nullable AnnotationMirror> c1, Collection<@Nullable AnnotationMirror> c2)
QualifierHierarchy.leastUpperBounds(Collection, Collection)
.c1
- type qualifiers for the first typec2
- type qualifiers for the second typepublic QualifierHierarchy getQualifierHierarchy()
protected final @Nullable Tree declarationFromElement(@Nullable Element elt)
elt
- an elementprotected final @Nullable ClassTree getCurrentClassTree(@Nullable Tree tree)
protected final AnnotatedTypeMirror.AnnotatedDeclaredType getCurrentClassType(@Nullable Tree tree)
protected final AnnotatedTypeMirror.AnnotatedDeclaredType getCurrentMethodReceiver(@Nullable Tree tree)
public final @Nullable TreePath getPath(@Nullable Tree node)
Tree
under the current root by
checking from the visitor's current path, and only using
Trees.getPath(CompilationUnitTree, Tree)
(which is much slower)
only if node
is not found on the current path.
Note that the given Tree has to be within the current compilation unit,
otherwise null will be returned.node
- the Tree
to get the path fornode
under the current rootprotected static <K extends @NonNull Object,V extends @NonNull Object> Map<K,V> createLRUCache(int size)
size
- size of the cachepublic @Nullable AnnotationMirror getDeclAnnotation(@Nullable Element elt, Class<? extends Annotation> anno)
anno
- annotation class