mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Consistently prefer "Refaster rule" over "Refaster template" (#286)
As the former term references a class containing one or more `@BeforeTemplate` methods, one or more `@Placeholder` methods and an optional `@AfterTemplate`, while the latter term more narrowly references a single `@BeforeTemplate` or `@AfterTemplate` method.
This commit is contained in:
committed by
GitHub
parent
7aef2cfe51
commit
128e178d37
@@ -11,7 +11,7 @@
|
||||
<artifactId>refaster-runner</artifactId>
|
||||
|
||||
<name>Picnic :: Error Prone Support :: Refaster Runner</name>
|
||||
<description>Exposes Refaster templates found on the classpath through a regular Error Prone check.</description>
|
||||
<description>Exposes Refaster rules found on the classpath through a regular Error Prone check.</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
@@ -15,11 +15,11 @@ import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Scans the classpath for {@value #REFASTER_TEMPLATE_SUFFIX} files and loads them as {@link
|
||||
* Scans the classpath for {@value #REFASTER_RULE_SUFFIX} files and loads them as {@link
|
||||
* CodeTransformer}s.
|
||||
*/
|
||||
public final class CodeTransformers {
|
||||
private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster";
|
||||
private static final String REFASTER_RULE_SUFFIX = ".refaster";
|
||||
private static final Supplier<ImmutableListMultimap<String, CodeTransformer>>
|
||||
ALL_CODE_TRANSFORMERS = Suppliers.memoize(CodeTransformers::loadAllCodeTransformers);
|
||||
|
||||
@@ -30,28 +30,28 @@ public final class CodeTransformers {
|
||||
*
|
||||
* <p>This method returns a cached view; all invocations except the first are very cheap.
|
||||
*
|
||||
* @return A mapping from Refaster template names to associated {@link CodeTransformer}s.
|
||||
* @return A mapping from Refaster rule names to associated {@link CodeTransformer}s.
|
||||
*/
|
||||
public static ImmutableListMultimap<String, CodeTransformer> getAllCodeTransformers() {
|
||||
return ALL_CODE_TRANSFORMERS.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans the classpath for compiled Refaster templates and returns the associated deserialized
|
||||
* {@link CodeTransformer}s, indexed by their name.
|
||||
* Scans the classpath for compiled Refaster rules and returns the associated deserialized {@link
|
||||
* CodeTransformer}s, indexed by their name.
|
||||
*
|
||||
* @return A mapping from Refaster template names to associated {@link CodeTransformer}s.
|
||||
* @return A mapping from Refaster rule names to associated {@link CodeTransformer}s.
|
||||
*/
|
||||
private static ImmutableListMultimap<String, CodeTransformer> loadAllCodeTransformers() {
|
||||
ImmutableListMultimap.Builder<String, CodeTransformer> transformers =
|
||||
ImmutableListMultimap.builder();
|
||||
|
||||
for (ResourceInfo resource : getClassPathResources()) {
|
||||
getRefasterTemplateName(resource)
|
||||
getRefasterRuleName(resource)
|
||||
.ifPresent(
|
||||
templateName ->
|
||||
ruleName ->
|
||||
loadCodeTransformer(resource)
|
||||
.ifPresent(transformer -> transformers.put(templateName, transformer)));
|
||||
.ifPresent(transformer -> transformers.put(ruleName, transformer)));
|
||||
}
|
||||
|
||||
return transformers.build();
|
||||
@@ -65,15 +65,15 @@ public final class CodeTransformers {
|
||||
}
|
||||
}
|
||||
|
||||
private static Optional<String> getRefasterTemplateName(ResourceInfo resource) {
|
||||
private static Optional<String> getRefasterRuleName(ResourceInfo resource) {
|
||||
String resourceName = resource.getResourceName();
|
||||
if (!resourceName.endsWith(REFASTER_TEMPLATE_SUFFIX)) {
|
||||
if (!resourceName.endsWith(REFASTER_RULE_SUFFIX)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
int lastPathSeparator = resourceName.lastIndexOf('/');
|
||||
int beginIndex = lastPathSeparator < 0 ? 0 : lastPathSeparator + 1;
|
||||
int endIndex = resourceName.length() - REFASTER_TEMPLATE_SUFFIX.length();
|
||||
int endIndex = resourceName.length() - REFASTER_RULE_SUFFIX.length();
|
||||
return Optional.of(resourceName.substring(beginIndex, endIndex));
|
||||
}
|
||||
|
||||
|
||||
@@ -37,11 +37,11 @@ import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A {@link BugChecker} that flags code that can be simplified using Refaster templates located on
|
||||
* the classpath.
|
||||
* A {@link BugChecker} that flags code that can be simplified using Refaster rules located on the
|
||||
* classpath.
|
||||
*
|
||||
* <p>This checker locates all {@code *.refaster} classpath resources and assumes they contain a
|
||||
* {@link CodeTransformer}. The set of loaded Refaster templates can be restricted by passing {@code
|
||||
* {@link CodeTransformer}. The set of loaded Refaster rules can be restricted by passing {@code
|
||||
* -XepOpt:Refaster:NamePattern=<someRegex>}.
|
||||
*/
|
||||
@AutoService(BugChecker.class)
|
||||
@@ -51,8 +51,8 @@ import java.util.stream.Stream;
|
||||
severity = SUGGESTION,
|
||||
tags = SIMPLIFICATION)
|
||||
public final class Refaster extends BugChecker implements CompilationUnitTreeMatcher {
|
||||
/** Flag to pass a pattern that restricts which Refaster templates are loaded. */
|
||||
public static final String INCLUDED_TEMPLATES_PATTERN_FLAG = "Refaster:NamePattern";
|
||||
/** Flag to pass a pattern that restricts which Refaster rules are loaded. */
|
||||
public static final String INCLUDED_RULES_PATTERN_FLAG = "Refaster:NamePattern";
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -151,7 +151,7 @@ public final class Refaster extends BugChecker implements CompilationUnitTreeMat
|
||||
CodeTransformers.getAllCodeTransformers();
|
||||
return CompositeCodeTransformer.compose(
|
||||
flags
|
||||
.get(INCLUDED_TEMPLATES_PATTERN_FLAG)
|
||||
.get(INCLUDED_RULES_PATTERN_FLAG)
|
||||
.map(Pattern::compile)
|
||||
.<ImmutableCollection<CodeTransformer>>map(
|
||||
nameFilter -> filterCodeTransformers(allTransformers, nameFilter))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/** Exposes Refaster templates found on the classpath through a regular Error Prone check. */
|
||||
/** Exposes Refaster rules found on the classpath through a regular Error Prone check. */
|
||||
@com.google.errorprone.annotations.CheckReturnValue
|
||||
@javax.annotation.ParametersAreNonnullByDefault
|
||||
package tech.picnic.errorprone.refaster.runner;
|
||||
|
||||
@@ -7,11 +7,11 @@ import org.junit.jupiter.api.Test;
|
||||
final class CodeTransformersTest {
|
||||
/**
|
||||
* Verifies that {@link CodeTransformers#getAllCodeTransformers()} finds the code transformer
|
||||
* compiled from {@link FooTemplates} on the classpath.
|
||||
* compiled from {@link FooRules} on the classpath.
|
||||
*/
|
||||
@Test
|
||||
void getAllCodeTransformers() {
|
||||
assertThat(CodeTransformers.getAllCodeTransformers().keySet())
|
||||
.containsExactly("FooTemplates$SimpleTemplate");
|
||||
.containsExactly("FooRules$SimpleRule");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ package tech.picnic.errorprone.refaster.runner;
|
||||
import com.google.errorprone.refaster.annotation.AfterTemplate;
|
||||
import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
||||
|
||||
/** An example template collection used to test {@link CodeTransformers}. */
|
||||
final class FooTemplates {
|
||||
private FooTemplates() {}
|
||||
/** An example rule collection used to test {@link CodeTransformers}. */
|
||||
final class FooRules {
|
||||
private FooRules() {}
|
||||
|
||||
/** Simple template for testing purposes. */
|
||||
static final class SimpleTemplate {
|
||||
/** Simple rule for testing purposes. */
|
||||
static final class SimpleRule {
|
||||
@BeforeTemplate
|
||||
boolean before(String string) {
|
||||
return string.toCharArray().length == 0;
|
||||
Reference in New Issue
Block a user