Skip to content

Commit

Permalink
Drop the old runtime Spring Core dependency
Browse files Browse the repository at this point in the history
As that's blocked due to vulnerabilities in some places.
  • Loading branch information
timtebeek committed Nov 14, 2024
1 parent 45a49eb commit ffe3428
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
11 changes: 1 addition & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ 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:4.+")

parserClasspath("org.springframework.batch:spring-batch-core:4.+")
parserClasspath("org.springframework.batch:spring-batch-core:5.+")
Expand Down Expand Up @@ -147,16 +148,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,60 @@
*/
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;

@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 SIMPLE_MONGO_DB_FACTORY = "org.springframework.data.mongodb.core.SimpleMongoDbFactory";
private static final String COM_MONGODB_MONGO_CLIENT_URI = "com.mongodb.MongoClientURI";
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);

@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 = ((J.MethodInvocation) expression).getArguments().get(0);
maybeRemoveImport(SIMPLE_MONGO_DB_FACTORY);
maybeRemoveImport(COM_MONGODB_MONGO_CLIENT_URI);
return JavaTemplate
.builder("new org.springframework.data.mongodb.core.SimpleMongoClientDbFactory(#{any(java.lang.String)})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-data-mongodb"))
.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.

0 comments on commit ffe3428

Please sign in to comment.