Skip to content

Commit

Permalink
Make last files consistent with refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Oct 11, 2022
1 parent 8e14bbc commit f3aa904
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static Optional<SeverityLevel> toSeverityLevel(Severity severity) {
* <p>The assigned severity is overridden only if this bug checker's severity was explicitly
* configured.
*
* <p>The original check name (i.e. the Refaster template name) is prepended to the {@link
* <p>The original check name (i.e. the Refaster rule name) is prepended to the {@link
* Description}'s message. The replacement check name ("Refaster Rule", a name which includes a
* space) is chosen such that it is guaranteed not to match any canonical bug checker name (as
* that could cause {@link VisitorState#reportMatch(Description)}} to override the reported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ final class CodeTransformersTest {
void getAllCodeTransformers() {
assertThat(CodeTransformers.getAllCodeTransformers().keySet())
.containsExactlyInAnyOrder(
"FooTemplates$StringOfSizeZeroTemplate",
"FooTemplates$StringOfSizeZeroVerboseTemplate",
"FooTemplates$StringOfSizeOneTemplate",
"FooTemplates$ExtraGrouping$StringOfSizeTwoTemplate",
"FooTemplates$ExtraGrouping$StringOfSizeThreeTemplate");
"FooRules$StringOfSizeZeroRule",
"FooRules$StringOfSizeZeroVerboseRule",
"FooRules$StringOfSizeOneRule",
"FooRules$ExtraGrouping$StringOfSizeTwoRule",
"FooRules$ExtraGrouping$StringOfSizeThreeRule");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
import tech.picnic.errorprone.refaster.annotation.Severity;

/** An example template collection used to test {@link CodeTransformers} and {@link Refaster}. */
/** An example rule collection used to test {@link CodeTransformers} and {@link Refaster}. */
final class FooRules {
private FooRules() {}

/** A simple template for testing purposes, lacking any custom annotations. */
static final class StringOfSizeZeroTemplate {
/** A simple rule for testing purposes, lacking any custom annotations. */
static final class StringOfSizeZeroRule {
@BeforeTemplate
boolean before(String string) {
return string.toCharArray().length == 0;
Expand All @@ -28,10 +28,10 @@ boolean after(String string) {
}

/**
* A simple template for testing purposes, matching the same set of expressions as {@link
* StringOfSizeZeroTemplate}, but producing a larger replacement string.
* A simple rule for testing purposes, matching the same set of expressions as {@link
* StringOfSizeZeroRule}, but producing a larger replacement string.
*/
static final class StringOfSizeZeroVerboseTemplate {
static final class StringOfSizeZeroVerboseRule {
@BeforeTemplate
boolean before(String string) {
return string.toCharArray().length == 0;
Expand All @@ -43,11 +43,11 @@ boolean after(String string) {
}
}

/** A simple template for testing purposes, having several custom annotations. */
/** A simple rule for testing purposes, having several custom annotations. */
@Description("A custom description about matching single-char strings")
@OnlineDocumentation
@Severity(WARNING)
static final class StringOfSizeOneTemplate {
static final class StringOfSizeOneRule {
@BeforeTemplate
boolean before(String string) {
return string.toCharArray().length == 1;
Expand All @@ -59,17 +59,15 @@ boolean after(String string) {
}
}

/**
* A nested class with annotations that are inherited by the Refaster templates contained in it.
*/
/** A nested class with annotations that are inherited by the Refaster rules contained in it. */
@Description("A custom subgroup description")
@OnlineDocumentation("https://example.com/template/${topLevelClassName}#${nestedClassName}")
@OnlineDocumentation("https://example.com/rule/${topLevelClassName}#${nestedClassName}")
@Severity(ERROR)
static final class ExtraGrouping {
private ExtraGrouping() {}

/** A simple template for testing purposes, inheriting custom annotations. */
static final class StringOfSizeTwoTemplate {
/** A simple rule for testing purposes, inheriting custom annotations. */
static final class StringOfSizeTwoRule {
@BeforeTemplate
boolean before(String string) {
return string.toCharArray().length == 2;
Expand All @@ -81,11 +79,11 @@ boolean after(String string) {
}
}

/** A simple template for testing purposes, overriding custom annotations. */
/** A simple rule for testing purposes, overriding custom annotations. */
@Description("A custom description about matching three-char strings")
@OnlineDocumentation("https://example.com/custom")
@Severity(SUGGESTION)
static final class StringOfSizeThreeTemplate {
static final class StringOfSizeThreeRule {
@BeforeTemplate
boolean before(String string) {
return string.toCharArray().length == 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ final class RefasterTest {
CompilationTestHelper.newInstance(Refaster.class, getClass())
.matchAllDiagnostics()
.expectErrorMessage(
"StringOfSizeZeroTemplate",
"StringOfSizeZeroRule",
containsPattern(
"\\[Refaster Rule\\] FooTemplates\\.StringOfSizeZeroTemplate: Refactoring opportunity\\s+.+\\s+"))
"\\[Refaster Rule\\] FooRules\\.StringOfSizeZeroRule: Refactoring opportunity\\s+.+\\s+"))
.expectErrorMessage(
"StringOfSizeOneTemplate",
"StringOfSizeOneRule",
containsPattern(
"\\[Refaster Rule\\] FooTemplates\\.StringOfSizeOneTemplate: "
"\\[Refaster Rule\\] FooRules\\.StringOfSizeOneRule: "
+ "A custom description about matching single-char strings\\s+.+\\s+"
+ "\\(see https://error-prone.picnic.tech/refasterrules/FooTemplates#StringOfSizeOneTemplate\\)"))
+ "\\(see https://error-prone.picnic.tech/refasterrules/FooRules#StringOfSizeOneRule\\)"))
.expectErrorMessage(
"StringOfSizeTwoTemplate",
"StringOfSizeTwoRule",
containsPattern(
"\\[Refaster Rule\\] FooTemplates\\.ExtraGrouping\\.StringOfSizeTwoTemplate: "
"\\[Refaster Rule\\] FooRules\\.ExtraGrouping\\.StringOfSizeTwoRule: "
+ "A custom subgroup description\\s+.+\\s+"
+ "\\(see https://example.com/template/FooTemplates#ExtraGrouping.StringOfSizeTwoTemplate\\)"))
+ "\\(see https://example.com/rule/FooRules#ExtraGrouping.StringOfSizeTwoRule\\)"))
.expectErrorMessage(
"StringOfSizeThreeTemplate",
"StringOfSizeThreeRule",
containsPattern(
"\\[Refaster Rule\\] FooTemplates\\.ExtraGrouping\\.StringOfSizeThreeTemplate: "
"\\[Refaster Rule\\] FooRules\\.ExtraGrouping\\.StringOfSizeThreeRule: "
+ "A custom description about matching three-char strings\\s+.+\\s+"
+ "\\(see https://example.com/custom\\)"));
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(Refaster.class, getClass());
private final BugCheckerRefactoringTestHelper restrictedRefactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(Refaster.class, getClass())
.setArgs(
"-XepOpt:Refaster:NamePattern=.*\\$(StringOfSizeZeroVerboseTemplate|StringOfSizeTwoTemplate)$");
"-XepOpt:Refaster:NamePattern=.*\\$(StringOfSizeZeroVerboseRule|StringOfSizeTwoRule)$");

@Test
void identification() {
Expand All @@ -64,13 +64,13 @@ void identification() {
"A.java",
"class A {",
" void m() {",
" // BUG: Diagnostic matches: StringOfSizeZeroTemplate",
" // BUG: Diagnostic matches: StringOfSizeZeroRule",
" boolean b1 = \"foo\".toCharArray().length == 0;",
" // BUG: Diagnostic matches: StringOfSizeOneTemplate",
" // BUG: Diagnostic matches: StringOfSizeOneRule",
" boolean b2 = \"bar\".toCharArray().length == 1;",
" // BUG: Diagnostic matches: StringOfSizeTwoTemplate",
" // BUG: Diagnostic matches: StringOfSizeTwoRule",
" boolean b3 = \"baz\".toCharArray().length == 2;",
" // BUG: Diagnostic matches: StringOfSizeThreeTemplate",
" // BUG: Diagnostic matches: StringOfSizeThreeRule",
" boolean b4 = \"qux\".toCharArray().length == 3;",
" }",
"}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private String getShortCheckName(String fullCheckName) {
String prefix = packageName() + '.';
checkState(
fullCheckName.startsWith(prefix),
"Refaster template class '%s' is not located in package '%s'",
"Refaster rule class '%s' is not located in package '%s'",
fullCheckName,
packageName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import java.lang.annotation.Target;

/**
* Describes the intent of a Refaster template or group of Refaster templates.
* Describes the intent of a Refaster rule or group of Refaster rules.
*
* <p>Annotations on nested classes override the description associated with any enclosing class.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Description {
/**
* A description of the annotated Refaster template(s).
* A description of the annotated Refaster rule(s).
*
* @return A non-{@code null} string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.lang.annotation.Target;

/**
* Describes the severity of a Refaster template or group of Refaster templates.
* Describes the severity of a Refaster rule or group of Refaster rules.
*
* <p>The default severity is the severity assigned to the {@code Refaster} bug checker, which may
* be controlled explicitly by running Error Prone with e.g. {@code -Xep:Refaster:WARN}. Annotations
Expand All @@ -17,7 +17,7 @@
@Retention(RetentionPolicy.SOURCE)
public @interface Severity {
/**
* The expected severity of any match of the annotated Refaster template(s).
* The expected severity of any match of the annotated Refaster rule(s).
*
* @return An Error Prone severity level.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* A collection of annotations that can be placed on Refaster template classes and Refaster template
* collection classes, thus influencing the way in which associated template matches are reported in
* A collection of annotations that can be placed on Refaster rule classes and Refaster rule
* collection classes, thus influencing the way in which associated rule matches are reported in
* non-patch mode.
*/
@com.google.errorprone.annotations.CheckReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ private static ImmutableRangeMap<Integer, String> indexRuleMatches(
ImmutableRangeMap.Builder<Integer, String> ruleMatches = ImmutableRangeMap.builder();

for (Description description : matches) {
String templateName = extractRefasterTemplateName(description);
String ruleName = extractRefasterRuleName(description);
Set<Replacement> replacements =
Iterables.getOnlyElement(description.fixes).getReplacements(endPositions);
for (Replacement replacement : replacements) {
ruleMatches.put(replacement.range(), templateName);
ruleMatches.put(replacement.range(), ruleName);
}
}

Expand Down Expand Up @@ -226,10 +226,10 @@ private void reportViolations(
state.reportMatch(describeMatch(tree, fixWithComment));
}

private static String extractRefasterTemplateName(Description description) {
private static String extractRefasterRuleName(Description description) {
String message = description.getRawMessage();
int index = message.indexOf(':');
checkState(index >= 0, "Failed to extract Refaster template name from string '%s'", message);
checkState(index >= 0, "Failed to extract Refaster rule name from string '%s'", message);
return getSubstringAfterFinalDelimiter('.', message.substring(0, index));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

/** Refaster rule collection to validate reporting of a match occurring in an unexpected place. */
@OnlineDocumentation
final class MatchInWrongMethodTemplates {
private MatchInWrongMethodTemplates() {}
final class MatchInWrongMethodRules {
private MatchInWrongMethodRules() {}

static final class StringIsEmpty {
@BeforeTemplate
Expand Down

0 comments on commit f3aa904

Please sign in to comment.