Skip to content

Commit adb9a14

Browse files
committed
added precondtions
1 parent 7d1c485 commit adb9a14

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

src/main/java/org/openrewrite/java/migrate/DeprecatedSecurityManager.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616
package org.openrewrite.java.migrate;
1717

18-
import org.openrewrite.Cursor;
19-
import org.openrewrite.ExecutionContext;
20-
import org.openrewrite.Recipe;
21-
import org.openrewrite.TreeVisitor;
18+
import org.openrewrite.*;
2219
import org.openrewrite.internal.ListUtils;
2320
import org.openrewrite.java.JavaIsoVisitor;
2421
import org.openrewrite.java.JavaTemplate;
2522
import org.openrewrite.java.MethodMatcher;
23+
import org.openrewrite.java.search.UsesJavaVersion;
2624
import org.openrewrite.java.tree.J;
2725
import org.openrewrite.java.tree.Statement;
2826

@@ -46,38 +44,39 @@ public String getDescription() {
4644

4745
@Override
4846
public TreeVisitor<?, ExecutionContext> getVisitor() {
49-
return new JavaIsoVisitor<ExecutionContext>() {
50-
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
51-
MethodMatcher SETSECURITY_REF = new MethodMatcher("java.lang.System setSecurityManager(java.lang.SecurityManager) ", true);
52-
List<Statement> statements = block.getStatements();
53-
boolean propertySet = false;
54-
for (int i = 0; i < statements.size() ; i++) {
55-
Statement stmt = statements.get(i);
56-
if (stmt instanceof J.MethodInvocation) {
57-
if (SETSECURITY_REF.matches((J.MethodInvocation) stmt)) {
58-
for (Statement statement : statements) {
59-
if (statement instanceof J.MethodInvocation &&
60-
((J.MethodInvocation) statement).getSimpleName().equals("setProperty") &&
61-
((J.MethodInvocation) statement).getArguments().get(0) instanceof J.Literal &&
62-
((J.Literal) ((J.MethodInvocation) statement).getArguments().get(0)).getValue().equals("java.security.manager")) {
63-
propertySet = true;
64-
break;
47+
return Preconditions.check(new UsesJavaVersion<>(18),
48+
new JavaIsoVisitor<ExecutionContext>() {
49+
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
50+
MethodMatcher SETSECURITY_REF = new MethodMatcher("java.lang.System setSecurityManager(java.lang.SecurityManager) ", true);
51+
List<Statement> statements = block.getStatements();
52+
boolean propertySet = false;
53+
for (int i = 0; i < statements.size(); i++) {
54+
Statement stmt = statements.get(i);
55+
if (stmt instanceof J.MethodInvocation) {
56+
if (SETSECURITY_REF.matches((J.MethodInvocation) stmt)) {
57+
for (Statement statement : statements) {
58+
if (statement instanceof J.MethodInvocation &&
59+
((J.MethodInvocation) statement).getSimpleName().equals("setProperty") &&
60+
((J.MethodInvocation) statement).getArguments().get(0) instanceof J.Literal &&
61+
((J.Literal) ((J.MethodInvocation) statement).getArguments().get(0)).getValue().equals("java.security.manager")) {
62+
propertySet = true;
63+
break;
64+
}
65+
}
66+
String templateString = "System.setProperty(\"java.security.manager\", \"allow\");";
67+
if (!propertySet) {
68+
stmt = JavaTemplate.builder(templateString)
69+
.contextSensitive()
70+
.build().apply(new Cursor(getCursor(), stmt),
71+
stmt.getCoordinates().replace());
72+
statements = ListUtils.insert(statements, stmt, i);
73+
return block.withStatements(statements);
74+
}
6575
}
6676
}
67-
String templateString = "System.setProperty(\"java.security.manager\", \"allow\");";
68-
if (!propertySet) {
69-
stmt = JavaTemplate.builder(templateString)
70-
.contextSensitive()
71-
.build().apply(new Cursor(getCursor(), stmt),
72-
stmt.getCoordinates().replace());
73-
statements = ListUtils.insert(statements, stmt, i);
74-
return block.withStatements(statements);
75-
}
7677
}
78+
return super.visitBlock(block, ctx);
7779
}
78-
}
79-
return super.visitBlock(block, ctx);
80-
}
81-
};
80+
});
8281
}
8382
}

src/test/java/org/openrewrite/java/migrate/DeprecatedSecurityManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DeprecatedSecurityManagerTest implements RewriteTest {
2727
@Override
2828
public void defaults(RecipeSpec spec) {
2929
spec.recipe(new DeprecatedSecurityManager())
30-
.allSources(src -> src.markers(javaVersion(11)));
30+
.allSources(src -> src.markers(javaVersion(21)));
3131
}
3232
@Test
3333
@DocumentExample

0 commit comments

Comments
 (0)