tech.picnic.errorprone.refasterrules.PatternRulesRecipes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of error-prone-contrib Show documentation
Show all versions of error-prone-contrib Show documentation
Extra Error Prone plugins by Picnic.
package tech.picnic.errorprone.refasterrules;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.*;
import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.template.function.*;
import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor;
import org.openrewrite.java.tree.*;
import javax.annotation.Generated;
import java.util.*;
import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;
/**
* OpenRewrite recipes created for Refaster template {@code tech.picnic.errorprone.refasterrules.PatternRules}.
*/
@SuppressWarnings("all")
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public class PatternRulesRecipes extends Recipe {
/**
* Instantiates a new instance.
*/
public PatternRulesRecipes() {}
@Override
public String getDisplayName() {
return "Refaster rules related to code dealing with regular expressions";
}
@Override
public String getDescription() {
return "Refaster template recipes for `tech.picnic.errorprone.refasterrules.PatternRules`. [Source](https://error-prone.picnic.tech/refasterrules/PatternRules).";
}
@Override
public List getRecipeList() {
return Arrays.asList(
new PatternAsPredicateRecipe(),
new PatternCompileAsPredicateRecipe()
);
}
/**
* OpenRewrite recipe created for Refaster template {@code PatternRules.PatternAsPredicate}.
*/
@SuppressWarnings("all")
@NonNullApi
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public static class PatternAsPredicateRecipe extends Recipe {
/**
* Instantiates a new instance.
*/
public PatternAsPredicateRecipe() {}
@Override
public String getDisplayName() {
return "Prefer `Pattern#asPredicate()` over non-JDK alternatives";
}
@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\nstatic final class PatternAsPredicate {\n \n @BeforeTemplate\n Predicate before(Pattern pattern) {\n return Predicates.contains(pattern);\n }\n \n @AfterTemplate\n Predicate after(Pattern pattern) {\n return pattern.asPredicate();\n }\n}\n```\n.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before = JavaTemplate
.builder("com.google.common.base.Predicates.contains(#{pattern:any(java.util.regex.Pattern)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate after = JavaTemplate
.builder("#{pattern:any(java.util.regex.Pattern)}.asPredicate()")
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeRemoveImport("com.google.common.base.Predicates");
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
}
};
return Preconditions.check(
Preconditions.and(
new UsesType<>("com.google.common.base.Predicates", true),
new UsesType<>("java.util.function.Predicate", true),
new UsesType<>("java.util.regex.Pattern", true),
new UsesMethod<>("com.google.common.base.Predicates contains(..)")
),
javaVisitor
);
}
}
/**
* OpenRewrite recipe created for Refaster template {@code PatternRules.PatternCompileAsPredicate}.
*/
@SuppressWarnings("all")
@NonNullApi
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public static class PatternCompileAsPredicateRecipe extends Recipe {
/**
* Instantiates a new instance.
*/
public PatternCompileAsPredicateRecipe() {}
@Override
public String getDisplayName() {
return "Prefer `Pattern#asPredicate()` over non-JDK alternatives";
}
@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\nstatic final class PatternCompileAsPredicate {\n \n @BeforeTemplate\n Predicate before(String pattern) {\n return containsPattern(pattern);\n }\n \n @AfterTemplate\n Predicate after(String pattern) {\n return Pattern.compile(pattern).asPredicate();\n }\n}\n```\n.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before = JavaTemplate
.builder("com.google.common.base.Predicates.containsPattern(#{pattern:any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate after = JavaTemplate
.builder("java.util.regex.Pattern.compile(#{pattern:any(java.lang.String)}).asPredicate()")
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
maybeRemoveImport("com.google.common.base.Predicates.containsPattern");
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
}
};
return Preconditions.check(
Preconditions.and(
new UsesType<>("java.util.function.Predicate", true),
new UsesMethod<>("com.google.common.base.Predicates containsPattern(..)")
),
javaVisitor
);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy