public class AnnotatedTypeFactory extends 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 Elements |
elements
Utility class for working with
Element s. |
protected Map<Tree,AnnotatedTypeMirror> |
fromTreeCache
Mapping from a Tree to its annotated type; before implicits are applied,
just what the programmer wrote.
|
protected ProcessingEnvironment |
processingEnv
The processing environment to use for accessing compiler internals.
|
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<?> alias,
AnnotationMirror type)
Add the annotation clazz as an alias for the annotation type.
|
protected void |
addAliasedDeclAnnotation(Class<? extends Annotation> annotation,
Class<? extends Annotation> alias,
AnnotationMirror annotationToUse)
Add the annotation
alias as an alias for the declaration
annotation annotation , where the annotation mirror
annoationToUse will be used instead. |
@Nullable AnnotationMirror |
aliasedAnnotation(AnnotationMirror a)
Returns the canonical annotation for the passed annotation if it is
an alias of a canonical one in the framework.
|
void |
annotateImplicit(Element elt,
@Mutable AnnotatedTypeMirror type)
Adds implicit annotations to a type obtained from a
Element . |
void |
annotateImplicit(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<AnnotatedTypeMirror>> |
constructorFromUse(NewClassTree tree)
Determines the
AnnotatedTypeMirror.AnnotatedExecutableType of a constructor
invocation. |
protected static <K,V> Map<K,V> |
createLRUCache(int size)
A Utility method for creating LRU cache
|
protected Tree |
declarationFromElement(Element elt)
Gets the declaration tree for the element, if the source is available.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromClass(ClassTree tree)
Determines the annotated type of a class from its declaration.
|
AnnotatedTypeMirror |
fromElement(Element elt)
Determines the annotated type of an element.
|
AnnotatedTypeMirror.AnnotatedExecutableType |
fromElement(ExecutableElement elt) |
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromElement(TypeElement elt) |
AnnotatedTypeMirror |
fromExpression(ExpressionTree tree)
Determines the annotated type of an expression.
|
AnnotatedTypeMirror |
fromMember(Tree tree)
Determines the annotated type of a variable or method declaration.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
fromNewClass(NewClassTree tree) |
AnnotatedTypeMirror |
fromTypeTree(Tree tree)
Determines the annotated type from a type in tree form.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(ClassTree tree) |
AnnotatedTypeMirror |
getAnnotatedType(Element elt)
Determines the annotated type of an element using
fromElement(Element) . |
AnnotatedTypeMirror.AnnotatedExecutableType |
getAnnotatedType(ExecutableElement elt) |
AnnotatedTypeMirror.AnnotatedExecutableType |
getAnnotatedType(MethodTree tree) |
AnnotatedTypeMirror.AnnotatedArrayType |
getAnnotatedType(NewArrayTree tree) |
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(NewClassTree tree) |
AnnotatedTypeMirror |
getAnnotatedType(Tree tree)
Determines the annotated type of an AST node.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getAnnotatedType(TypeElement elt) |
AnnotatedTypeMirror |
getAnnotatedTypeFromTypeTree(Tree tree)
Determines the annotated type from a type in tree form.
|
List<Pair<AnnotationMirror,AnnotationMirror>> |
getAnnotationWithMetaAnnotation(Element element,
Class<? extends Annotation> metaAnnotation)
Returns a list of all annotations used to annotate this element,
which have a meta-annotation (i.e., an annotation on that annotation)
with class
metaAnnotation . |
AnnotatedTypeMirror.AnnotatedDeclaredType |
getBoxedType(AnnotatedTypeMirror.AnnotatedPrimitiveType type)
returns the annotated boxed type of the given primitive type.
|
protected ClassTree |
getCurrentClassTree(Tree tree)
Returns the current class type being visited by the visitor.
|
protected AnnotatedTypeMirror.AnnotatedDeclaredType |
getCurrentClassType(Tree tree) |
protected AnnotatedTypeMirror.AnnotatedDeclaredType |
getCurrentMethodReceiver(Tree tree)
Returns the receiver type of the current method being visited, and
returns null if the visited tree is not within a method.
|
AnnotationMirror |
getDeclAnnotation(Element elt,
Class<? extends Annotation> anno)
Returns the actual annotation mirror used to annotate this element,
whose name equals the passed annotation class, if one exists, or null otherwise.
|
Set<AnnotationMirror> |
getDeclAnnotations(Element elt)
Returns all of the actual annotation mirrors used to annotate this element
(includes stub files).
|
AnnotationTree |
getDeclAnnotationTree(MethodTree meth,
Class<? extends Annotation> anno)
Find the declaration annotation in method meth that
has type anno and return the annotation tree.
|
List<Pair<AnnotationMirror,AnnotationMirror>> |
getDeclAnnotationWithMetaAnnotation(Element element,
Class<? extends Annotation> metaAnnotation)
Returns a list of all declaration annotations used to annotate this element,
which have a meta-annotation (i.e., an annotation on that annotation)
with class
metaAnnotation . |
AnnotatedTypeMirror |
getDefaultedAnnotatedType(Tree tree)
Get the defaulted type of a variable, without considering
flow inference from the initializer expression.
|
Elements |
getElementUtils()
Accessor for the element utilities.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getEnclosingType(TypeElement element,
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.
|
TreePath |
getPath(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. |
ProcessingEnvironment |
getProcessingEnv()
Accessor for the processing environment.
|
QualifierHierarchy |
getQualifierHierarchy() |
AnnotatedTypeMirror |
getReceiverType(ExpressionTree expression)
Returns the receiver type of the expression tree, or null if it does not exist.
|
AnnotatedTypeMirror.AnnotatedDeclaredType |
getSelfType(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.
|
AnnotatedTypeMirror |
getUninferredMethodTypeArgument(AnnotatedTypeMirror.AnnotatedTypeVariable typeVar)
This method is a hack to use when a method type argument
could not be inferred automatically.
|
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(Tree tree) |
Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<AnnotatedTypeMirror>> |
methodFromUse(MethodInvocationTree tree)
Determines the type of the invoked method based on the passed method
invocation tree.
|
void |
postAsMemberOf(AnnotatedTypeMirror type,
AnnotatedTypeMirror owner,
Element element)
A callback method for the AnnotatedTypeFactory subtypes to customize
AnnotatedTypes.asMemberOf().
|
protected void |
postDirectSuperTypes(AnnotatedTypeMirror type,
List<? extends 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.
|
AnnotatedTypeMirror |
toAnnotatedType(TypeMirror t)
A convenience method that converts a
TypeMirror to an AnnotatedTypeMirror using AnnotatedTypeMirror.createType(javax.lang.model.type.TypeMirror, checkers.types.AnnotatedTypeFactory) . |
String |
toString() |
AnnotatedTypeMirror |
type(Tree node)
Determines an empty annotated type of the given tree.
|
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 final @Nullable CompilationUnitTree root
protected final ProcessingEnvironment processingEnv
protected final Types types
TypeMirror
s.protected final VisitorState visitorState
protected final QualifierHierarchy qualHierarchy
public final int uid
protected final Map<Tree,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 AnnotatedTypeMirror getAnnotatedType(Element elt)
fromElement(Element)
.elt
- the elementelt
IllegalArgumentException
- if elt
is nullfromElement(Element)
public AnnotatedTypeMirror getAnnotatedType(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 AnnotatedTypeMirror getDefaultedAnnotatedType(Tree tree)
public AnnotatedTypeMirror getAnnotatedTypeFromTypeTree(Tree tree)
tree
- the type treepublic AnnotatedTypeMirror fromElement(Element elt)
elt
- the elementpublic AnnotatedTypeMirror.AnnotatedDeclaredType fromClass(ClassTree tree)
tree
- the class declarationpublic AnnotatedTypeMirror fromMember(Tree tree)
tree
- the variable or method declarationIllegalArgumentException
- if tree
is not a method or
variable declarationpublic AnnotatedTypeMirror fromExpression(ExpressionTree tree)
tree
- an expressionpublic AnnotatedTypeMirror fromTypeTree(Tree tree)
tree
- the type treepublic void annotateImplicit(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
public void annotateImplicit(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(AnnotatedTypeMirror type, List<? extends 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 AnnotatedTypeFactorypublic void postAsMemberOf(AnnotatedTypeMirror type, AnnotatedTypeMirror owner, 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(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, Tree tree)
public final AnnotatedTypeMirror getReceiverType(ExpressionTree expression)
expression
- The expression for which to determine the receiver typepublic Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<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.
Note that "this" and "super" constructor invocations are also handled by this
method. Method constructorFromUse
is only used for a constructor invocation
in a "new" expression.tree
- the method invocation treepublic Pair<AnnotatedTypeMirror.AnnotatedExecutableType,List<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?
Note that "this" and "super" constructor invocations are handled by
method methodFromUse
. This method only handles constructor invocations
in a "new" expression.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(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(MethodTree tree)
getAnnotatedType(Tree)
public final AnnotatedTypeMirror.AnnotatedDeclaredType getAnnotatedType(TypeElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedExecutableType getAnnotatedType(ExecutableElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedDeclaredType fromElement(TypeElement elt)
getAnnotatedType(Element)
public final AnnotatedTypeMirror.AnnotatedExecutableType fromElement(ExecutableElement elt)
getAnnotatedType(Element)
public boolean isSupportedQualifier(@Nullable AnnotationMirror a)
a
- any annotationprotected void addAliasedAnnotation(Class<?> alias, AnnotationMirror type)
public @Nullable AnnotationMirror aliasedAnnotation(AnnotationMirror a)
a
- the qualifier to check for an aliasprotected void addAliasedDeclAnnotation(Class<? extends Annotation> annotation, Class<? extends Annotation> alias, AnnotationMirror annotationToUse)
alias
as an alias for the declaration
annotation annotation
, where the annotation mirror
annoationToUse
will be used instead. If multiple calls are made
with the same annotation
, then the anontationToUse
must
be the same.public final AnnotatedTypeMirror toAnnotatedType(TypeMirror t)
TypeMirror
to an AnnotatedTypeMirror
using AnnotatedTypeMirror.createType(javax.lang.model.type.TypeMirror, checkers.types.AnnotatedTypeFactory)
.t
- the TypeMirror
AnnotatedTypeMirror
that has t
as its
underlying typepublic AnnotatedTypeMirror type(Tree node)
TypeMirror
for the tree and converts that into an
AnnotatedTypeMirror
, but does not add any annotations to the
result.
Most users will want to use getAnnotatedType instead; this method
is mostly for internal use.node
- node
, without any annotationspublic QualifierHierarchy getQualifierHierarchy()
protected final Tree declarationFromElement(Element elt)
elt
- an elementprotected final ClassTree getCurrentClassTree(Tree tree)
protected final AnnotatedTypeMirror.AnnotatedDeclaredType getCurrentClassType(Tree tree)
protected final AnnotatedTypeMirror.AnnotatedDeclaredType getCurrentMethodReceiver(Tree tree)
protected final boolean isWithinConstructor(Tree tree)
public final TreePath getPath(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,V> Map<K,V> createLRUCache(int size)
size
- size of the cachepublic AnnotationTree getDeclAnnotationTree(MethodTree meth, Class<? extends Annotation> anno)
meth
- the method tree to queryanno
- the annotation class to look forpublic AnnotationMirror getDeclAnnotation(Element elt, Class<? extends Annotation> anno)
anno
- annotation classpublic Set<AnnotationMirror> getDeclAnnotations(Element elt)
element
- The element for which to determine annotations.public List<Pair<AnnotationMirror,AnnotationMirror>> getDeclAnnotationWithMetaAnnotation(Element element, Class<? extends Annotation> metaAnnotation)
metaAnnotation
.element
- The element for which to determine annotations.metaAnnotation
- The meta annotation that needs to be present.(anno, metaAnno)
where anno
is
the annotation mirror at element
, and metaAnno
is
the annotation mirror used to annotate anno
.public List<Pair<AnnotationMirror,AnnotationMirror>> getAnnotationWithMetaAnnotation(Element element, Class<? extends Annotation> metaAnnotation)
metaAnnotation
.element
- The element at which to look for annotations.metaAnnotation
- The meta annotation that needs to be present.(anno, metaAnno)
where anno
is
the annotation mirror at element
, and metaAnno
is
the annotation mirror used to annotate anno
.public AnnotatedTypeMirror getUninferredMethodTypeArgument(AnnotatedTypeMirror.AnnotatedTypeVariable typeVar)
public Elements getElementUtils()
public ProcessingEnvironment getProcessingEnv()