Skip to content

Commit

Permalink
Introduce ConstantsFormat Refaster rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Nov 27, 2023
1 parent 17c7b39 commit ca8ba55
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.util.Convert;
import com.sun.tools.javac.util.Constants;
import java.util.Formattable;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -150,7 +150,7 @@ private Description trySuggestExplicitJoin(
SuggestedFix.Builder fix =
SuggestedFix.builder()
.replace(tree.getMethodSelect(), "String.join")
.replace(arguments.next(), String.format("\"%s\"", Convert.quote(separator)));
.replace(arguments.next(), Constants.format(separator));

while (arguments.hasNext()) {
ExpressionTree argument = arguments.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.sun.tools.javac.util.Constants;
import com.sun.tools.javac.util.Convert;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to {@link com.google.errorprone.bugpatterns.BugChecker} classes. */
Expand Down Expand Up @@ -52,4 +54,16 @@ BugCheckerRefactoringTestHelper after(
return helper.addInputLines(path, source).expectUnchanged();
}
}

static final class ConstantsFormat {
@BeforeTemplate
String before(String value) {
return String.format("\"%s\"", Convert.quote(value));
}

@AfterTemplate
String after(String value) {
return Constants.format(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.bugpatterns.BugChecker;
import com.sun.tools.javac.util.Convert;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(FixChoosers.class);
return ImmutableSet.of(Convert.class, FixChoosers.class);
}

ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelperIdentity() {
Expand All @@ -26,4 +27,8 @@ ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelpe
.addInputLines("A.java", "class A {}")
.addOutputLines("A.java", "class A {}");
}

String testConstantsFormat() {
return String.format("\"%s\"", Convert.quote("foo"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.bugpatterns.BugChecker;
import com.sun.tools.javac.util.Constants;
import com.sun.tools.javac.util.Convert;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(FixChoosers.class);
return ImmutableSet.of(Convert.class, FixChoosers.class);
}

ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelperIdentity() {
Expand All @@ -24,4 +26,8 @@ ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelpe
.addInputLines("A.java", "class A {}")
.expectUnchanged();
}

String testConstantsFormat() {
return Constants.format("foo");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static java.util.Comparator.naturalOrder;
import static tech.picnic.errorprone.refaster.runner.Refaster.INCLUDED_RULES_PATTERN_FLAG;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableRangeMap;
Expand Down Expand Up @@ -123,7 +122,9 @@ public static void validate(Class<?> clazz) {
String className = clazz.getSimpleName();

BugCheckerRefactoringTestHelper.newInstance(RefasterRuleCollection.class, clazz)
.setArgs(ImmutableList.of("-XepOpt:" + RULE_COLLECTION_FLAG + '=' + className))
.setArgs(
"--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"-XepOpt:" + RULE_COLLECTION_FLAG + '=' + className)
.addInput(className + "TestInput.java")
.addOutput(className + "TestOutput.java")
.doTest(TestMode.TEXT_MATCH);
Expand Down

0 comments on commit ca8ba55

Please sign in to comment.