Skip to content

Commit 2e51b86

Browse files
ranuradhAnuRam123timtebeek
authored
Adding Java 11 recipe -RemovedSecurityManagerMethods (#458)
* adding Java 11 recipe -RemovedSecurityManagerMethods * adding the .yml file * deleting test file * updating recipes to only work with void returns * updating description * removed extra ( & updated description * Apply formatter for consistency --------- Co-authored-by: anuram <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent e954a3e commit 2e51b86

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Recipe;
20+
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.java.JavaTemplate;
22+
import org.openrewrite.java.JavaVisitor;
23+
import org.openrewrite.java.MethodMatcher;
24+
import org.openrewrite.java.tree.J;
25+
26+
public class RemovedSecurityManagerMethods extends Recipe {
27+
@Override
28+
public String getDisplayName() {
29+
return "Replace deprecated methods in`SecurityManager`";
30+
}
31+
32+
@Override
33+
public String getDescription() {
34+
return "Replace `SecurityManager` methods `checkAwtEventQueueAccess()`, `checkSystemClipboardAccess()`," +
35+
" `checkMemberAccess()` and `checkTopLevelWindow()` deprecated in Java SE 11 by" +
36+
" `checkPermission(new java.security.AllPermission())`.";
37+
}
38+
39+
@Override
40+
public TreeVisitor<?, ExecutionContext> getVisitor() {
41+
return new JavaVisitor<ExecutionContext>() {
42+
private final MethodMatcher METHOD_PATTERN_QUE = new MethodMatcher("java.lang.SecurityManager checkAwtEventQueueAccess()", false);
43+
private final MethodMatcher METHOD_PATTERN_CLIP = new MethodMatcher("java.lang.SecurityManager checkSystemClipboardAccess()", false);
44+
private final MethodMatcher METHOD_PATTERN_MEMBER = new MethodMatcher("java.lang.SecurityManager checkMemberAccess(..)", false);
45+
private final MethodMatcher METHOD_PATTERN_WINDOW = new MethodMatcher("java.lang.SecurityManager checkTopLevelWindow(..)", false);
46+
47+
@Override
48+
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
49+
if (METHOD_PATTERN_QUE.matches(method) || METHOD_PATTERN_CLIP.matches(method) || METHOD_PATTERN_MEMBER.matches(method) || METHOD_PATTERN_WINDOW.matches(method)) {
50+
return JavaTemplate.builder("checkPermission(new java.security.AllPermission())")
51+
.imports("java.security.AllPermission")
52+
.build().apply(updateCursor(method),
53+
method.getCoordinates().replaceMethod());
54+
}
55+
return method;
56+
}
57+
};
58+
}
59+
}

src/main/resources/META-INF/rewrite/java-version-11.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ recipeList:
6767
- org.openrewrite.github.SetupJavaUpgradeJavaVersion:
6868
minimumJavaMajorVersion: 11
6969
- org.openrewrite.java.migrate.InternalBindContextFactory
70+
- org.openrewrite.java.migrate.RemovedSecurityManagerMethods
7071

7172
---
7273
type: specs.openrewrite.org/v1beta/recipe

0 commit comments

Comments
 (0)