|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.sun.source.util.TreeScanner<R,P> com.sun.source.util.TreePathScanner<R,P> checkers.source.SourceVisitor<R,P> checkers.basetype.BaseTypeVisitor<R,P>
public class BaseTypeVisitor<R,P> extends SourceVisitor<R,P>
A SourceVisitor
that performs assignment and pseudo-assignment
checking, method invocation checking, and assignability checking.
This implementation uses the AnnotatedTypeFactory
implementation
provided by an associated BaseTypeChecker
; its visitor methods will
invoke this factory on parts of the AST to determine the "annotated type" of
an expression. Then, the visitor methods will check the types in assignments
and pseudo-assignments using commonAssignmentCheck(com.sun.source.tree.Tree, com.sun.source.tree.ExpressionTree, java.lang.String, P)
, which
ultimately calls the BaseTypeChecker.isSubtype(checkers.types.AnnotatedTypeMirror, checkers.types.AnnotatedTypeMirror)
method and reports
errors that violate Java's rules of assignment.
Note that since this implementation only performs assignment and
pseudo-assignment checking, other rules for custom type systems must be added
in subclasses (e.g., dereference checking in the NullnessChecker
is
implemented in the NullnessChecker
's
TreeScanner.visitMemberSelect(com.sun.source.tree.MemberSelectTree, P)
method).
This implementation does the following checks:
1. Assignment and Pseudo-Assignment Check:
It verifies that any assignment type check, using
Checker.isSubtype
method. This includes method invocation and
method overriding checks.
2. Type Validity Check:
It verifies that any user-supplied type is a valid type, using
Checker.isValidUse
method.
3. (Re-)Assignability Check:
It verifies that any assignment is valid, using
Checker.isAssignable
method.
BaseTypeChecker.isSubtype(AnnotatedTypeMirror, AnnotatedTypeMirror)
,
AnnotatedTypeFactory
Modifier and Type | Field and Description |
---|---|
protected AnnotationUtils |
annoFactory
The annotation factory to use for creating annotations. |
protected AnnotatedTypes |
annoTypes
utilities class for annotated types |
protected BaseTypeChecker |
checker
The checker corresponding to this visitor. |
protected AnnotatedTypeFactory |
plainFactory
|
protected VisitorState |
visitorState
For storing visitor state |
Fields inherited from class checkers.source.SourceVisitor |
---|
atypeFactory, elements, root, trees, types |
Constructor and Description |
---|
BaseTypeVisitor(BaseTypeChecker checker,
CompilationUnitTree root)
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkAccess(IdentifierTree node,
P p)
|
protected void |
checkArguments(List<? extends AnnotatedTypeMirror> requiredArgs,
List<? extends ExpressionTree> passedArgs,
P p)
A helper method to check that each passed argument is a subtype of the corresponding required argument, and issues "argument.invalid" error for each passed argument that not a subtype of the required one. |
protected void |
checkArrayInitialization(AnnotatedTypeMirror type,
List<? extends ExpressionTree> initializers,
P p)
|
protected void |
checkAssignability(AnnotatedTypeMirror varType,
Tree varTree)
Tests, for a re-assignment, whether the variable is assignable or not. |
protected boolean |
checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType dt,
AnnotatedTypeMirror.AnnotatedExecutableType constructor,
Tree src)
|
protected void |
checkDefaultConstructor(ClassTree node)
|
protected boolean |
checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method,
MethodInvocationTree node)
Tests whether the method can be invoked using the receiver of the 'node' method invocation, and issues a "method.invocation.invalid" if the invocation is invalid. |
protected boolean |
checkOverride(MethodTree overriderTree,
AnnotatedTypeMirror.AnnotatedDeclaredType enclosingType,
AnnotatedTypeMirror.AnnotatedExecutableType overridden,
AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType,
P p)
Checks that an overriding method's return type, parameter types, and receiver type are correct with respect to the annotations on the overridden method's return type, parameter types, and receiver type. |
protected void |
checkTypeArguments(List<? extends AnnotatedTypeMirror> typevars,
List<? extends AnnotatedTypeMirror> typeargs,
List<? extends Tree> typeargTrees,
P p)
Checks that the annotations on the type arguments supplied to a type or a method invocation are within the bounds of the type variables as declared, and issues the "generic.argument.invalid" error if they are not. |
protected void |
checkTypecastRedundancy(TypeCastTree node,
P p)
|
protected void |
checkTypecastSafety(TypeCastTree node,
P p)
|
protected void |
commonAssignmentCheck(AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree,
String errorKey,
P p)
Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it does. |
protected void |
commonAssignmentCheck(AnnotatedTypeMirror varType,
ExpressionTree valueExp,
String errorKey,
P p)
Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it does. |
protected void |
commonAssignmentCheck(Tree varTree,
ExpressionTree valueExp,
String errorKey,
P p)
Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it does. |
protected MemberSelectTree |
enclosingMemberSelect()
|
protected Tree |
enclosingStatement(Tree tree)
|
protected boolean |
isAccessAllowed(Element field,
AnnotatedTypeMirror receiver,
ExpressionTree accessTree)
|
R |
scan(Tree tree,
P p)
|
protected boolean |
shouldSkip(Element element)
Tests whether the class owner of the passed element is an unannotated class and matches the pattern specified in the checker.skipClasses property. |
protected boolean |
shouldSkip(ExpressionTree exprTree)
Tests whether the expression should not be checked because of the tree referring to unannotated classes, as specified in the checker.skipClasses property. |
void |
validateTypeOf(Tree tree)
Tests whether the tree expressed by the passed type tree is a valid type, and emits an error if that is not the case (e.g. |
R |
visitAnnotation(AnnotationTree node,
P p)
|
R |
visitAssignment(AssignmentTree node,
P p)
Performs two checks: subtyping and assignability checks, using commonAssignmentCheck(Tree, ExpressionTree, String, Object) . |
R |
visitClass(ClassTree node,
P p)
|
R |
visitCompilationUnit(CompilationUnitTree node,
P p)
Override Compilation Unit so we won't visit package names or imports |
R |
visitCompoundAssignment(CompoundAssignmentTree node,
P p)
Performs assignability check using checkAssignability(AnnotatedTypeMirror, Tree) . |
R |
visitEnhancedForLoop(EnhancedForLoopTree node,
P p)
Performs a subtype check, to test whether the node expression iterable type is a subtype of the variable type in the enhanced for loop. |
R |
visitIdentifier(IdentifierTree node,
P p)
|
R |
visitInstanceOf(InstanceOfTree node,
P p)
|
R |
visitMethod(MethodTree node,
P p)
Performs pseudo-assignment check: checks that the method obeys override and subtype rules to all overridden methods. |
R |
visitMethodInvocation(MethodInvocationTree node,
P p)
Performs a method invocation check. |
R |
visitNewArray(NewArrayTree node,
P p)
|
R |
visitNewClass(NewClassTree node,
P p)
Performs a new class invocation check. |
R |
visitParameterizedType(ParameterizedTypeTree node,
P p)
Checks that the annotations on the type arguments supplied to a type or a method invocation are within the bounds of the type variables as declared, and issues the "generic.argument.invalid" error if they are not. |
R |
visitReturn(ReturnTree node,
P p)
Checks that the type of the return expression is a subtype of the enclosing method required return type. |
R |
visitTypeCast(TypeCastTree node,
P p)
|
R |
visitUnary(UnaryTree node,
P p)
Performs assignability check using checkAssignability(AnnotatedTypeMirror, Tree) . |
R |
visitVariable(VariableTree node,
P p)
|
Methods inherited from class com.sun.source.util.TreePathScanner |
---|
getCurrentPath, scan |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final BaseTypeChecker checker
protected final AnnotationUtils annoFactory
protected final AnnotatedTypes annoTypes
protected final VisitorState visitorState
protected final AnnotatedTypeFactory plainFactory
Constructor Detail |
---|
public BaseTypeVisitor(BaseTypeChecker checker, CompilationUnitTree root)
checker
- the typechecker associated with this visitor (for
callbacks to BaseTypeChecker.isSubtype(checkers.types.AnnotatedTypeMirror, checkers.types.AnnotatedTypeMirror)
)root
- the root of the AST that this visitor operates onMethod Detail |
---|
public R scan(Tree tree, P p)
scan
in class TreePathScanner<R,P>
public R visitClass(ClassTree node, P p)
visitClass
in interface TreeVisitor<R,P>
visitClass
in class TreeScanner<R,P>
protected void checkDefaultConstructor(ClassTree node)
public R visitMethod(MethodTree node, P p)
visitMethod
in interface TreeVisitor<R,P>
visitMethod
in class TreeScanner<R,P>
public R visitVariable(VariableTree node, P p)
visitVariable
in interface TreeVisitor<R,P>
visitVariable
in class TreeScanner<R,P>
public R visitAssignment(AssignmentTree node, P p)
commonAssignmentCheck(Tree, ExpressionTree, String, Object)
.
If the subtype check fails, it issues a "assignment.invalid" error.
visitAssignment
in interface TreeVisitor<R,P>
visitAssignment
in class TreeScanner<R,P>
public R visitEnhancedForLoop(EnhancedForLoopTree node, P p)
visitEnhancedForLoop
in interface TreeVisitor<R,P>
visitEnhancedForLoop
in class TreeScanner<R,P>
public R visitMethodInvocation(MethodInvocationTree node, P p)
visitMethodInvocation
in interface TreeVisitor<R,P>
visitMethodInvocation
in class TreeScanner<R,P>
public R visitNewClass(NewClassTree node, P p)
visitNewClass
in interface TreeVisitor<R,P>
visitNewClass
in class TreeScanner<R,P>
public R visitReturn(ReturnTree node, P p)
visitReturn
in interface TreeVisitor<R,P>
visitReturn
in class TreeScanner<R,P>
public R visitUnary(UnaryTree node, P p)
checkAssignability(AnnotatedTypeMirror, Tree)
.
visitUnary
in interface TreeVisitor<R,P>
visitUnary
in class TreeScanner<R,P>
public R visitCompoundAssignment(CompoundAssignmentTree node, P p)
checkAssignability(AnnotatedTypeMirror, Tree)
.
visitCompoundAssignment
in interface TreeVisitor<R,P>
visitCompoundAssignment
in class TreeScanner<R,P>
public R visitNewArray(NewArrayTree node, P p)
visitNewArray
in interface TreeVisitor<R,P>
visitNewArray
in class TreeScanner<R,P>
public R visitParameterizedType(ParameterizedTypeTree node, P p)
visitParameterizedType
in interface TreeVisitor<R,P>
visitParameterizedType
in class TreeScanner<R,P>
protected void checkTypecastRedundancy(TypeCastTree node, P p)
protected void checkTypecastSafety(TypeCastTree node, P p)
public R visitTypeCast(TypeCastTree node, P p)
visitTypeCast
in interface TreeVisitor<R,P>
visitTypeCast
in class TreeScanner<R,P>
public R visitInstanceOf(InstanceOfTree node, P p)
visitInstanceOf
in interface TreeVisitor<R,P>
visitInstanceOf
in class TreeScanner<R,P>
protected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, String errorKey, P p)
varTree
- the AST node for the variablevalueExp
- the AST node for the valueerrorKey
- the error message to use if the check failsp
- a checker-specified parameterprotected void commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree valueExp, String errorKey, P p)
varType
- the annotated type of the variablevalueExp
- the AST node for the valueerrorKey
- the error message to use if the check failsp
- a checker-specified parameterprotected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, String errorKey, P p)
varType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueTree
- the location to use when reporting the error messageerrorKey
- the error message to use if the check failsp
- a checker-specified parameterprotected void checkArrayInitialization(AnnotatedTypeMirror type, List<? extends ExpressionTree> initializers, P p)
protected void checkTypeArguments(List<? extends AnnotatedTypeMirror> typevars, List<? extends AnnotatedTypeMirror> typeargs, List<? extends Tree> typeargTrees, P p)
typevars
- the type variables from a class or method declarationtypeargs
- the type arguments from the type or method invocationtypeargTrees
- the type arguments as trees, used for error reportingp
- protected boolean checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree node)
method
- the type of the invoked methodnode
- the method invocation nodeprotected boolean checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType dt, AnnotatedTypeMirror.AnnotatedExecutableType constructor, Tree src)
protected void checkArguments(List<? extends AnnotatedTypeMirror> requiredArgs, List<? extends ExpressionTree> passedArgs, P p)
requiredArgs
- the required typespassedArgs
- the expressions passed to the corresponding typesp
- protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedDeclaredType enclosingType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, P p)
This method returns the result of the check, but also emits error messages as a side effect.
overriderTree
- the AST node of the overriding methodenclosingType
- the declared type enclosing the overrider methodoverridden
- the type of the overridden methodoverriddenType
- the declared type enclosing the overridden methodp
- an optional parameter (as supplied to visitor methods)protected void checkAssignability(AnnotatedTypeMirror varType, Tree varTree)
varType
- the type of the variable being re-assignedvarTree
- the tree used to access the variable in the assignmentprotected MemberSelectTree enclosingMemberSelect()
protected Tree enclosingStatement(Tree tree)
public R visitIdentifier(IdentifierTree node, P p)
visitIdentifier
in interface TreeVisitor<R,P>
visitIdentifier
in class TreeScanner<R,P>
protected void checkAccess(IdentifierTree node, P p)
protected boolean isAccessAllowed(Element field, AnnotatedTypeMirror receiver, ExpressionTree accessTree)
public void validateTypeOf(Tree tree)
tree
- the AST type supplied by the userprotected final boolean shouldSkip(ExpressionTree exprTree)
checker.skipClasses
property.
It returns true if exprTree is a method invocation or a field access
to a class whose qualified name matches @{link checker.skipClasses}
expression. It also return true for conditional expressions where
the true or false expressions should be skipped.
exprTree
- any expression treeprotected final boolean shouldSkip(Element element)
checker.skipClasses
property.
element
- an elementpublic R visitAnnotation(AnnotationTree node, P p)
visitAnnotation
in interface TreeVisitor<R,P>
visitAnnotation
in class TreeScanner<R,P>
public R visitCompilationUnit(CompilationUnitTree node, P p)
visitCompilationUnit
in interface TreeVisitor<R,P>
visitCompilationUnit
in class TreeScanner<R,P>
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |