private static boolean isPartOfMethodInvocation(TreePath idPath) { Kind parentKind = idPath.getParentPath().getLeaf().getKind(); if (parentKind == Kind.METHOD_INVOCATION) { // must be an argument return true; } if (parentKind == Kind.MEMBER_SELECT) { Tree maybeMethodInvocation = idPath.getParentPath().getParentPath().getLeaf(); // likely the target of the method invocation return maybeMethodInvocation.getKind() == Kind.METHOD_INVOCATION; } return false; }
/** * Given a TreePath, finds the first enclosing node of the given type and returns the path from * the enclosing node to the top-level {@code CompilationUnitTree}. */ public static <T> TreePath findPathFromEnclosingNodeToTopLevel(TreePath path, Class<T> klass) { if (path != null) { do { path = path.getParentPath(); } while (path != null && !(klass.isInstance(path.getLeaf()))); } return path; }
/** * Returns the enclosing method of the given visitor state. Returns null if the state is within a * lambda expression or anonymous class. */ @Nullable private static MethodTree enclosingMethod(VisitorState state) { for (Tree node : state.getPath().getParentPath()) { switch (node.getKind()) { case LAMBDA_EXPRESSION: case NEW_CLASS: return null; case METHOD: return (MethodTree) node; default: break; } } return null; }
@Override public IdentifierTree visitIdentifier(IdentifierTree node, Void aVoid) { Symbol nodeSymbol = ASTHelpers.getSymbol(node); if (symbols.contains(nodeSymbol) && !isSuppressed(node)) { if (getCurrentPath().getParentPath().getLeaf().getKind() != Kind.CASE) { builder.prefixWith(node, enclosingReplacement); moveTypeAnnotations(node); return node; } } return super.visitIdentifier(node, aVoid); }
private static boolean isInPrivateScope(VisitorState state) { TreePath treePath = state.getPath(); do { Tree currentLeaf = treePath.getLeaf(); if (currentLeaf instanceof ClassTree) { ClassTree currentClassTree = (ClassTree) currentLeaf; if (currentClassTree.getModifiers().getFlags().contains(PRIVATE)) { return true; } } treePath = treePath.getParentPath(); } while (treePath != null); return false; } }
/** * Must be run under MDR transaction! */ public javax.lang.model.element.Element getJavaClass() { TreePath path = null; // try { // path = getCompletionTreePath(getController(), endOffset, COMPLETION_QUERY_TYPE); // } catch (IOException ex) { // Exceptions.printStackTrace(ex); // } javax.lang.model.element.Element el = null; try { getController().toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); } catch (IOException ex) { Exceptions.printStackTrace(ex); } while ((el == null || !(ElementKind.CLASS == el.getKind() || ElementKind.INTERFACE == el.getKind())) && path != null) { path.getCompilationUnit().getTypeDecls(); el = getController().getTrees().getElement(path); path = path.getParentPath(); } return el; }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { ExpressionTree initializer = stripNullCheck(tree.getInitializer(), state); Tree parent = state.getPath().getParentPath().getLeaf(); // must be a static class variable with member select initializer if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS || !tree.getModifiers().getFlags().contains(STATIC)) { return Description.NO_MATCH; } MemberSelectTree rhs = (MemberSelectTree) initializer; Symbol rhsClass = ASTHelpers.getSymbol(rhs.getExpression()); Symbol lhsClass = ASTHelpers.getSymbol(parent); if (rhsClass != null && lhsClass != null && rhsClass.equals(lhsClass) && rhs.getIdentifier().contentEquals(tree.getName())) { return describeForVarDecl(tree, state); } return Description.NO_MATCH; }
while (path != null && !(path.getLeaf() instanceof MethodTree)) { path = path.getParentPath(); MethodTree methodDecl = (MethodTree) path.getLeaf(); for (VariableTree param : methodDecl.getParameters()) { if (symbols.contains(ASTHelpers.getSymbol(param))) { return true;
@Nullable private static JCMethodDecl findSurroundingMethod(TreePath path) { while (path.getLeaf().getKind() != Kind.METHOD) { if (path.getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) { // Ignore return statements in lambda expressions. There's no method declaration to suggest // annotations for anyway. return null; } path = path.getParentPath(); } return (JCMethodDecl) path.getLeaf(); } }
private static boolean collectionUsed(VisitorState state) { TreePath path = state.getPath(); return !(path.getParentPath().getLeaf() instanceof MemberSelectTree) || !(path.getParentPath().getParentPath().getLeaf() instanceof MethodInvocationTree) || !COLLECTION_SETTER.matches( (MethodInvocationTree) path.getParentPath().getParentPath().getLeaf(), state) || ASTHelpers.targetType(state.withPath(path.getParentPath().getParentPath())) != null; }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!FENCE_MATCHER.matches(tree, state)) { return NO_MATCH; } Tree previous = null; OUTER: for (Tree enclosing : state.getPath().getParentPath()) { switch (enclosing.getKind()) { case TRY: if (((TryTree) enclosing).getFinallyBlock().equals(previous)) { return NO_MATCH; } break; case CLASS: case METHOD: case LAMBDA_EXPRESSION: break OUTER; default: // fall out } previous = enclosing; } return describeMatch(tree); } }
private static TreePath findTypePath(TreePath tp) { if (!TYPE_PATH_ELEMENT.contains(tp.getLeaf().getKind())) return null; while (TYPE_PATH_ELEMENT.contains(tp.getParentPath().getLeaf().getKind())) { tp = tp.getParentPath(); } return tp; }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } Tree parent = state.getPath().getParentPath().getLeaf(); if (!(parent instanceof TypeCastTree)) { return NO_MATCH; } if (!((TypeCastTree) parent).getExpression().equals(tree)) { return NO_MATCH; } Type type = ASTHelpers.getType(parent); if (type == null || !INTEGRAL.contains(type.getKind())) { return NO_MATCH; } return describeMatch(tree); } }
private static TreePath findParentOfKind(VisitorState state, Kind kind) { TreePath path = state.getPath(); while (path != null && path.getLeaf().getKind() != kind) { path = path.getParentPath(); } return path; }
private Tree getCurrentlyAnnotatedNode(VisitorState state) { return state.getPath().getParentPath().getParentPath().getLeaf(); } }
@Override public Description matchBinary(BinaryTree tree, VisitorState state) { switch (tree.getKind()) { case AND: case OR: break; default: return NO_MATCH; } if (!isSameType(getType(tree), state.getSymtab().booleanType, state)) { return NO_MATCH; } Iterator<Tree> stateIterator = state.getPath().getParentPath().iterator(); Tree parent = stateIterator.next(); if (parent instanceof BinaryTree && (parent.getKind() == Kind.AND || parent.getKind() == Kind.OR)) { return NO_MATCH; } else { SuggestedFix.Builder fix = SuggestedFix.builder(); new TreeScannerBinary(state).scan(tree, fix); return describeMatch(tree, fix.build()); } }
/** * Returns path to the deepest tree of one of the given kinds containing * the given starting tree. * @param kinds requested tree kinds * @param path path to the starting tree * @return requested path or null if no tree of the given kinds encloses * the given starting tree * * @since 0.136 */ public TreePath getPathElementOfKind(Set<Tree.Kind> kinds, TreePath path) { while (path != null) { if (kinds.contains(path.getLeaf().getKind())) { return path; } path = path.getParentPath(); } return null; }
Tree toReplace = null; for (TreePath path = state.getPath().getParentPath(); path != null; path = path.getParentPath()) { Tree enclosing = path.getLeaf(); if (unmodifiableMatcher.matches(enclosing, state)) { unmodifiable = enclosing; constant = symbol.isStatic() && symbol.getModifiers().contains(Modifier.FINAL) && symbol.getKind() == ElementKind.FIELD;
private TreePath findEnclosing(VisitorState state) { for (TreePath path = state.getPath(); path != null; path = path.getParentPath()) { switch (path.getLeaf().getKind()) { case METHOD: case LAMBDA_EXPRESSION: return path; case CLASS: return null; default: // fall out } } return null; } }
private Tree getCurrentlyAnnotatedNode(VisitorState state) { return state.getPath().getParentPath().getParentPath().getLeaf(); } }