Skip to content

Commit

Permalink
Change getEndPosition signature to accept Trees
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=128086178
  • Loading branch information
cushon committed Jul 22, 2016
1 parent 6a025bf commit bb0f833
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 34 deletions.
12 changes: 3 additions & 9 deletions core/src/main/java/com/google/errorprone/VisitorState.java
Expand Up @@ -23,7 +23,6 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ErrorProneToken; import com.google.errorprone.util.ErrorProneToken;
import com.google.errorprone.util.ErrorProneTokens; import com.google.errorprone.util.ErrorProneTokens;

import com.sun.source.tree.Tree; import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath; import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
Expand All @@ -44,7 +43,6 @@
import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names; import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Options; import com.sun.tools.javac.util.Options;

import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
Expand Down Expand Up @@ -322,17 +320,13 @@ public ImmutableList<ErrorProneToken> getTokensForNode(Tree tree) {
return ErrorProneTokens.getTokens(getSourceForNode(tree), context); return ErrorProneTokens.getTokens(getSourceForNode(tree), context);
} }


/** /** Returns the end position of the node, or -1 if it is not available. */
* Gets the end position of the given node. public int getEndPosition(Tree node) {
*
* @return the end position of the node, or -1 if it is not available
*/
public int getEndPosition(JCTree node) {
JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit(); JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit();
if (compilationUnit.endPositions == null) { if (compilationUnit.endPositions == null) {
return -1; return -1;
} }
return node.getEndPosition(compilationUnit.endPositions); return ((JCTree) node).getEndPosition(compilationUnit.endPositions);
} }


/** /**
Expand Down
Expand Up @@ -32,7 +32,6 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.CatchTree; import com.sun.source.tree.CatchTree;
import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
Expand All @@ -44,7 +43,6 @@
import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.tree.TreeScanner;

import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
Expand Down Expand Up @@ -79,8 +77,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
} }
SuggestedFix.Builder fix = SuggestedFix.builder(); SuggestedFix.Builder fix = SuggestedFix.builder();
fix.replace( fix.replace(
state.getEndPosition((JCTree) ASTHelpers.getReceiver(tree)), state.getEndPosition(ASTHelpers.getReceiver(tree)),
state.getEndPosition((JCTree) tree), state.getEndPosition(tree),
String.format(".getConstructor().newInstance()")); String.format(".getConstructor().newInstance()"));
boolean fixedExceptions = fixExceptions(state, fix); boolean fixedExceptions = fixExceptions(state, fix);
if (!fixedExceptions) { if (!fixedExceptions) {
Expand Down
Expand Up @@ -37,17 +37,14 @@
import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.suppliers.Suppliers; import com.google.errorprone.suppliers.Suppliers;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.ImportTree; import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree; import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree; import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Options; import com.sun.tools.javac.util.Options;

import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileReader; import java.io.FileReader;
Expand Down Expand Up @@ -359,8 +356,8 @@ private Fix appendCharset(
SuggestedFix.Builder fix = SuggestedFix.builder(); SuggestedFix.Builder fix = SuggestedFix.builder();
if (arguments.isEmpty()) { if (arguments.isEmpty()) {
fix.replace( fix.replace(
state.getEndPosition((JCTree) select), state.getEndPosition(select),
state.getEndPosition((JCTree) tree), state.getEndPosition(tree),
String.format("(%s)", charset.replacement())); String.format("(%s)", charset.replacement()));
} else { } else {
fix.postfixWith(Iterables.getLast(arguments), ", " + charset.replacement()); fix.postfixWith(Iterables.getLast(arguments), ", " + charset.replacement());
Expand Down
Expand Up @@ -27,11 +27,8 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.tree.JCTree;

import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;


/** @author cushon@google.com (Liam Miller-Cushon) */ /** @author cushon@google.com (Liam Miller-Cushon) */
Expand All @@ -57,8 +54,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return describeMatch( return describeMatch(
tree, tree,
SuggestedFix.replace( SuggestedFix.replace(
state.getEndPosition((JCTree) ASTHelpers.getReceiver(tree)), state.getEndPosition(ASTHelpers.getReceiver(tree)),
state.getEndPosition((JCTree) tree), state.getEndPosition(tree),
".annotationType()")); ".annotationType()"));
} }
return Description.NO_MATCH; return Description.NO_MATCH;
Expand Down
Expand Up @@ -27,10 +27,8 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.tree.JCTree;


/** @author cushon@google.com (Liam Miller-Cushon) */ /** @author cushon@google.com (Liam Miller-Cushon) */
@BugPattern( @BugPattern(
Expand All @@ -51,8 +49,8 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return describeMatch( return describeMatch(
tree, tree,
SuggestedFix.replace( SuggestedFix.replace(
state.getEndPosition((JCTree) ASTHelpers.getReceiver(tree)), state.getEndPosition(ASTHelpers.getReceiver(tree)),
state.getEndPosition((JCTree) tree), state.getEndPosition(tree),
".getDeclaringClass()")); ".getDeclaringClass()"));
} }
return Description.NO_MATCH; return Description.NO_MATCH;
Expand Down
Expand Up @@ -28,7 +28,6 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Description.Builder; import com.google.errorprone.matchers.Description.Builder;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.CaseTree; import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.IdentifierTree;
Expand All @@ -37,11 +36,9 @@
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCSwitch; import com.sun.tools.javac.tree.JCTree.JCSwitch;
import com.sun.tools.javac.tree.TreeInfo; import com.sun.tools.javac.tree.TreeInfo;

import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;

import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;


/** /**
Expand Down Expand Up @@ -86,7 +83,7 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
private void buildFixes( private void buildFixes(
SwitchTree tree, VisitorState state, Set<String> unhandled, Builder description) { SwitchTree tree, VisitorState state, Set<String> unhandled, Builder description) {


int idx = state.getEndPosition((JCTree) tree) - 1; // preserve closing '}' int idx = state.getEndPosition(tree) - 1; // preserve closing '}'


StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String label : unhandled) { for (String label : unhandled) {
Expand Down
Expand Up @@ -29,14 +29,12 @@
import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.ASTHelpers;

import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree; import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree; import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol; import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;

import java.util.List; import java.util.List;


/** /**
Expand Down Expand Up @@ -91,7 +89,7 @@ private Fix buildFix(Tree tree, List<? extends Tree> arguments, VisitorState sta
JCTree node = (JCTree) tree; JCTree node = (JCTree) tree;
int startAbsolute = node.getStartPosition(); int startAbsolute = node.getStartPosition();
int lower = ((JCTree) arguments.get(0)).getStartPosition() - startAbsolute; int lower = ((JCTree) arguments.get(0)).getStartPosition() - startAbsolute;
int upper = state.getEndPosition((JCTree) arguments.get(arguments.size() - 1)) - startAbsolute; int upper = state.getEndPosition(arguments.get(arguments.size() - 1)) - startAbsolute;


CharSequence source = state.getSourceForNode(node); CharSequence source = state.getSourceForNode(node);


Expand Down

0 comments on commit bb0f833

Please sign in to comment.