-
Notifications
You must be signed in to change notification settings - Fork 106
Add ScanningRecipe for migrate Hooks.enableAutomaticContextPropagation()
to Spring property
#772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,123 @@ | ||||||||||||||
/* | ||||||||||||||
* Copyright 2024 the original author or authors. | ||||||||||||||
* <p> | ||||||||||||||
* Licensed under the Moderne Source Available License (the "License"); | ||||||||||||||
* you may not use this file except in compliance with the License. | ||||||||||||||
* You may obtain a copy of the License at | ||||||||||||||
* <p> | ||||||||||||||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||||||||||||||
* <p> | ||||||||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
* See the License for the specific language governing permissions and | ||||||||||||||
* limitations under the License. | ||||||||||||||
*/ | ||||||||||||||
package org.openrewrite.java.spring.boot3; | ||||||||||||||
|
||||||||||||||
import org.jspecify.annotations.Nullable; | ||||||||||||||
import org.openrewrite.*; | ||||||||||||||
import org.openrewrite.java.JavaIsoVisitor; | ||||||||||||||
import org.openrewrite.java.MethodMatcher; | ||||||||||||||
import org.openrewrite.java.search.UsesType; | ||||||||||||||
import org.openrewrite.java.spring.AddSpringProperty; | ||||||||||||||
import org.openrewrite.java.tree.J; | ||||||||||||||
import org.openrewrite.properties.tree.Properties; | ||||||||||||||
import org.openrewrite.yaml.tree.Yaml; | ||||||||||||||
|
||||||||||||||
import java.util.concurrent.atomic.AtomicBoolean; | ||||||||||||||
|
||||||||||||||
import static org.openrewrite.Preconditions.and; | ||||||||||||||
|
||||||||||||||
public class MigrateHooksToReactorContextProperty extends ScanningRecipe<AtomicBoolean> { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also might be good to hook this recipe into one of the larger composites, or Spring Boot best practices, such that it's run automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This recipe can be applicable from Spring Boot 3.2. I believe it would be a good fit for the Migrate to Spring Boot 3.2 recipe. If not, could you please provide some guidance on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That then makes sense, yes thanks! |
||||||||||||||
@Override | ||||||||||||||
public String getDisplayName() { | ||||||||||||||
return "Use `spring.reactor.context-propagation` property"; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public String getDescription() { | ||||||||||||||
return "Replace `Hooks.enableAutomaticContextPropagation()` with `spring.reactor.context-propagation=true`."; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public AtomicBoolean getInitialValue(ExecutionContext ctx) { | ||||||||||||||
return new AtomicBoolean(false); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While perhaps not as common in practice, we'd need to correctly handle multi module projects too, which might each have this hook and a configuration properties file, or multiple. You can look at this example for how to handle that with an alternative Accumulator: Lines 245 to 250 in e7a8b99
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static final String SPRING_BOOT_APPLICATION_FQN = "org.springframework.boot.autoconfigure.SpringBootApplication"; | ||||||||||||||
private static final String HOOKS_TYPE = "reactor.core.publisher.Hooks"; | ||||||||||||||
private static final MethodMatcher HOOKS_MATCHER = new MethodMatcher("reactor.core.publisher.Hooks enableAutomaticContextPropagation()"); | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean foundHooksInSpringApp) { | ||||||||||||||
return Preconditions.check( | ||||||||||||||
and( | ||||||||||||||
new UsesType<>(SPRING_BOOT_APPLICATION_FQN, true), | ||||||||||||||
new UsesType<>(HOOKS_TYPE, true) | ||||||||||||||
), | ||||||||||||||
new JavaIsoVisitor<ExecutionContext>() { | ||||||||||||||
@Override | ||||||||||||||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||||||||||||||
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); | ||||||||||||||
|
||||||||||||||
if (HOOKS_MATCHER.matches(mi)) { | ||||||||||||||
foundHooksInSpringApp.set(true); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return mi; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean foundHooksInSpringApp) { | ||||||||||||||
if (!foundHooksInSpringApp.get()) { | ||||||||||||||
return TreeVisitor.noop(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return new TreeVisitor<Tree, ExecutionContext>() { | ||||||||||||||
@Override | ||||||||||||||
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) { | ||||||||||||||
if (tree instanceof J.CompilationUnit) { | ||||||||||||||
return new HooksRemovalVisitor().visitNonNull(tree, ctx); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (isApplicationProperties(tree)) { | ||||||||||||||
return addSpringProperty(ctx, tree, "spring.reactor.context-propagation", "true"); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return tree; | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static boolean isApplicationProperties(@Nullable Tree tree) { | ||||||||||||||
return (tree instanceof Properties.File && | ||||||||||||||
"application.properties".equals(((Properties.File) tree).getSourcePath().getFileName().toString())) || | ||||||||||||||
(tree instanceof Yaml.Documents && | ||||||||||||||
((Yaml.Documents) tree).getSourcePath().getFileName().toString().matches("application\\.ya*ml")); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static Tree addSpringProperty(ExecutionContext ctx, Tree properties, String property, String value) { | ||||||||||||||
return new AddSpringProperty(property, value, null, null) | ||||||||||||||
.getVisitor() | ||||||||||||||
.visitNonNull(properties, ctx); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private static class HooksRemovalVisitor extends JavaIsoVisitor<ExecutionContext> { | ||||||||||||||
@Override | ||||||||||||||
public J.@Nullable MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||||||||||||||
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); | ||||||||||||||
|
||||||||||||||
// Only remove if we're in a @SpringBootApplication class | ||||||||||||||
if (HOOKS_MATCHER.matches(mi)) { | ||||||||||||||
maybeRemoveImport(HOOKS_TYPE); | ||||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return mi; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.java.spring.boot3; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.properties.Assertions.properties; | ||
|
||
class MigrateHooksToReactorContextPropertyTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new MigrateHooksToReactorContextProperty()); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void replaceMethodCallWithProperty() { | ||
rewriteRun( | ||
spec -> spec.recipe(new MigrateHooksToReactorContextProperty()), | ||
java( | ||
""" | ||
package org.springframework.boot.autoconfigure; | ||
public @interface SpringBootApplication {} | ||
""" | ||
), | ||
java( | ||
""" | ||
package org.springframework.boot; | ||
public class SpringApplication { | ||
public static void run(Class<?> cls, String[] args) {} | ||
} | ||
""" | ||
), | ||
java( | ||
""" | ||
package reactor.core.publisher; | ||
public class Hooks { | ||
public static void enableAutomaticContextPropagation() {} | ||
} | ||
""" | ||
), | ||
java( | ||
""" | ||
import reactor.core.publisher.Hooks; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MyApplication { | ||
public static void main(String[] args) { | ||
Hooks.enableAutomaticContextPropagation(); | ||
SpringApplication.run(MyApplication.class, args); | ||
} | ||
} | ||
""", | ||
""" | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class MyApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(MyApplication.class, args); | ||
} | ||
} | ||
""" | ||
), | ||
properties( | ||
"", | ||
"spring.reactor.context-propagation=true", | ||
spec -> spec.path("application.properties") | ||
) | ||
); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.