-
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
Changes from 3 commits
78803d0
8837cb8
7a1df6b
2d02f11
def6acb
c85c43d
6f72820
ce2b2e1
50c1971
cbebe01
8e6af5a
ea03906
318ee5a
57b0990
b69fad2
7be352f
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,128 @@ | ||
/* | ||
* 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 java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.ScanningRecipe; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.TreeVisitor; | ||
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 static org.openrewrite.Preconditions.and; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public class MigrateHooksToReactorContextProperty extends ScanningRecipe<AtomicBoolean> { | ||
|
||
@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); | ||
} | ||
|
||
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.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,94 @@ | ||
/* | ||
* 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 static org.openrewrite.java.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.properties.Assertions.properties; | ||
|
||
public 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.