|
| 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 | +} |
0 commit comments