-
Notifications
You must be signed in to change notification settings - Fork 109
Java21 recipe deprecatedSecurityManager #669
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
Closed
ranuradh
wants to merge
7
commits into
openrewrite:main
from
ranuradh:recipe_deprecatedSecurityManager
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d1c485
Java21 recipe deprecatedSecurityManager
AnuRam123 adb9a14
added precondtions
AnuRam123 1ee3c53
small change
AnuRam123 47f1451
description update
AnuRam123 1ebafda
Update src/main/java/org/openrewrite/java/migrate/DeprecatedSecurityM…
ranuradh e58d3b7
updates to description
AnuRam123 3c1284c
Merge branch 'recipe_deprecatedSecurityManager' of https://github.com…
AnuRam123 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
83 changes: 83 additions & 0 deletions
83
src/main/java/org/openrewrite/java/migrate/DeprecatedSecurityManager.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,83 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.openrewrite.*; | ||
| import org.openrewrite.internal.ListUtils; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesJavaVersion; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.Statement; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class DeprecatedSecurityManager extends Recipe { | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Call `System.setProperty(\"java.security.manager\",\"allow\")` before calling `System.setSecurityManager(java.lang.SecurityManager)`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "The default value of the `java.security.manager` system property has been changed to disallow since Java 18." + | ||
| " Unless the system property is set to allow on the command line, any invocation of System.setSecurityManager(SecurityManager) " + | ||
| " with a non-null argument will throw an `UnsupportedOperationException`. " + | ||
| " You can set system property as `Djava.security.manager=allow.` to prevent the exception." + | ||
| "The recipe calls `System.setProperty(\"java.security.manager\",\"allow\")` before calling `System.setSecurityManager(SecurityManager)`."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new UsesJavaVersion<>(18), | ||
| new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.Block visitBlock(J.Block block, ExecutionContext ctx) { | ||
| MethodMatcher SETSECURITY_REF = new MethodMatcher("java.lang.System setSecurityManager(java.lang.SecurityManager) ", true); | ||
| List<Statement> statements = block.getStatements(); | ||
| boolean propertySet = false; | ||
| for (int i = 0; i < statements.size(); i++) { | ||
| Statement stmt = statements.get(i); | ||
| if (stmt instanceof J.MethodInvocation) { | ||
| if (SETSECURITY_REF.matches((J.MethodInvocation) stmt)) { | ||
| for (Statement statement : statements) { | ||
| if (statement instanceof J.MethodInvocation && | ||
| ((J.MethodInvocation) statement).getSimpleName().equals("setProperty") && | ||
| ((J.MethodInvocation) statement).getArguments().get(0) instanceof J.Literal && | ||
| ((J.Literal) ((J.MethodInvocation) statement).getArguments().get(0)).getValue().equals("java.security.manager")) { | ||
| propertySet = true; | ||
| break; | ||
| } | ||
| } | ||
| String templateString = "System.setProperty(\"java.security.manager\", \"allow\");"; | ||
| if (!propertySet) { | ||
| stmt = JavaTemplate.builder(templateString) | ||
| .contextSensitive() | ||
| .build().apply(new Cursor(getCursor(), stmt), | ||
| stmt.getCoordinates().replace()); | ||
| statements = ListUtils.insert(statements, stmt, i); | ||
| return block.withStatements(statements); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return super.visitBlock(block, ctx); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
73 changes: 73 additions & 0 deletions
73
src/test/java/org/openrewrite/java/migrate/DeprecatedSecurityManagerTest.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,73 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| 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.java.Assertions.javaVersion; | ||
|
|
||
| class DeprecatedSecurityManagerTest implements RewriteTest { | ||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new DeprecatedSecurityManager()) | ||
| .allSources(src -> src.markers(javaVersion(21))); | ||
| } | ||
| @Test | ||
| @DocumentExample | ||
| void deprecatedSecurityManager() { | ||
| rewriteRun( | ||
| //language=java | ||
| java( | ||
| """ | ||
| import java.lang.Thread; | ||
| import java.lang.*; | ||
|
|
||
| public class Test { | ||
|
|
||
| public void foo(){ | ||
| String x = "dlaj"; | ||
| } | ||
| public static void main(String[] args) { | ||
| System.setSecurityManager(new SecurityManager()); | ||
| int i = 5; | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.lang.Thread; | ||
| import java.lang.*; | ||
|
|
||
| public class Test { | ||
|
|
||
| public void foo(){ | ||
| String x = "dlaj"; | ||
| } | ||
| public static void main(String[] args) { | ||
| System.setProperty("java.security.manager", "allow"); | ||
| System.setSecurityManager(new SecurityManager()); | ||
| int i = 5; | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| } |
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.