Skip to content

Commit

Permalink
Fix the suggested fix for the first element in a class where the modi…
Browse files Browse the repository at this point in the history
…fiers

contain an opening brace.

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=220771489
  • Loading branch information
graememorgan authored and ronshapiro committed Nov 13, 2018
1 parent 6fea7b9 commit 40d4a4e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
34 changes: 24 additions & 10 deletions core/src/main/java/com/google/errorprone/bugpatterns/Unused.java
Expand Up @@ -94,6 +94,7 @@
import com.sun.tools.javac.code.Symbol.VarSymbol; import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.parser.Tokens.Comment; import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.TokenKind;
import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCAssign; import com.sun.tools.javac.tree.JCTree.JCAssign;
Expand Down Expand Up @@ -881,20 +882,23 @@ private static SuggestedFix replaceWithComments(
} }
if (previousMember != null) { if (previousMember != null) {
startTokenization = state.getEndPosition(previousMember); startTokenization = state.getEndPosition(previousMember);
} else if (state.getEndPosition(classTree.getModifiers()) == Position.NOPOS) {
startTokenization = ((JCTree) classTree).getStartPosition();
} else { } else {
int startOfClass = ((JCTree) classTree).getStartPosition(); startTokenization = state.getEndPosition(classTree.getModifiers());
String source =
state
.getSourceCode()
.subSequence(startOfClass, ((JCTree) tree).getStartPosition())
.toString();
startTokenization = startOfClass + source.indexOf("{") + 1;
} }
String source = String source =
state.getSourceCode().subSequence(startTokenization, state.getEndPosition(tree)).toString(); state.getSourceCode().subSequence(startTokenization, state.getEndPosition(tree)).toString();
List<ErrorProneToken> tokens = ErrorProneTokens.getTokens(source, state.context); ImmutableList<ErrorProneToken> tokens = ErrorProneTokens.getTokens(source, state.context);
if (tokens.isEmpty() || tokens.get(0).comments().isEmpty()) { if (previousMember == null) {
return SuggestedFix.replace(startTokenization, state.getEndPosition(tree), replacement); tokens = getTokensAfterOpeningBrace(tokens);
}
if (tokens.isEmpty()) {
return SuggestedFix.replace(tree, replacement);
}
if (tokens.get(0).comments().isEmpty()) {
return SuggestedFix.replace(
startTokenization + tokens.get(0).pos(), state.getEndPosition(tree), replacement);
} }
List<Comment> comments = List<Comment> comments =
tokens.get(0).comments().stream() tokens.get(0).comments().stream()
Expand All @@ -918,6 +922,16 @@ private static SuggestedFix replaceWithComments(
startTokenization + startPos, state.getEndPosition(tree), replacement); startTokenization + startPos, state.getEndPosition(tree), replacement);
} }


private static ImmutableList<ErrorProneToken> getTokensAfterOpeningBrace(
ImmutableList<ErrorProneToken> tokens) {
for (int i = 0; i < tokens.size() - 1; ++i) {
if (tokens.get(i).kind() == TokenKind.LBRACE) {
return tokens.subList(i + 1, tokens.size());
}
}
return ImmutableList.of();
}

private static boolean isEnhancedForLoopVar(TreePath variablePath) { private static boolean isEnhancedForLoopVar(TreePath variablePath) {
Tree tree = variablePath.getLeaf(); Tree tree = variablePath.getLeaf();
Tree parent = variablePath.getParentPath().getLeaf(); Tree parent = variablePath.getParentPath().getLeaf();
Expand Down
Expand Up @@ -612,6 +612,23 @@ public void removal_javadocAndSingleLines() {
.doTest(TestMode.TEXT_MATCH); .doTest(TestMode.TEXT_MATCH);
} }


@Test
public void removal_rogueBraces() {
refactoringHelper
.addInputLines(
"Test.java",
"@SuppressWarnings(\"foo\" /* { */)",
"public class Test {",
" private static final int A = 1;",
"}")
.addOutputLines(
"Test.java", //
"@SuppressWarnings(\"foo\" /* { */)",
"public class Test {",
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test @Test
public void unusedWithComment_interspersedComments() { public void unusedWithComment_interspersedComments() {
helper helper
Expand Down

0 comments on commit 40d4a4e

Please sign in to comment.