use of com.sun.source.util.TreePath in project checker-framework by typetools.
the class AnnotatedTypeFactory method type.
/**
* Determines an empty annotated type of the given tree. In other words, finds the {@link
* TypeMirror} for the tree and converts that into an {@link AnnotatedTypeMirror}, but does not
* add any annotations to the result.
*
* <p>Most users will want to use getAnnotatedType instead; this method is mostly for internal
* use.
*
* @param node the tree to analyze
* @return the type of {@code node}, without any annotations
*/
protected final AnnotatedTypeMirror type(Tree node) {
boolean isDeclaration = TreeUtils.isTypeDeclaration(node);
// Attempt to obtain the type via JCTree.
if (TreeUtils.typeOf(node) != null) {
AnnotatedTypeMirror result = toAnnotatedType(TreeUtils.typeOf(node), isDeclaration);
return result;
}
// Attempt to obtain the type via TreePath (slower).
TreePath path = this.getPath(node);
assert path != null : "No path or type in tree: " + node;
TypeMirror t = trees.getTypeMirror(path);
assert validType(t) : "Invalid type " + t + " for node " + t;
return toAnnotatedType(t, isDeclaration);
}
use of com.sun.source.util.TreePath in project checker-framework by typetools.
the class AnnotatedTypeFactory method getMostInnerClassOrMethod.
private final Element getMostInnerClassOrMethod(Tree tree) {
if (visitorState.getMethodTree() != null) {
return TreeUtils.elementFromDeclaration(visitorState.getMethodTree());
}
if (visitorState.getClassTree() != null) {
return TreeUtils.elementFromDeclaration(visitorState.getClassTree());
}
TreePath path = getPath(tree);
if (path == null) {
ErrorReporter.errorAbort(String.format("AnnotatedTypeFactory.getMostInnerClassOrMethod: getPath(tree)=>null%n TreePath.getPath(root, tree)=>%s\n for tree (%s) = %s%n root=%s", TreePath.getPath(root, tree), tree.getClass(), tree, root));
// dead code
return null;
}
for (Tree pathTree : path) {
if (pathTree instanceof MethodTree) {
return TreeUtils.elementFromDeclaration((MethodTree) pathTree);
} else if (pathTree instanceof ClassTree) {
return TreeUtils.elementFromDeclaration((ClassTree) pathTree);
}
}
ErrorReporter.errorAbort("AnnotatedTypeFactory.getMostInnerClassOrMethod: cannot be here!");
// dead code
return null;
}
use of com.sun.source.util.TreePath in project checker-framework by typetools.
the class Heuristics method matchParents.
/**
* Determines whether a tree has a particular set of direct parents, ignoring blocks and
* parentheses.
*
* <p>For example, to test whether an expression (specified by {@code path}) is immediately
* contained by an if statement which is immediately contained in a method, one would invoke:
*
* <pre>
* matchParents(path, Kind.IF, Kind.METHOD)
* </pre>
*
* @param path the path to match
* @param kinds the tree kinds to match against, in ascending order starting from the desired
* kind of the parent
* @return true if the tree path matches the desired kinds, skipping blocks and parentheses, for
* as many kinds as specified
*/
public static boolean matchParents(TreePath path, Tree.Kind... kinds) {
TreePath parentPath = path.getParentPath();
boolean result = true;
ArrayDeque<Tree.Kind> queue = new ArrayDeque<>(Arrays.asList(kinds));
Tree tree;
while ((tree = parentPath.getLeaf()) != null) {
if (queue.isEmpty()) {
break;
}
if (tree.getKind() == Tree.Kind.BLOCK || tree.getKind() == Tree.Kind.PARENTHESIZED) {
parentPath = parentPath.getParentPath();
continue;
}
result &= queue.removeFirst() == parentPath.getLeaf().getKind();
parentPath = parentPath.getParentPath();
}
return result;
}
use of com.sun.source.util.TreePath in project checker-framework by typetools.
the class AliasingVisitor method commonAssignmentCheck.
@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey) {
super.commonAssignmentCheck(varType, valueType, valueTree, errorKey);
// If we are visiting a pseudo-assignment, visitorLeafKind is either
// Kind.NEW_CLASS or Kind.METHOD_INVOCATION.
TreePath path = visitorState.getPath();
if (path == null) {
return;
}
Kind visitorLeafKind = path.getLeaf().getKind();
if (visitorLeafKind == Kind.NEW_CLASS || visitorLeafKind == Kind.METHOD_INVOCATION) {
// Handling pseudo-assignments
if (canBeLeaked(valueTree)) {
Kind parentKind = visitorState.getPath().getParentPath().getLeaf().getKind();
if (!varType.hasAnnotation(NonLeaked.class) && !(varType.hasAnnotation(LeakedToResult.class) && parentKind == Kind.EXPRESSION_STATEMENT)) {
checker.report(Result.failure("unique.leaked"), valueTree);
}
}
}
}
use of com.sun.source.util.TreePath in project checker-framework by typetools.
the class LockVisitor method getLockExpressions.
private List<LockExpression> getLockExpressions(boolean implicitThis, AnnotationMirror gbAnno, Tree tree) {
List<String> expressions = AnnotationUtils.getElementValueArray(gbAnno, "value", String.class, true);
if (expressions.isEmpty()) {
return Collections.emptyList();
}
TreePath currentPath = getCurrentPath();
List<Receiver> params = FlowExpressions.getParametersOfEnclosingMethod(atypeFactory, currentPath);
TypeMirror enclosingType = TreeUtils.typeOf(TreeUtils.enclosingClass(currentPath));
Receiver pseudoReceiver = FlowExpressions.internalReprOfPseudoReceiver(currentPath, enclosingType);
FlowExpressionContext exprContext = new FlowExpressionContext(pseudoReceiver, params, atypeFactory.getContext());
Receiver self;
if (implicitThis) {
self = pseudoReceiver;
} else if (TreeUtils.isExpressionTree(tree)) {
self = FlowExpressions.internalReprOf(atypeFactory, (ExpressionTree) tree);
} else {
self = new Unknown(TreeUtils.typeOf(tree));
}
List<LockExpression> lockExpressions = new ArrayList<>();
for (String expression : expressions) {
lockExpressions.add(parseExpressionString(expression, exprContext, currentPath, self));
}
return lockExpressions;
}
Aggregations