|
15 | 15 | */
|
16 | 16 | package org.openrewrite.java.spring.data;
|
17 | 17 |
|
18 |
| -import com.google.errorprone.refaster.annotation.AfterTemplate; |
19 |
| -import com.google.errorprone.refaster.annotation.BeforeTemplate; |
20 |
| -import com.mongodb.MongoClientURI; |
21 |
| -import org.openrewrite.java.template.RecipeDescriptor; |
22 |
| -import org.springframework.data.mongodb.MongoDbFactory; |
23 |
| -import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory; |
24 |
| -import org.springframework.data.mongodb.core.SimpleMongoDbFactory; |
| 18 | +import org.openrewrite.ExecutionContext; |
| 19 | +import org.openrewrite.Preconditions; |
| 20 | +import org.openrewrite.Recipe; |
| 21 | +import org.openrewrite.TreeVisitor; |
| 22 | +import org.openrewrite.java.*; |
| 23 | +import org.openrewrite.java.search.UsesMethod; |
| 24 | +import org.openrewrite.java.tree.Expression; |
| 25 | +import org.openrewrite.java.tree.J; |
| 26 | +import org.openrewrite.java.tree.MethodCall; |
25 | 27 |
|
26 |
| -@RecipeDescriptor( |
27 |
| - name = "Use `new SimpleMongoClientDbFactory(String)`", |
28 |
| - description = "Replace usage of deprecated `new SimpleMongoDbFactory(new MongoClientURI(String))` with `new SimpleMongoClientDbFactory(String)`.") |
29 |
| -@SuppressWarnings("deprecation") |
30 |
| -public class RefactorSimpleMongoDbFactory { |
| 28 | +public class RefactorSimpleMongoDbFactory extends Recipe { |
31 | 29 |
|
32 |
| - @BeforeTemplate |
33 |
| - public MongoDbFactory before(String uri) { |
34 |
| - return new SimpleMongoDbFactory(new MongoClientURI(uri)); |
| 30 | + private static final String COM_MONGODB_MONGO_CLIENT_URI = "com.mongodb.MongoClientURI"; |
| 31 | + private static final String SIMPLE_MONGO_DB_FACTORY = "org.springframework.data.mongodb.core.SimpleMongoDbFactory"; |
| 32 | + private static final MethodMatcher SIMPLE_FACTORY_CONSTRUCTOR = new MethodMatcher(SIMPLE_MONGO_DB_FACTORY + " <constructor>(..)", true); |
| 33 | + private static final MethodMatcher MONGO_CLIENT_CONSTRUCTOR = new MethodMatcher(COM_MONGODB_MONGO_CLIENT_URI + " <constructor>(String)", true); |
| 34 | + |
| 35 | + private static final String SIMPLE_MONGO_CLIENT_DB_FACTORY = "org.springframework.data.mongodb.core.SimpleMongoClientDbFactory"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDisplayName() { |
| 39 | + return "Use `new SimpleMongoClientDbFactory(String)`"; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getDescription() { |
| 44 | + return "Replace usage of deprecated `new SimpleMongoDbFactory(new MongoClientURI(String))` with `new SimpleMongoClientDbFactory(String)`."; |
35 | 45 | }
|
36 | 46 |
|
37 |
| - @AfterTemplate |
38 |
| - public MongoDbFactory after(String uri) { |
39 |
| - return new SimpleMongoClientDbFactory(uri); |
| 47 | + @Override |
| 48 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 49 | + JavaVisitor<ExecutionContext> javaVisitor = new JavaIsoVisitor<ExecutionContext>() { |
| 50 | + @Override |
| 51 | + public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { |
| 52 | + if (SIMPLE_FACTORY_CONSTRUCTOR.matches(newClass)) { |
| 53 | + Expression expression = newClass.getArguments().get(0); |
| 54 | + if (MONGO_CLIENT_CONSTRUCTOR.matches(expression)) { |
| 55 | + Expression uri = ((MethodCall) expression).getArguments().get(0); |
| 56 | + maybeRemoveImport(SIMPLE_MONGO_DB_FACTORY); |
| 57 | + maybeRemoveImport(COM_MONGODB_MONGO_CLIENT_URI); |
| 58 | + maybeAddImport(SIMPLE_MONGO_CLIENT_DB_FACTORY); |
| 59 | + return JavaTemplate |
| 60 | + .builder("new SimpleMongoClientDbFactory(#{any(java.lang.String)})") |
| 61 | + .imports(SIMPLE_MONGO_CLIENT_DB_FACTORY) |
| 62 | + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-data-mongodb-2", "mongo-java-driver")) |
| 63 | + .build() |
| 64 | + .apply(getCursor(), newClass.getCoordinates().replace(), uri); |
| 65 | + } |
| 66 | + } |
| 67 | + return super.visitNewClass(newClass, ctx); |
| 68 | + } |
| 69 | + }; |
| 70 | + return Preconditions.check( |
| 71 | + Preconditions.and( |
| 72 | + new UsesMethod<>(SIMPLE_FACTORY_CONSTRUCTOR), |
| 73 | + new UsesMethod<>(MONGO_CLIENT_CONSTRUCTOR) |
| 74 | + ), |
| 75 | + javaVisitor |
| 76 | + ); |
40 | 77 | }
|
41 | 78 | }
|
0 commit comments