Skip to content

Commit

Permalink
Update SuggestedFixes: change method signature of removeModifiers, ad…
Browse files Browse the repository at this point in the history
…d a renameMethod function, and temporarily ignore EP plugin test broken by the resulting one version violation, until the next JavaBuilder release.

RELNOTES: Added a SuggestedFixes.renameMethod function

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=150478500
  • Loading branch information
epmjohnston authored and eaftan committed Mar 29, 2017
1 parent fa0c55a commit 671f58c
Showing 1 changed file with 28 additions and 1 deletion.
Expand Up @@ -40,6 +40,7 @@
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.SuggestedFix.Builder;
import com.google.errorprone.util.ErrorProneToken;
import com.google.errorprone.util.ErrorProneTokens;
import com.sun.source.doctree.DocTree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.AssignmentTree;
Expand All @@ -57,6 +58,7 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.parser.Tokens;
import com.sun.tools.javac.parser.Tokens.TokenKind;
import com.sun.tools.javac.tree.DCTree;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
Expand Down Expand Up @@ -177,7 +179,7 @@ public static SuggestedFix addModifiers(Tree tree, VisitorState state, Modifier.

/** Remove modifiers from the given class, method, or field declaration. */
@Nullable
public static Fix removeModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
public static SuggestedFix removeModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
Set<Modifier> toRemove = ImmutableSet.copyOf(modifiers);
ModifiersTree originalModifiers = getModifiers(tree);
if (originalModifiers == null) {
Expand Down Expand Up @@ -392,6 +394,31 @@ public void visitIdent(JCTree.JCIdent tree) {
return fix.build();
}

/** Be warned, only changes method name at the declaration. */
public static SuggestedFix renameMethod(
MethodTree tree, final String replacement, VisitorState state) {
// Search tokens from beginning of method tree to beginning of method body.
int basePos = ((JCTree) tree).getStartPosition();
int endPos =
tree.getBody() != null
? ((JCTree) tree.getBody()).getStartPosition()
: state.getEndPosition(tree);
List<ErrorProneToken> methodTokens =
ErrorProneTokens.getTokens(
state.getSourceCode().subSequence(basePos, endPos).toString(), state.context);
for (ErrorProneToken token : methodTokens) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(tree.getName())) {
int nameStartPosition = basePos + token.pos();
int nameEndPosition = basePos + token.endPos();
return SuggestedFix.builder()
.replace(nameStartPosition, nameEndPosition, replacement)
.build();
}
}
// Method name not found.
throw new AssertionError();
}

/** Deletes the given exceptions from a method's throws clause. */
public static Fix deleteExceptions(
MethodTree tree, final VisitorState state, List<ExpressionTree> toDelete) {
Expand Down

0 comments on commit 671f58c

Please sign in to comment.