Skip to content

Commit

Permalink
Drop the old runtime Spring Core dependency brought in through spring…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
timtebeek authored Nov 14, 2024
1 parent cdf343c commit 1c8d450
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
12 changes: 2 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ recipeDependencies {
parserClasspath("org.springframework.data:spring-data-commons:1.+")
parserClasspath("org.springframework.data:spring-data-jpa:2.+")
parserClasspath("org.springframework.data:spring-data-jpa:2.3.+")
parserClasspath("org.springframework.data:spring-data-mongodb:2.2.+")
parserClasspath("org.mongodb:mongo-java-driver:3.12.+")

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

annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion")
compileOnly("com.google.errorprone:error_prone_core:2.+") {
exclude("com.google.auto.service", "auto-service-annotations")
}
implementation("org.mongodb:mongo-java-driver:3.12.+")
implementation("org.springframework.data:spring-data-mongodb:2.2.+"){
because("We only require the classes (for refaster style recipes), not the dependencies")
exclude(group = "org.springframework")
}

testRuntimeOnly("ch.qos.logback:logback-classic:1.+")
testRuntimeOnly(gradleApi())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,64 @@
*/
package org.openrewrite.java.spring.data;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.mongodb.MongoClientURI;
import org.openrewrite.java.template.RecipeDescriptor;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.MethodCall;

@RecipeDescriptor(
name = "Use `new SimpleMongoClientDbFactory(String)`",
description = "Replace usage of deprecated `new SimpleMongoDbFactory(new MongoClientURI(String))` with `new SimpleMongoClientDbFactory(String)`.")
@SuppressWarnings("deprecation")
public class RefactorSimpleMongoDbFactory {
public class RefactorSimpleMongoDbFactory extends Recipe {

@BeforeTemplate
public MongoDbFactory before(String uri) {
return new SimpleMongoDbFactory(new MongoClientURI(uri));
private static final String COM_MONGODB_MONGO_CLIENT_URI = "com.mongodb.MongoClientURI";
private static final String SIMPLE_MONGO_DB_FACTORY = "org.springframework.data.mongodb.core.SimpleMongoDbFactory";
private static final MethodMatcher SIMPLE_FACTORY_CONSTRUCTOR = new MethodMatcher(SIMPLE_MONGO_DB_FACTORY + " <constructor>(..)", true);
private static final MethodMatcher MONGO_CLIENT_CONSTRUCTOR = new MethodMatcher(COM_MONGODB_MONGO_CLIENT_URI + " <constructor>(String)", true);

private static final String SIMPLE_MONGO_CLIENT_DB_FACTORY = "org.springframework.data.mongodb.core.SimpleMongoClientDbFactory";

@Override
public String getDisplayName() {
return "Use `new SimpleMongoClientDbFactory(String)`";
}

@Override
public String getDescription() {
return "Replace usage of deprecated `new SimpleMongoDbFactory(new MongoClientURI(String))` with `new SimpleMongoClientDbFactory(String)`.";
}

@AfterTemplate
public MongoDbFactory after(String uri) {
return new SimpleMongoClientDbFactory(uri);
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> javaVisitor = new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
if (SIMPLE_FACTORY_CONSTRUCTOR.matches(newClass)) {
Expression expression = newClass.getArguments().get(0);
if (MONGO_CLIENT_CONSTRUCTOR.matches(expression)) {
Expression uri = ((MethodCall) expression).getArguments().get(0);
maybeRemoveImport(SIMPLE_MONGO_DB_FACTORY);
maybeRemoveImport(COM_MONGODB_MONGO_CLIENT_URI);
maybeAddImport(SIMPLE_MONGO_CLIENT_DB_FACTORY);
return JavaTemplate
.builder("new SimpleMongoClientDbFactory(#{any(java.lang.String)})")
.imports(SIMPLE_MONGO_CLIENT_DB_FACTORY)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-data-mongodb-2", "mongo-java-driver"))
.build()
.apply(getCursor(), newClass.getCoordinates().replace(), uri);
}
}
return super.visitNewClass(newClass, ctx);
}
};
return Preconditions.check(
Preconditions.and(
new UsesMethod<>(SIMPLE_FACTORY_CONSTRUCTOR),
new UsesMethod<>(MONGO_CLIENT_CONSTRUCTOR)
),
javaVisitor
);
}
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/spring-data-23.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ recipeList:
artifactId: mongodb-driver-legacy
version: 5.1.x
onlyIfUsing: com.mongodb.MongoClientURI
- org.openrewrite.java.spring.data.RefactorSimpleMongoDbFactoryRecipe
- org.openrewrite.java.spring.data.RefactorSimpleMongoDbFactory
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.mongodb.MongoDbFactory
newFullyQualifiedTypeName: org.springframework.data.mongodb.MongoDatabaseFactory
Expand Down

0 comments on commit 1c8d450

Please sign in to comment.