aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java')
-rw-r--r--third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java189
1 files changed, 63 insertions, 126 deletions
diff --git a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java
index 5ceb5450dd..a3ae88fa31 100644
--- a/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java
+++ b/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TreeUtils.java
@@ -18,6 +18,7 @@ import javax.lang.model.util.ElementFilter;
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.ArrayAccessTree;
+import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
@@ -29,10 +30,12 @@ import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
+import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.PrimitiveTypeTree;
+import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TypeCastTree;
@@ -72,28 +75,11 @@ public final class TreeUtils {
* @return true iff tree describes a call to super
*/
public static boolean isSuperCall(MethodInvocationTree tree) {
- return isNamedMethodCall("super", tree);
- }
-
- /**
- * Checks if the method invocation is a call to this.
- *
- * @param tree
- * a tree defining a method invocation
- *
- * @return true iff tree describes a call to this
- */
- public static boolean isThisCall(MethodInvocationTree tree) {
- return isNamedMethodCall("this", tree);
-
- }
-
- protected static boolean isNamedMethodCall(String name, MethodInvocationTree tree) {
/*@Nullable*/ ExpressionTree mst = tree.getMethodSelect();
assert mst != null; /*nninvariant*/
if (mst.getKind() == Tree.Kind.IDENTIFIER ) {
- return ((IdentifierTree)mst).getName().contentEquals(name);
+ return ((IdentifierTree)mst).getName().contentEquals("super");
}
if (mst.getKind() == Tree.Kind.MEMBER_SELECT) {
@@ -104,7 +90,7 @@ public final class TreeUtils {
}
return ((IdentifierTree) selectTree.getExpression()).getName()
- .contentEquals(name);
+ .contentEquals("super");
}
return false;
@@ -133,22 +119,19 @@ public final class TreeUtils {
public static boolean isSelfAccess(final ExpressionTree tree) {
ExpressionTree tr = TreeUtils.skipParens(tree);
// If method invocation check the method select
- if (tr.getKind() == Tree.Kind.ARRAY_ACCESS) {
+ if (tr.getKind() == Tree.Kind.ARRAY_ACCESS)
return false;
- }
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION) {
tr = ((MethodInvocationTree)tree).getMethodSelect();
}
tr = TreeUtils.skipParens(tr);
- if (tr.getKind() == Tree.Kind.TYPE_CAST) {
+ if (tr.getKind() == Tree.Kind.TYPE_CAST)
tr = ((TypeCastTree)tr).getExpression();
- }
tr = TreeUtils.skipParens(tr);
- if (tr.getKind() == Tree.Kind.IDENTIFIER) {
+ if (tr.getKind() == Tree.Kind.IDENTIFIER)
return true;
- }
if (tr.getKind() == Tree.Kind.MEMBER_SELECT) {
tr = ((MemberSelectTree)tr).getExpression();
@@ -186,9 +169,8 @@ public final class TreeUtils {
while (p != null) {
Tree leaf = p.getLeaf();
assert leaf != null; /*nninvariant*/
- if (kinds.contains(leaf.getKind())) {
+ if (kinds.contains(leaf.getKind()))
return leaf;
- }
p = p.getParentPath();
}
@@ -196,7 +178,7 @@ public final class TreeUtils {
}
/**
- * Gets path to the first enclosing class tree, where class is
+ * Gets path to the the first enclosing class tree, where class is
* defined by the classTreeKinds method.
*
* @param path the path defining the tree node
@@ -207,7 +189,7 @@ public final class TreeUtils {
}
/**
- * Gets path to the first enclosing tree of the specified kind.
+ * Gets path to the the first enclosing tree of the specified kind.
*
* @param path the path defining the tree node
* @param kind the kind of the desired tree
@@ -218,7 +200,7 @@ public final class TreeUtils {
}
/**
- * Gets path to the first enclosing tree with any one of the specified kinds.
+ * Gets path to the the first enclosing tree with any one of the specified kinds.
*
* @param path the path defining the tree node
* @param kinds the set of kinds of the desired tree
@@ -230,9 +212,8 @@ public final class TreeUtils {
while (p != null) {
Tree leaf = p.getLeaf();
assert leaf != null; /*nninvariant*/
- if (kinds.contains(leaf.getKind())) {
+ if (kinds.contains(leaf.getKind()))
return p;
- }
p = p.getParentPath();
}
@@ -251,9 +232,8 @@ public final class TreeUtils {
while (p != null) {
Tree leaf = p.getLeaf();
- if (treeClass.isInstance(leaf)) {
+ if (treeClass.isInstance(leaf))
return treeClass.cast(leaf);
- }
p = p.getParentPath();
}
@@ -268,7 +248,7 @@ public final class TreeUtils {
*
* @param path the path defining the tree node
* @return the enclosing class (or interface) as given by the path, or null
- * if one does not exist
+ * if one does not exist.
*/
public static /*@Nullable*/ ClassTree enclosingClass(final /*@Nullable*/ TreePath path) {
return (ClassTree) enclosingOfKind(path, classTreeKinds());
@@ -328,11 +308,10 @@ public final class TreeUtils {
/**
* Returns the tree with the assignment context for the treePath
- * leaf node. (Does not handle pseudo-assignment of an argument to
- * a parameter or a receiver expression to a receiver.)
+ * leaf node.
*
- * The assignment context for the {@code treePath} is the leaf of its parent,
- * if the leaf is one of the following trees:
+ * The assignment context for the treepath is the most enclosing
+ * tree of type:
* <ul>
* <li>AssignmentTree </li>
* <li>CompoundAssignmentTree </li>
@@ -343,44 +322,30 @@ public final class TreeUtils {
* <li>VariableTree</li>
* </ul>
*
- * If the leaf is a ConditionalExpressionTree or ParenthesizedTree, then recur on the leaf.
- *
- * Otherwise, null is returned.
- *
- * @return the assignment context as described
+ * @param treePath
+ * @return the assignment context as described.
*/
public static Tree getAssignmentContext(final TreePath treePath) {
- TreePath parentPath = treePath.getParentPath();
-
- if (parentPath == null) {
- return null;
- }
+ TreePath path = treePath.getParentPath();
- Tree parent = parentPath.getLeaf();
- switch (parent.getKind()) {
- case PARENTHESIZED:
- case CONDITIONAL_EXPRESSION:
- return getAssignmentContext(parentPath);
- case ASSIGNMENT:
- case METHOD_INVOCATION:
- case NEW_ARRAY:
- case NEW_CLASS:
- case RETURN:
- case VARIABLE:
- return parent;
- default:
- // 11 Tree.Kinds are CompoundAssignmentTrees,
- // so use instanceof rather than listing all 11.
- if (parent instanceof CompoundAssignmentTree) {
- return parent;
- }
+ if (path == null)
return null;
- }
+ Tree node = path.getLeaf();
+ if ((node instanceof AssignmentTree) ||
+ (node instanceof CompoundAssignmentTree) ||
+ (node instanceof MethodInvocationTree) ||
+ (node instanceof NewArrayTree) ||
+ (node instanceof NewClassTree) ||
+ (node instanceof ReturnTree) ||
+ (node instanceof VariableTree))
+ return node;
+ return null;
}
/**
* Gets the element for a class corresponding to a declaration.
*
+ * @param node
* @return the element for the given class
*/
public static final TypeElement elementFromDeclaration(ClassTree node) {
@@ -391,6 +356,7 @@ public final class TreeUtils {
/**
* Gets the element for a method corresponding to a declaration.
*
+ * @param node
* @return the element for the given method
*/
public static final ExecutableElement elementFromDeclaration(MethodTree node) {
@@ -401,6 +367,7 @@ public final class TreeUtils {
/**
* Gets the element for a variable corresponding to its declaration.
*
+ * @param node
* @return the element for the given variable
*/
public static final VariableElement elementFromDeclaration(VariableTree node) {
@@ -437,7 +404,7 @@ public final class TreeUtils {
* Determine whether the given ExpressionTree has an underlying element.
*
* @param node the ExpressionTree to test
- * @return whether the tree refers to an identifier, member select, or method invocation
+ * @return whether the tree refers to an identifier, member select, or method invocation.
*/
public static final boolean isUseOfElement(ExpressionTree node) {
node = TreeUtils.skipParens(node);
@@ -457,11 +424,10 @@ public final class TreeUtils {
*/
public static final Name methodName(MethodInvocationTree node) {
ExpressionTree expr = node.getMethodSelect();
- if (expr.getKind() == Tree.Kind.IDENTIFIER) {
+ if (expr.getKind() == Tree.Kind.IDENTIFIER)
return ((IdentifierTree)expr).getName();
- } else if (expr.getKind() == Tree.Kind.MEMBER_SELECT) {
+ else if (expr.getKind() == Tree.Kind.MEMBER_SELECT)
return ((MemberSelectTree)expr).getIdentifier();
- }
ErrorReporter.errorAbort("TreeUtils.methodName: cannot be here: " + node);
return null; // dead code
}
@@ -490,11 +456,10 @@ public final class TreeUtils {
Tree first;
if (tree.getKind() == Tree.Kind.BLOCK) {
BlockTree block = (BlockTree)tree;
- if (block.getStatements().isEmpty()) {
+ if (block.getStatements().isEmpty())
first = block;
- } else {
+ else
first = block.getStatements().iterator().next();
- }
} else {
first = tree;
}
@@ -504,8 +469,8 @@ public final class TreeUtils {
/**
* Determine whether the given class contains an explicit constructor.
*
- * @param node a class tree
- * @return true, iff there is an explicit constructor
+ * @param node A class tree.
+ * @return True, iff there is an explicit constructor.
*/
public static boolean hasExplicitConstructor(ClassTree node) {
TypeElement elem = TreeUtils.elementFromDeclaration(node);
@@ -567,9 +532,8 @@ public final class TreeUtils {
*/
public static boolean isCompileTimeString(ExpressionTree node) {
ExpressionTree tree = TreeUtils.skipParens(node);
- if (tree instanceof LiteralTree) {
+ if (tree instanceof LiteralTree)
return true;
- }
if (TreeUtils.isUseOfElement(tree)) {
Element elt = TreeUtils.elementFromUse(tree);
@@ -687,9 +651,8 @@ public final class TreeUtils {
* of any method that overrides that one.
*/
public static boolean isMethodInvocation(Tree tree, ExecutableElement method, ProcessingEnvironment env) {
- if (!(tree instanceof MethodInvocationTree)) {
+ if (!(tree instanceof MethodInvocationTree))
return false;
- }
MethodInvocationTree methInvok = (MethodInvocationTree)tree;
ExecutableElement invoked = TreeUtils.elementFromUse(methInvok);
return isMethod(invoked, method, env);
@@ -724,8 +687,10 @@ public final class TreeUtils {
* Determine whether the given expression is either "this" or an outer
* "C.this".
*
- * <p>
* TODO: Should this also handle "super"?
+ *
+ * @param tree
+ * @return
*/
public static final boolean isExplicitThisDereference(ExpressionTree tree) {
if (tree.getKind() == Tree.Kind.IDENTIFIER
@@ -747,24 +712,7 @@ public final class TreeUtils {
}
/**
- * Determine whether {@code tree} is a class literal, such
- * as
- *
- * <pre>
- * <em>Object</em> . <em>class</em>
- * </pre>
- *
- * @return true iff if tree is a class literal
- */
- public static boolean isClassLiteral(Tree tree) {
- if (tree.getKind() != Tree.Kind.MEMBER_SELECT) {
- return false;
- }
- return "class".equals(((MemberSelectTree) tree).getIdentifier().toString());
- }
-
- /**
- * Determine whether {@code tree} is a field access expressions, such
+ * Determine whether <code>tree</code> is a field access expressions, such
* as
*
* <pre>
@@ -773,7 +721,7 @@ public final class TreeUtils {
* </pre>
*
* @return true iff if tree is a field access expression (implicit or
- * explicit)
+ * explicit).
*/
public static boolean isFieldAccess(Tree tree) {
if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
@@ -792,11 +740,11 @@ public final class TreeUtils {
}
/**
- * Compute the name of the field that the field access {@code tree}
- * accesses. Requires {@code tree} to be a field access, as determined
- * by {@code isFieldAccess}.
+ * Compute the name of the field that the field access <code>tree</code>
+ * accesses. Requires <code>tree</code> to be a field access, as determined
+ * by <code>isFieldAccess</code>.
*
- * @return the name of the field accessed by {@code tree}.
+ * @return The name of the field accessed by <code>tree</code>.
*/
public static String getFieldName(Tree tree) {
assert isFieldAccess(tree);
@@ -810,7 +758,7 @@ public final class TreeUtils {
}
/**
- * Determine whether {@code tree} refers to a method element, such
+ * Determine whether <code>tree</code> refers to a method element, such
* as
*
* <pre>
@@ -819,7 +767,7 @@ public final class TreeUtils {
* </pre>
*
* @return true iff if tree is a method access expression (implicit or
- * explicit)
+ * explicit).
*/
public static boolean isMethodAccess(Tree tree) {
if (tree.getKind().equals(Tree.Kind.MEMBER_SELECT)) {
@@ -844,11 +792,11 @@ public final class TreeUtils {
}
/**
- * Compute the name of the method that the method access {@code tree}
- * accesses. Requires {@code tree} to be a method access, as determined
- * by {@code isMethodAccess}.
+ * Compute the name of the method that the method access <code>tree</code>
+ * accesses. Requires <code>tree</code> to be a method access, as determined
+ * by <code>isMethodAccess</code>.
*
- * @return the name of the method accessed by {@code tree}.
+ * @return The name of the method accessed by <code>tree</code>.
*/
public static String getMethodName(Tree tree) {
assert isMethodAccess(tree);
@@ -893,9 +841,9 @@ public final class TreeUtils {
/**
* Returns the VariableElement for a field declaration.
*
- * @param typeName the class where the field is declared
- * @param fieldName the name of the field
- * @param env the processing environment
+ * @param typeName the class where the field is declared.
+ * @param fieldName the name of the field.
+ * @param env the processing environment.
* @return the VariableElement for typeName.fieldName
*/
public static VariableElement getField(String typeName, String fieldName, ProcessingEnvironment env) {
@@ -913,7 +861,7 @@ public final class TreeUtils {
*
* TODO: is there a nicer way than an instanceof?
*
- * @param tree the Tree to test
+ * @param tree the Tree to test.
* @return whether the tree is an ExpressionTree
*/
public static boolean isExpressionTree(Tree tree) {
@@ -953,15 +901,4 @@ public final class TreeUtils {
return false;
}
}
-
- /**
- * @see Object#getClass()
- * @return true iff invocationTree is an instance of getClass()
- */
- public static boolean isGetClassInvocation(MethodInvocationTree invocationTree) {
- final Element declarationElement = elementFromUse(invocationTree);
- String ownerName = ElementUtils.getQualifiedClassName(declarationElement.getEnclosingElement()).toString();
- return ownerName.equals("java.lang.Object")
- && declarationElement.getSimpleName().toString().equals("getClass");
- }
}