Skip to content

Commit

Permalink
Get rid of static JavaParser field to avoid classloading issues on th…
Browse files Browse the repository at this point in the history
…e saas
  • Loading branch information
sambsnyd committed Jun 19, 2023
1 parent a75dafd commit 4b029f7
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,6 @@ public class UseReactivePanacheMongoEntityBaseUniT extends Recipe {
private static final MethodMatcher PERSIST_MATCHER = new MethodMatcher("io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase persist()");
private static final MethodMatcher UPDATE_MATCHER = new MethodMatcher("io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase update()");
private static final MethodMatcher PERSIST_OR_UPDATE_MATCHER = new MethodMatcher("io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase persistOrUpdate()");
private static final JavaParser.Builder<?, ?> PARSER =
JavaParser.fromJavaVersion().dependsOn(
Stream.of(
Parser.Input.fromString("" +
"package io.smallrye.mutiny;" +
"public interface Uni<T> {" +
" Uni<Void> replaceWithVoid() {};" +
"}"
),
Parser.Input.fromString("" +
"package io.quarkus.mongodb.panache.reactive;" +
"import io.smallrye.mutiny.Uni;" +
"public abstract class ReactivePanacheMongoEntityBase {" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> persist() {};" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> update() {};" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> persistOrUpdate() {};" +
"}"
)
).collect(Collectors.toList())
);

@Override
public String getDisplayName() {
Expand All @@ -74,6 +54,28 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private static class UseReactivePanacheMongoEntityBaseUniTVisitor extends JavaIsoVisitor<ExecutionContext> {

private static final JavaParser.Builder<?, ?> parser =
JavaParser.fromJavaVersion().dependsOn(
Stream.of(
Parser.Input.fromString("" +
"package io.smallrye.mutiny;" +
"public interface Uni<T> {" +
" Uni<Void> replaceWithVoid() {};" +
"}"
),
Parser.Input.fromString("" +
"package io.quarkus.mongodb.panache.reactive;" +
"import io.smallrye.mutiny.Uni;" +
"public abstract class ReactivePanacheMongoEntityBase {" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> persist() {};" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> update() {};" +
" public <T extends ReactivePanacheMongoEntityBase> Uni<T> persistOrUpdate() {};" +
"}"
)
).collect(Collectors.toList())
);

private static boolean hasVoidParameterization(J.MethodInvocation method) {
JavaType.Parameterized returnType = TypeUtils.asParameterized(method.getType());
if (returnType != null) {
Expand All @@ -91,23 +93,23 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (PERSIST_MATCHER.matches(mi)) {
if (hasVoidParameterization(mi)) {
mi = JavaTemplate.builder("#{any(io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase)}.persist().replaceWithVoid()")
.javaParser(PARSER)
.javaParser(parser)
.build().apply(new Cursor(getCursor().getParent(), mi),
mi.getCoordinates().replace(),
mi.getSelect());
}
} else if (UPDATE_MATCHER.matches(mi)) {
if (hasVoidParameterization(mi)) {
mi = JavaTemplate.builder("#{any(io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase)}.update().replaceWithVoid()")
.javaParser(PARSER)
.javaParser(parser)
.build().apply(new Cursor(getCursor().getParent(), mi),
mi.getCoordinates().replace(),
mi.getSelect());
}
} else if (PERSIST_OR_UPDATE_MATCHER.matches(mi)) {
if (hasVoidParameterization(mi)) {
mi = JavaTemplate.builder("#{any(io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase)}.persistOrUpdate().replaceWithVoid()")
.javaParser(PARSER)
.javaParser(parser)
.build().apply(new Cursor(getCursor().getParent(), mi),
mi.getCoordinates().replace(),
mi.getSelect());
Expand Down

0 comments on commit 4b029f7

Please sign in to comment.