private int getEndPosition(VisitorState state, JCTree node) { return state.getEndPosition(node); }
/** Gets the index of where to insert the delta argument. */ private int getDeltaInsertionIndex( MethodInvocationTree methodInvocationTree, VisitorState state) { JCTree lastArgument = (JCTree) Iterables.getLast(methodInvocationTree.getArguments()); return state.getEndPosition(lastArgument); }
private void removeRightOperand(BinaryTree superBinary) { fix.replace( state.getEndPosition(superBinary.getLeftOperand()), state.getEndPosition(superBinary.getRightOperand()), ""); }
@Override public Void visitAssignment(AssignmentTree tree, ExpressionTree expr) { if (expressionsEqual(tree.getVariable(), expr)) { earliestStart = Math.min(earliestStart, state.getEndPosition(tree)); } return super.visitAssignment(tree, expr); } }
/** * Returns a {@link Fix} that adds members defined by {@code firstMember} (and optionally {@code * otherMembers}) to the end of the class referenced by {@code classTree}. This method should only * be called once per {@link ClassTree} as the suggestions will otherwise collide. */ public static Fix addMembers( ClassTree classTree, VisitorState state, String firstMember, String... otherMembers) { checkNotNull(classTree); int classEndPosition = state.getEndPosition(classTree); StringBuilder stringBuilder = new StringBuilder(); for (String memberSnippet : Lists.asList(firstMember, otherMembers)) { stringBuilder.append("\n\n").append(memberSnippet); } stringBuilder.append('\n'); return SuggestedFix.replace( classEndPosition - 1, classEndPosition - 1, stringBuilder.toString()); }
/** Deletes an entry and its delimiter from a list. */ private static void removeFromList( SuggestedFix.Builder fix, VisitorState state, List<? extends Tree> arguments, Tree tree) { int idx = arguments.indexOf(tree); if (idx == arguments.size() - 1) { fix.replace( state.getEndPosition(arguments.get(arguments.size() - 1)), state.getEndPosition(tree), ""); } else { fix.replace( ((JCTree) tree).getStartPosition(), ((JCTree) arguments.get(idx + 1)).getStartPosition(), ""); } } }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (ANNOTATION_CLASS.matches(tree, state)) { return describeMatch( tree, SuggestedFix.replace( state.getEndPosition(ASTHelpers.getReceiver(tree)), state.getEndPosition(tree), ".annotationType()")); } return Description.NO_MATCH; } }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (ENUM_CLASS.matches(tree, state)) { return describeMatch( tree, SuggestedFix.replace( state.getEndPosition(ASTHelpers.getReceiver(tree)), state.getEndPosition(tree), ".getDeclaringClass()")); } return Description.NO_MATCH; } }
private static int caseEndPosition(VisitorState state, JCTree.JCCase caseTree) { // if the statement group is a single block statement, handle fall through comments at the // end of the block if (caseTree.stats.size() == 1) { JCTree.JCStatement only = getOnlyElement(caseTree.stats); if (only.hasTag(JCTree.Tag.BLOCK)) { BlockTree blockTree = (BlockTree) only; return blockTree.getStatements().isEmpty() ? ((JCTree) blockTree).getStartPosition() : state.getEndPosition(getLast(blockTree.getStatements())); } } return state.getEndPosition(caseTree); } }
void advance() { ExpressionTree nextArgument = argumentsIterator.next(); currentArgumentEndPosition = state.getEndPosition(nextArgument) - offset; previousArgumentEndPosition = currentArgumentStartPosition; currentArgumentStartPosition = ((JCTree) nextArgument).getStartPosition() - offset; if (previousCommentedResultBuilder != null) { resultBuilder.add(previousCommentedResultBuilder.build()); } previousCommentedResultBuilder = currentCommentedResultBuilder; currentCommentedResultBuilder = Commented.<ExpressionTree>builder().setTree(nextArgument); }
private static String getMessageOrFormat(MethodInvocationTree tree, VisitorState state) { if (tree.getArguments().size() > 1) { return "String.format(" + state .getSourceCode() .subSequence( ((JCTree) tree.getArguments().get(0)).getStartPosition(), state.getEndPosition(Iterables.getLast(tree.getArguments()))) + ")"; } return state.getSourceForNode(getOnlyElement(tree.getArguments())); }
private Fix appendCharset( Tree tree, Tree select, List<? extends ExpressionTree> arguments, VisitorState state, CharsetFix charset) { SuggestedFix.Builder fix = SuggestedFix.builder(); if (arguments.isEmpty()) { fix.replace( state.getEndPosition(select), state.getEndPosition(tree), String.format("(%s)", charset.replacement())); } else { fix.postfixWith(Iterables.getLast(arguments), ", " + charset.replacement()); } charset.addImport(fix, state); return fix.build(); }
private static SuggestedFix appendArgument( NewClassTree constructor, String exception, VisitorState state, List<Type> types) { if (types.isEmpty()) { // Skip past the opening '(' of the constructor. String source = state.getSourceForNode(constructor); int startPosition = ((JCTree) constructor).getStartPosition(); int pos = source.indexOf('(', state.getEndPosition(constructor.getIdentifier()) - startPosition) + startPosition + 1; return SuggestedFix.replace(pos, pos, exception); } return SuggestedFix.postfixWith( getLast(constructor.getArguments()), String.format(", %s", exception)); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return Description.NO_MATCH; } return buildDescription(tree) .setMessage( String.format("Use of %s is a no-op and is not allowed.", ASTHelpers.getSymbol(tree))) .addFix( SuggestedFix.replace( state.getEndPosition(ASTHelpers.getReceiver(tree)), state.getEndPosition(tree), "")) .build(); }
/** * Attach comments to nodes on arguments of method calls. Calls such as {@code test(param1 /* c1 * */, /* c2 */ param2)} will attach the comment c1 to {@code param1} and the comment c2 * to {@code param2}. * * <p>Warning: this is expensive to compute as it involves re-tokenizing the source for this node * * <p>Currently this method will only tokenize the source code of the method call itself. However, * the source positions in the returned {@code Comment} objects are adjusted so that they are * relative to the whole file. */ public static ImmutableList<Commented<ExpressionTree>> findCommentsForArguments( MethodInvocationTree methodInvocationTree, VisitorState state) { int startPosition = state.getEndPosition(methodInvocationTree.getMethodSelect()); return findCommentsForArguments( methodInvocationTree, methodInvocationTree.getArguments(), startPosition, state); }
/** Replaces the name of the method being invoked in {@code tree} with {@code replacement}. */ public static SuggestedFix renameMethodInvocation( MethodInvocationTree tree, String replacement, VisitorState state) { Tree methodSelect = tree.getMethodSelect(); int startPos; String extra = ""; if (methodSelect instanceof MemberSelectTree) { startPos = state.getEndPosition(((MemberSelectTree) methodSelect).getExpression()); extra = "."; } else if (methodSelect instanceof IdentifierTree) { startPos = ((JCTree) tree).getStartPosition(); } else { return SuggestedFix.builder().build(); } int endPos = state.getEndPosition(methodSelect); return SuggestedFix.replace(startPos, endPos, extra + replacement); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!NEW_INSTANCE.matches(tree, state)) { return Description.NO_MATCH; } SuggestedFix.Builder fix = SuggestedFix.builder(); fix.replace( state.getEndPosition(ASTHelpers.getReceiver(tree)), state.getEndPosition(tree), ".getDeclaredConstructor().newInstance()"); boolean fixedExceptions = fixExceptions(state, fix); if (!fixedExceptions) { fixThrows(state, fix); } return describeMatch(tree, fix.build()); }
@Override public Description matchParenthesized(ParenthesizedTree tree, VisitorState state) { ExpressionTree expression = tree.getExpression(); if (state.getPath().getParentPath().getLeaf() instanceof StatementTree) { return NO_MATCH; } if (ASTHelpers.requiresParentheses(expression, state)) { return NO_MATCH; } return describeMatch( tree, SuggestedFix.builder() .replace( ((JCTree) tree).getStartPosition(), ((JCTree) expression).getStartPosition(), "") .replace(state.getEndPosition(expression), state.getEndPosition(tree), "") .build()); } }
@Override public Void visitBinary(BinaryTree tree, SuggestedFix.Builder p) { if (tree.getKind() == Kind.AND || tree.getKind() == Kind.OR) { p.replace( /* startPos= */ state.getEndPosition(tree.getLeftOperand()), /* endPos= */ ((JCTree) tree.getRightOperand()).getStartPosition(), tree.getKind() == Tree.Kind.AND ? " && " : " || "); } return super.visitBinary(tree, p); } }
private Description checkCondition(ExpressionTree condition, VisitorState state) { if (!(condition instanceof AssignmentTree)) { return NO_MATCH; } AssignmentTree assign = (AssignmentTree) condition; return buildDescription(condition) .addFix( SuggestedFix.builder().prefixWith(condition, "(").postfixWith(condition, ")").build()) .addFix( SuggestedFix.replace( /*startPos=*/ state.getEndPosition(assign.getVariable()), /*endPos=*/ ((JCTree) assign.getExpression()).getStartPosition(), " == ")) .build(); } }