Skip to content

Commit 1c8d450

Browse files
authored
Drop the old runtime Spring Core dependency brought in through spring-data-mongodb:2.+ (#626)
* Drop the old runtime Spring Core dependency As that's blocked due to vulnerabilities in some places. * Remove excess newline * Fix reference in yaml * Adopt spring-data-mongodb-2 * Adopt spring-data-mongodb 2.2 specifically * Add missing mongo-java-driver
1 parent cdf343c commit 1c8d450

File tree

5 files changed

+58
-29
lines changed

5 files changed

+58
-29
lines changed

build.gradle.kts

+2-10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ recipeDependencies {
8787
parserClasspath("org.springframework.data:spring-data-commons:1.+")
8888
parserClasspath("org.springframework.data:spring-data-jpa:2.+")
8989
parserClasspath("org.springframework.data:spring-data-jpa:2.3.+")
90+
parserClasspath("org.springframework.data:spring-data-mongodb:2.2.+")
91+
parserClasspath("org.mongodb:mongo-java-driver:3.12.+")
9092

9193
parserClasspath("org.springframework.batch:spring-batch-core:4.+")
9294
parserClasspath("org.springframework.batch:spring-batch-core:5.+")
@@ -147,16 +149,6 @@ dependencies {
147149
runtimeOnly("org.openrewrite.recipe:rewrite-reactive-streams:$rewriteVersion")
148150
runtimeOnly("org.openrewrite.recipe:rewrite-testing-frameworks:$rewriteVersion")
149151

150-
annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion")
151-
compileOnly("com.google.errorprone:error_prone_core:2.+") {
152-
exclude("com.google.auto.service", "auto-service-annotations")
153-
}
154-
implementation("org.mongodb:mongo-java-driver:3.12.+")
155-
implementation("org.springframework.data:spring-data-mongodb:2.2.+"){
156-
because("We only require the classes (for refaster style recipes), not the dependencies")
157-
exclude(group = "org.springframework")
158-
}
159-
160152
testRuntimeOnly("ch.qos.logback:logback-classic:1.+")
161153
testRuntimeOnly(gradleApi())
162154

src/main/java/org/openrewrite/java/spring/data/RefactorSimpleMongoDbFactory.java

+55-18
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,64 @@
1515
*/
1616
package org.openrewrite.java.spring.data;
1717

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;
2527

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 {
3129

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)`.";
3545
}
3646

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+
);
4077
}
4178
}
Binary file not shown.

src/main/resources/META-INF/rewrite/spring-data-23.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ recipeList:
2828
artifactId: mongodb-driver-legacy
2929
version: 5.1.x
3030
onlyIfUsing: com.mongodb.MongoClientURI
31-
- org.openrewrite.java.spring.data.RefactorSimpleMongoDbFactoryRecipe
31+
- org.openrewrite.java.spring.data.RefactorSimpleMongoDbFactory
3232
- org.openrewrite.java.ChangeType:
3333
oldFullyQualifiedTypeName: org.springframework.data.mongodb.MongoDbFactory
3434
newFullyQualifiedTypeName: org.springframework.data.mongodb.MongoDatabaseFactory

0 commit comments

Comments
 (0)