Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ private static void convertInsertion(
int i = LocalVariableScanner.indexOfVarTree(path, varTree, rec.varName);
int m = methTree.getStartPosition();
int a = varTree.getStartPosition();
int b = varTree.getEndPosition(tree.endPositions);
int b = TreePathUtil.getEndPosition(varTree, tree);
LocalLocation loc = new LocalLocation(i, a - m, b - a);
decl = meth.body.locals.getVivify(loc);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private IPair<ASTRecord, Integer> getBaseTypePosition(JCTree t) {
exp = jfa.getExpression();
if (jfa.sym.isStatic()) {
return pathAndPos(
exp, getFirstInstanceAfter('.', exp.getEndPosition(tree.endPositions)) + 1);
exp, getFirstInstanceAfter('.', TreePathUtil.getEndPosition(exp, tree)) + 1);
}
} while (exp instanceof JCFieldAccess
&& ((JCFieldAccess) exp).sym.getKind() != ElementKind.PACKAGE);
Expand All @@ -292,7 +292,7 @@ private IPair<ASTRecord, Integer> getBaseTypePosition(JCTree t) {
t = exp;
}
return pathAndPos(
t, getFirstInstanceAfter('.', t.getEndPosition(tree.endPositions)) + 1);
t, getFirstInstanceAfter('.', TreePathUtil.getEndPosition(t, tree)) + 1);
case ARRAY_TYPE:
t = ((JCArrayTypeTree) t).elemtype;
break;
Expand Down Expand Up @@ -395,11 +395,11 @@ public IPair<ASTRecord, Integer> visitIdentifier(IdentifierTree node, Insertion
if (n < getDimsSize((JCExpression) parent)) { // else n == #dims
i =
getNthInstanceInRange(
'[', i, ((JCNewArray) parent).getEndPosition(tree.endPositions), n + 1);
'[', i, TreePathUtil.getEndPosition(((JCNewArray) parent), tree), n + 1);
}
}
if (i == null) {
i = jcnode.getEndPosition(tree.endPositions);
i = TreePathUtil.getEndPosition(jcnode, tree);
}
} else if (parent.getKind() == Tree.Kind.NEW_CLASS) { // NewClassTree)
dbug.debug("TypePositionFinder.visitIdentifier: recognized class%n");
Expand All @@ -426,7 +426,7 @@ public IPair<ASTRecord, Integer> visitIdentifier(IdentifierTree node, Insertion
public IPair<ASTRecord, Integer> visitMemberSelect(MemberSelectTree node, Insertion ins) {
dbug.debug("TypePositionFinder.visitMemberSelect(%s)%n", node);
JCFieldAccess raw = (JCFieldAccess) node;
return IPair.of(astRecord(node), raw.getEndPosition(tree.endPositions) - raw.name.length());
return IPair.of(astRecord(node), TreePathUtil.getEndPosition(raw, tree) - raw.name.length());
}

@Override
Expand Down Expand Up @@ -514,7 +514,7 @@ public IPair<ASTRecord, Integer> visitArrayType(ArrayTypeTree node, Insertion in
int largestLevels = arrayLevels(largest);
int levels = arrayLevels(node);
int start = arrayContentType(att).getPreferredPosition() + 1;
int end = att.getEndPosition(tree.endPositions);
int end = TreePathUtil.getEndPosition(att, tree);
int pos = arrayInsertPos(start, end);

dbug.debug(" levels=%d largestLevels=%d%n", levels, largestLevels);
Expand Down Expand Up @@ -633,7 +633,7 @@ public IPair<ASTRecord, Integer> visitNewArray(NewArrayTree node, Insertion ins)
dbug.debug("TypePositionFinder.visitNewArray%n");
JCNewArray na = (JCNewArray) node;
GenericArrayLocationCriterion galc = ins.getCriteria().getGenericArrayLocation();
ASTRecord rec = ASTIndex.indexOf(tree).get(node);
ASTRecord rec = astRecord(node);
ASTPath astPath = ins.getCriteria().getASTPath();
String childSelector = null;
// Invariant: na.dims.size() == 0 or na.elems == null (but not both)
Expand Down Expand Up @@ -767,7 +767,7 @@ public IPair<ASTRecord, Integer> visitNewArray(NewArrayTree node, Insertion ins)
}
if (na.dims.size() != 0) {
int startPos = na.getStartPosition();
int endPos = na.getEndPosition(tree.endPositions);
int endPos = TreePathUtil.getEndPosition(na, tree);
int pos = getNthInstanceInRange('[', startPos, endPos, dim + 1);
return IPair.of(rec.replacePath(astPath), pos);
}
Expand Down Expand Up @@ -1219,7 +1219,7 @@ && wildcardLast(i.getCriteria().getGenericArrayLocation().getLocation()))) {
pos = ((JCExpression) bound).getStartPosition();
((AnnotationInsertion) i).setGenerateBound(true);
} else {
int limit = ((JCTree) parent(node)).getEndPosition(tree.endPositions);
int limit = TreePathUtil.getEndPosition(parent(node), tree);
Integer nextpos1 = getNthInstanceInRange(',', pos + 1, limit, 1);
Integer nextpos2 = getNthInstanceInRange('>', pos + 1, limit, 1);
pos = (nextpos1 != Position.NOPOS && nextpos1 < nextpos2) ? nextpos1 : nextpos2;
Expand All @@ -1238,7 +1238,7 @@ && wildcardLast(i.getCriteria().getGenericArrayLocation().getLocation()))) {
}
} else if (i.getKind() == Insertion.Kind.CLOSE_PARENTHESIS) {
JCTree jcTree = (JCTree) node;
pos = jcTree.getEndPosition(tree.endPositions);
pos = TreePathUtil.getEndPosition(jcTree, tree);
} else {
boolean typeScan = true;
if (node.getKind() == Tree.Kind.METHOD) { // MethodTree
Expand All @@ -1261,7 +1261,7 @@ && wildcardLast(i.getCriteria().getGenericArrayLocation().getLocation()))) {
&& i.getKind() == Insertion.Kind.CONSTRUCTOR
&& (((JCMethodDecl) node).mods.flags & Flags.GENERATEDCONSTR) != 0) {
Tree parent = path.getParentPath().getLeaf();
pos = ((JCClassDecl) parent).getEndPosition(tree.endPositions) - 1;
pos = TreePathUtil.getEndPosition(parent, tree) - 1;
insertRecord = null; // TODO
} else {
// looking for the declaration
Expand Down Expand Up @@ -1360,8 +1360,7 @@ Integer findPositionByASTPath(ASTPath astPath, TreePath path, Insertion i) {
// in the source tree. For example, a receiver annotation
// matches on the method and inserts on the (possibly newly
// created) receiver.
Map<Tree, ASTRecord> astIndex = ASTIndex.indexOf(tree);
ASTRecord insertRecord = astIndex.get(node);
ASTRecord insertRecord = astRecord(node);
dbug.debug("TreeFinder.scan: node=%s%n criteria=%s%n", node, i.getCriteria());

if (TreePathUtil.hasClassKind(node)
Expand All @@ -1379,7 +1378,7 @@ Integer findPositionByASTPath(ASTPath astPath, TreePath path, Insertion i) {
}
Tree parent = path.getParentPath().getLeaf();
insertRecord = insertRecord.extend(Tree.Kind.METHOD, ASTPath.PARAMETER, -1);
pos = ((JCTree) parent).getEndPosition(tree.endPositions) - 1;
pos = TreePathUtil.getEndPosition(parent, tree) - 1;
} else if (node.getKind() == Tree.Kind.METHOD && entry.childSelectorIs(ASTPath.TYPE)) {
JCMethodDecl jcnode = (JCMethodDecl) node;
Tree returnType = jcnode.getReturnType();
Expand Down Expand Up @@ -1419,7 +1418,7 @@ Integer findPositionByASTPath(ASTPath astPath, TreePath path, Insertion i) {
pos = ((JCExpression) bound).getStartPosition();
((AnnotationInsertion) i).setGenerateBound(true);
} else {
int limit = ((JCTree) parent(node)).getEndPosition(tree.endPositions);
int limit = TreePathUtil.getEndPosition(parent(node), tree);
Integer nextpos1 = getNthInstanceInRange(',', pos + 1, limit, 1);
Integer nextpos2 = getNthInstanceInRange('>', pos + 1, limit, 1);
pos = (nextpos1 != Position.NOPOS && nextpos1 < nextpos2) ? nextpos1 : nextpos2;
Expand Down Expand Up @@ -1462,7 +1461,7 @@ Integer findPositionByASTPath(ASTPath astPath, TreePath path, Insertion i) {
}
jcTree = (JCTree) node;
}
pos = jcTree.getEndPosition(tree.endPositions);
pos = TreePathUtil.getEndPosition(jcTree, tree);
} else {
boolean typeScan = true;
if (node.getKind() == Tree.Kind.METHOD) { // MethodTree
Expand All @@ -1483,7 +1482,7 @@ Integer findPositionByASTPath(ASTPath astPath, TreePath path, Insertion i) {
&& i.getKind() == Insertion.Kind.CONSTRUCTOR
&& (((JCMethodDecl) node).mods.flags & Flags.GENERATEDCONSTR) != 0) {
Tree parent = path.getParentPath().getLeaf();
pos = ((JCClassDecl) parent).getEndPosition(tree.endPositions) - 1;
pos = TreePathUtil.getEndPosition(parent, tree) - 1;
insertRecord = null; // TODO
} else {
// looking for the declaration
Expand Down Expand Up @@ -1514,7 +1513,7 @@ private Integer implicitClassBoundPosition(JCClassDecl cd, Insertion i) {
String name = cd.getSimpleName().toString();
if (cd.typarams == null || cd.typarams.isEmpty()) {
int start = cd.getStartPosition();
int offset = Math.max(start, mods.getEndPosition(tree.endPositions) + 1);
int offset = Math.max(start, TreePathUtil.getEndPosition(mods, tree) + 1);
String s = cd.toString().substring(offset - start);
Pattern p =
Pattern.compile(
Expand All @@ -1532,7 +1531,7 @@ private Integer implicitClassBoundPosition(JCClassDecl cd, Insertion i) {
pos = offset + m.end() - 1;
} else { // generic class
JCTypeParameter param = cd.typarams.get(cd.typarams.length() - 1);
int start = param.getEndPosition(tree.endPositions);
int start = TreePathUtil.getEndPosition(param, tree);
pos = getFirstInstanceAfter('>', start) + 1;
}
((AnnotationInsertion) i).setGenerateExtends(true);
Expand All @@ -1556,13 +1555,13 @@ private int findMethodName(JCMethodDecl node) {
return Position.NOPOS;
}
int nodeStart = node.getStartPosition();
int nodeEnd = node.getEndPosition(tree.endPositions);
int nodeEnd = TreePathUtil.getEndPosition(node, tree);
int nodeLength = nodeEnd - nodeStart;
int modsLength =
mods.getEndPosition(tree.endPositions)
TreePathUtil.getEndPosition(mods, tree)
- mods.getStartPosition(); // can't trust string length!
int bodyLength =
body == null ? 1 : body.getEndPosition(tree.endPositions) - body.getStartPosition();
body == null ? 1 : TreePathUtil.getEndPosition(body, tree) - body.getStartPosition();
int start = nodeStart + modsLength;
int end = nodeStart + nodeLength - bodyLength;
int angle = name.lastIndexOf('>'); // check for type params
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.checkerframework.afu.annotator.scanner;

import com.google.common.base.Throwables;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.JCTree;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;

/** Utility methods relating to TreePaths. */
public class TreePathUtil {
Expand Down Expand Up @@ -227,4 +233,63 @@ public static String getBinaryName(TreePath path) {
}
throw new Error("unreachable");
}

/**
* Returns the end position of the given tree, see {@link
* SourcePositions#getEndPosition(CompilationUnitTree, Tree)}.
*
* @param tree an AST node
* @param unit the compilation unit that contains {@code tree}
* @return the end position of the given tree
*/
public static int getEndPosition(Tree tree, CompilationUnitTree unit) {
// This method has no `throws` clause, because of the `try` block.
try {
return (int) GET_END_POS_HANDLE.invokeExact((JCTree) tree, (JCTree.JCCompilationUnit) unit);
} catch (Throwable e) {
Throwables.throwIfUnchecked(e);
throw new AssertionError(e);
}
}

/** The {@link MethodHandle} for retrieving the end position of a {@link JCTree}. */
private static final MethodHandle GET_END_POS_HANDLE = getEndPosMethodHandle();

/**
* Returns the {@link MethodHandle} for retrieving the end position of a {@link JCTree}.
*
* @return the {@link MethodHandle} for retrieving the end position of a {@link JCTree}.
*/
private static MethodHandle getEndPosMethodHandle() {
MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
// JDK versions after https://bugs.openjdk.org/browse/JDK-8372948
// (tree, unit) -> tree.getEndPosition()
return MethodHandles.dropArguments(
lookup.findVirtual(JCTree.class, "getEndPosition", MethodType.methodType(int.class)),
1,
JCTree.JCCompilationUnit.class);
} catch (ReflectiveOperationException e1) {
// JDK versions before https://bugs.openjdk.org/browse/JDK-8372948
// (tree, unit) -> tree.getEndPosition(unit.endPositions)
try {
return MethodHandles.filterArguments(
lookup.findVirtual(
JCTree.class,
"getEndPosition",
MethodType.methodType(
int.class, Class.forName("com.sun.tools.javac.tree.EndPosTable"))),
1,
lookup
.findVarHandle(
JCTree.JCCompilationUnit.class,
"endPositions",
Class.forName("com.sun.tools.javac.tree.EndPosTable"))
.toMethodHandle(VarHandle.AccessMode.GET));
} catch (ReflectiveOperationException e2) {
e2.addSuppressed(e1);
throw new LinkageError(e2.getMessage(), e2);
}
}
}
}
Loading