-
Notifications
You must be signed in to change notification settings - Fork 101
Add recipe to replace ManagedBean with Named annotation #753
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3848f6b
Add recipe and test for replacing ManagedBean with Named annotation
evie-lau 967c632
Update description with EE levels
evie-lau e7b0da7
Copyright and formatting
evie-lau 289fde7
Apply suggestions from code review
evie-lau 65a10a0
Add jakarta inject jar, use precondition
evie-lau 3b6b423
Simplify getting annotation name value
evie-lau 203be88
Use a single annotation matcher with a wildcard
timtebeek 54d481b
Merge branch 'main' into managedBeanToNamed
timtebeek 64a5772
Update faces recipe lists
evie-lau 18a4938
Add tags to recipe
evie-lau 65eb1e0
Cleanup
evie-lau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/main/java/org/openrewrite/java/migrate/jakarta/UpdateManagedBeanToNamed.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2025 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.migrate.jakarta; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.AnnotationMatcher; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.trait.Annotated; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static org.openrewrite.java.trait.Traits.annotated; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class UpdateManagedBeanToNamed extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Update Faces `@ManagedBean` to use CDI `@Named`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Faces ManagedBean was deprecated in JSF 2.3 (EE8) and removed in Jakarta Faces 4.0 (EE10). " + | ||
"Replace `@ManagedBean` with `@Named` for CDI-based bean management."; | ||
} | ||
|
||
@Override | ||
public Set<String> getTags() { | ||
return new HashSet<>(Arrays.asList("jakarta", "faces", "jsf")); | ||
} | ||
|
||
private static final AnnotationMatcher MANAGED_BEAN_MATCHER = new AnnotationMatcher("*.faces.bean.ManagedBean"); | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Preconditions.Check(new UsesType<>("*.faces.bean.ManagedBean", false), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ctx) { | ||
Optional<Annotated> annotated = annotated(MANAGED_BEAN_MATCHER).get(getCursor()); | ||
if (annotated.isPresent()) { | ||
maybeAddImport("jakarta.inject.Named"); | ||
maybeRemoveImport("javax.faces.bean.ManagedBean"); | ||
maybeRemoveImport("jakarta.faces.bean.ManagedBean"); | ||
// Get the name from the @ManagedBean annotation | ||
String beanName = annotated | ||
.flatMap(a -> a.getDefaultAttribute("name")) | ||
.map(name -> name.getValue(String.class)) | ||
.orElse(null); | ||
// Replace the @ManagedBean annotation with @Named | ||
if (beanName != null) { | ||
return JavaTemplate.builder("@Named(\"#{}\")") | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.inject-api-2.0.1")) | ||
.imports("jakarta.inject.Named") | ||
.build() | ||
.apply(getCursor(), annotation.getCoordinates().replace(), beanName); | ||
} | ||
return JavaTemplate.builder("@Named") | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.inject-api-2.0.1")) | ||
.imports("jakarta.inject.Named") | ||
.build() | ||
.apply(getCursor(), annotation.getCoordinates().replace()); | ||
} | ||
return annotation; | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/org/openrewrite/java/migrate/jakarta/UpdateManagedBeanToNamedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2025 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.migrate.jakarta; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class UpdateManagedBeanToNamedTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.parser(JavaParser.fromJavaVersion() | ||
.classpathFromResources(new InMemoryExecutionContext(), "jsf-api-2.1.29-11", "jakarta.faces-api-3.0.0")) | ||
.recipe(new UpdateManagedBeanToNamed()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"javax", "jakarta"}) | ||
void updateManagedBeanToNamed(String pkg) { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import %s.faces.bean.ManagedBean; | ||
|
||
@ManagedBean | ||
public class ApplicationBean2 { | ||
} | ||
""".formatted(pkg), | ||
""" | ||
import jakarta.inject.Named; | ||
|
||
@Named | ||
public class ApplicationBean2 { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"javax", "jakarta"}) | ||
void updateManagedBeanToNamedWithArg(String pkg) { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import %s.faces.bean.ManagedBean; | ||
|
||
@ManagedBean(name="myBean") | ||
public class ApplicationBean2 { | ||
} | ||
""".formatted(pkg), | ||
""" | ||
import jakarta.inject.Named; | ||
|
||
@Named("myBean") | ||
public class ApplicationBean2 { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Binary file added
BIN
+10.4 KB
src/test/resources/META-INF/rewrite/classpath/jakarta.inject-api-2.0.1.jar
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.