tech.picnic.errorprone.refasterrules.AssertJCharSequenceRulesRecipes 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.AssertJCharSequenceRules}.
*/
@SuppressWarnings("all")
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public class AssertJCharSequenceRulesRecipes extends Recipe {
/**
* Instantiates a new instance.
*/
public AssertJCharSequenceRulesRecipes() {}
@Override
public String getDisplayName() {
return "`AssertJCharSequenceRules` Refaster recipes";
}
@Override
public String getDescription() {
return "Refaster template recipes for `tech.picnic.errorprone.refasterrules.AssertJCharSequenceRules`. [Source](https://error-prone.picnic.tech/refasterrules/AssertJCharSequenceRules).";
}
@Override
public List getRecipeList() {
return Arrays.asList(
new AssertThatCharSequenceIsEmptyRecipe(),
new AssertThatCharSequenceIsNotEmptyRecipe(),
new AssertThatCharSequenceHasSizeRecipe()
);
}
/**
* OpenRewrite recipe created for Refaster template {@code AssertJCharSequenceRules.AssertThatCharSequenceIsEmpty}.
*/
@SuppressWarnings("all")
@NonNullApi
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public static class AssertThatCharSequenceIsEmptyRecipe extends Recipe {
/**
* Instantiates a new instance.
*/
public AssertThatCharSequenceIsEmptyRecipe() {}
@Override
public String getDisplayName() {
return "Refaster template `AssertJCharSequenceRules.AssertThatCharSequenceIsEmpty`";
}
@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\nstatic final class AssertThatCharSequenceIsEmpty {\n \n @BeforeTemplate\n void before(CharSequence charSequence) {\n Refaster.anyOf(assertThat(charSequence.length()).isEqualTo(0L), assertThat(charSequence.length()).isNotPositive());\n }\n \n @AfterTemplate\n @UseImportPolicy(value = STATIC_IMPORT_ALWAYS)\n void after(CharSequence charSequence) {\n assertThat(charSequence).isEmpty();\n }\n}\n```\n.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before$0 = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}.length()).isEqualTo(0L);")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate before$1 = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}.length()).isNotPositive();")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate after = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}).isEmpty();")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before$0.matcher(getCursor())).find()) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx,
SHORTEN_NAMES
);
}
if ((matcher = before$1.matcher(getCursor())).find()) {
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 UsesMethod<>("org.assertj.core.api.Assertions assertThat(..)"),
new UsesMethod<>("java.lang.CharSequence length(..)"),
Preconditions.or(
new UsesMethod<>("org.assertj.core.api.AbstractIntegerAssert isEqualTo(..)"),
new UsesMethod<>("org.assertj.core.api.AbstractIntegerAssert isNotPositive(..)")
)
),
javaVisitor
);
}
}
/**
* OpenRewrite recipe created for Refaster template {@code AssertJCharSequenceRules.AssertThatCharSequenceIsNotEmpty}.
*/
@SuppressWarnings("all")
@NonNullApi
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public static class AssertThatCharSequenceIsNotEmptyRecipe extends Recipe {
/**
* Instantiates a new instance.
*/
public AssertThatCharSequenceIsNotEmptyRecipe() {}
@Override
public String getDisplayName() {
return "Refaster template `AssertJCharSequenceRules.AssertThatCharSequenceIsNotEmpty`";
}
@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\nstatic final class AssertThatCharSequenceIsNotEmpty {\n \n @BeforeTemplate\n AbstractAssert, ?> before(CharSequence charSequence) {\n return Refaster.anyOf(assertThat(charSequence.length()).isNotEqualTo(0), assertThat(charSequence.length()).isPositive());\n }\n \n @AfterTemplate\n @UseImportPolicy(value = STATIC_IMPORT_ALWAYS)\n AbstractAssert, ?> after(CharSequence charSequence) {\n return assertThat(charSequence).isNotEmpty();\n }\n}\n```\n.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before$0 = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}.length()).isNotEqualTo(0)")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate before$1 = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}.length()).isPositive()")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate after = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}).isNotEmpty()")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before$0.matcher(getCursor())).find()) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)),
getCursor(),
ctx,
SHORTEN_NAMES
);
}
if ((matcher = before$1.matcher(getCursor())).find()) {
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<>("org.assertj.core.api.AbstractAssert", true),
new UsesMethod<>("org.assertj.core.api.Assertions assertThat(..)"),
new UsesMethod<>("java.lang.CharSequence length(..)"),
Preconditions.or(
new UsesMethod<>("org.assertj.core.api.AbstractIntegerAssert isNotEqualTo(..)"),
new UsesMethod<>("org.assertj.core.api.AbstractIntegerAssert isPositive(..)")
)
),
javaVisitor
);
}
}
/**
* OpenRewrite recipe created for Refaster template {@code AssertJCharSequenceRules.AssertThatCharSequenceHasSize}.
*/
@SuppressWarnings("all")
@NonNullApi
@Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor")
public static class AssertThatCharSequenceHasSizeRecipe extends Recipe {
/**
* Instantiates a new instance.
*/
public AssertThatCharSequenceHasSizeRecipe() {}
@Override
public String getDisplayName() {
return "Refaster template `AssertJCharSequenceRules.AssertThatCharSequenceHasSize`";
}
@Override
public String getDescription() {
return "Recipe created for the following Refaster template:\n```java\nstatic final class AssertThatCharSequenceHasSize {\n \n @BeforeTemplate\n AbstractAssert, ?> before(CharSequence charSequence, int length) {\n return assertThat(charSequence.length()).isEqualTo(length);\n }\n \n @AfterTemplate\n @UseImportPolicy(value = STATIC_IMPORT_ALWAYS)\n AbstractAssert, ?> after(CharSequence charSequence, int length) {\n return assertThat(charSequence).hasSize(length);\n }\n}\n```\n.";
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {
final JavaTemplate before = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}.length()).isEqualTo(#{length:any(int)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
final JavaTemplate after = JavaTemplate
.builder("org.assertj.core.api.Assertions.assertThat(#{charSequence:any(java.lang.CharSequence)}).hasSize(#{length:any(int)})")
.javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
JavaTemplate.Matcher matcher;
if ((matcher = before.matcher(getCursor())).find()) {
return embed(
after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)),
getCursor(),
ctx,
SHORTEN_NAMES
);
}
return super.visitMethodInvocation(elem, ctx);
}
};
return Preconditions.check(
Preconditions.and(
new UsesType<>("org.assertj.core.api.AbstractAssert", true),
new UsesMethod<>("org.assertj.core.api.AbstractIntegerAssert isEqualTo(..)"),
new UsesMethod<>("org.assertj.core.api.Assertions assertThat(..)"),
new UsesMethod<>("java.lang.CharSequence length(..)")
),
javaVisitor
);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy