|
| 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 com.fasterxml.jackson.annotation.JsonCreator; |
| 19 | +import lombok.AllArgsConstructor; |
| 20 | +import lombok.EqualsAndHashCode; |
| 21 | +import lombok.Value; |
| 22 | +import org.openrewrite.ExecutionContext; |
| 23 | +import org.openrewrite.Option; |
| 24 | +import org.openrewrite.Recipe; |
| 25 | +import org.openrewrite.TreeVisitor; |
| 26 | +import org.openrewrite.java.JavaTemplate; |
| 27 | +import org.openrewrite.java.JavaVisitor; |
| 28 | +import org.openrewrite.java.MethodMatcher; |
| 29 | +import org.openrewrite.java.tree.J; |
| 30 | + |
| 31 | +@Value |
| 32 | +@EqualsAndHashCode(callSuper = false) |
| 33 | +@AllArgsConstructor |
| 34 | +public class ReplaceComSunAWTUtilitiesMethods extends Recipe { |
| 35 | + |
| 36 | + @Option(displayName = "Method pattern to replace", |
| 37 | + description = "The method pattern to match and replace.", |
| 38 | + example = "com.sun.awt.AWTUtilities isTranslucencySupported(com.sun.awt.AWTUtilities.Translucency)", |
| 39 | + required = false) |
| 40 | + String getAWTIsWindowsTranslucencyPattern; |
| 41 | + |
| 42 | + |
| 43 | + @Option(displayName = "Method pattern to replace", |
| 44 | + description = "The method pattern to match and replace.", |
| 45 | + example = "com.test.AWTUtilities isWindowOpaque(java.awt.Window)", |
| 46 | + required = false) |
| 47 | + String isWindowOpaquePattern; |
| 48 | + |
| 49 | + @Option(displayName = "Method pattern to replace", |
| 50 | + description = "The method pattern to match and replace.", |
| 51 | + example = "com.test.AWTUtilities isTranslucencyCapable(java.awt.GraphicsConfiguration)", |
| 52 | + required = false) |
| 53 | + String isTranslucencyCapablePattern; |
| 54 | + |
| 55 | + @Option(displayName = "Method pattern to replace", |
| 56 | + description = "The method pattern to match and replace.", |
| 57 | + example = "com.test.AWTUtilities setWindowOpacity(java.awt.Window, float)", |
| 58 | + required = false) |
| 59 | + String setWindowOpacityPattern; |
| 60 | + |
| 61 | + @Option(displayName = "Method pattern to replace", |
| 62 | + description = "The method pattern to match and replace.", |
| 63 | + example = "com.test.AWTUtilities getWindowOpacity(java.awt.Window)", |
| 64 | + required = false) |
| 65 | + String getWindowOpacityPattern; |
| 66 | + |
| 67 | + @Option(displayName = "Method pattern to replace", |
| 68 | + description = "The method pattern to match and replace.", |
| 69 | + example = "com.test.AWTUtilitiesTest getWindowShape(java.awt.Window)", |
| 70 | + required = false) |
| 71 | + String getWindowShapePattern; |
| 72 | + |
| 73 | + @Option(displayName = "Method pattern to replace", |
| 74 | + description = "The method pattern to match and replace.", |
| 75 | + example = "com.test.AWTUtilities setComponentMixingCutoutShape(java.awt.Component,java.awt.Shape)", |
| 76 | + required = false) |
| 77 | + String setComponentMixingCutoutShapePattern; |
| 78 | + |
| 79 | + @JsonCreator |
| 80 | + public ReplaceComSunAWTUtilitiesMethods() { |
| 81 | + getAWTIsWindowsTranslucencyPattern = "com.sun.awt.AWTUtilities isTranslucencySupported(com.sun.awt.AWTUtilities.Translucency)"; |
| 82 | + getWindowOpacityPattern = "com.sun.awt.AWTUtilities getWindowOpacity(java.awt.Window)"; |
| 83 | + getWindowShapePattern = "com.sun.awt.AWTUtilities getWindowShape(java.awt.Window)"; |
| 84 | + isWindowOpaquePattern = "com.sun.awt.AWTUtilities isWindowOpaque(java.awt.Window)"; |
| 85 | + isTranslucencyCapablePattern = "com.sun.awt.AWTUtilities isTranslucencyCapable(java.awt.GraphicsConfiguration)"; |
| 86 | + setComponentMixingCutoutShapePattern = "com.sun.awt.AWTUtilities setComponentMixingCutoutShape(java.awt.Component,java.awt.Shape)"; |
| 87 | + setWindowOpacityPattern = "com.sun.awt.AWTUtilities setWindowOpacity(java.awt.Window, float)"; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String getDisplayName() { |
| 92 | + return "Replace `com.sun.awt.AWTUtilities` static method invocations"; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String getDescription() { |
| 97 | + return "This recipe replaces several static calls in `com.sun.awt.AWTUtilities` with the JavaSE 11 equivalent. " + |
| 98 | + "The methods replaced are `AWTUtilities.isTranslucencySupported()`, `AWTUtilities.setWindowOpacity()`, `AWTUtilities.getWindowOpacity()`, " + |
| 99 | + "`AWTUtilities.getWindowShape()`, `AWTUtilities.isWindowOpaque()`, `AWTUtilities.isTranslucencyCapable()` and `AWTUtilities.setComponentMixingCutoutShape()`."; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 104 | + MethodMatcher getAWTIsWindowsTranslucencyMethod = new MethodMatcher(getAWTIsWindowsTranslucencyPattern); |
| 105 | + MethodMatcher getWindowOpacityPatternMethod = new MethodMatcher(getWindowOpacityPattern); |
| 106 | + MethodMatcher getWindowShapePatternMethod = new MethodMatcher(getWindowShapePattern); |
| 107 | + MethodMatcher isWindowOpaquePatternMethod = new MethodMatcher(isWindowOpaquePattern); |
| 108 | + MethodMatcher isTranslucencyCapablePatternMethod = new MethodMatcher(isTranslucencyCapablePattern); |
| 109 | + MethodMatcher setComponentMixingCutoutShapePatternMethod = new MethodMatcher(setComponentMixingCutoutShapePattern); |
| 110 | + MethodMatcher setWindowOpacityPatternMethod = new MethodMatcher(setWindowOpacityPattern); |
| 111 | + |
| 112 | + return new JavaVisitor<ExecutionContext>() { |
| 113 | + @Override |
| 114 | + public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { |
| 115 | + super.visitMethodInvocation(mi, ctx); |
| 116 | + if (getAWTIsWindowsTranslucencyMethod.matches(mi)) { |
| 117 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 118 | + maybeAddImport("java.awt.GraphicsDevice", false); |
| 119 | + maybeAddImport("java.awt.GraphicsEnvironment", false); |
| 120 | + maybeAddImport("java.awt.Window", false); |
| 121 | + maybeAddImport("java.awt.GraphicsDevice.WindowTranslucency", false); |
| 122 | + String templateString = "GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isWindowTranslucencySupported(WindowTranslucency." |
| 123 | + + ((J.FieldAccess) mi.getArguments().get(0)).getSimpleName(); |
| 124 | + return JavaTemplate.builder(templateString). |
| 125 | + imports("java.awt.GraphicsDevice", |
| 126 | + "java.awt.GraphicsEnvironment", |
| 127 | + "java.awt.Window", |
| 128 | + "java.awt.GraphicsDevice.WindowTranslucency") |
| 129 | + .build() |
| 130 | + .apply(getCursor(), mi.getCoordinates().replace()) |
| 131 | + .withPrefix(mi.getPrefix()); |
| 132 | + } |
| 133 | + if (isWindowOpaquePatternMethod.matches(mi)) { |
| 134 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 135 | + return JavaTemplate.builder("#{any()}.isOpaque()") |
| 136 | + .build() |
| 137 | + .apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0)) |
| 138 | + .withPrefix(mi.getPrefix()); |
| 139 | + } |
| 140 | + if (isTranslucencyCapablePatternMethod.matches(mi)) { |
| 141 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 142 | + return JavaTemplate.builder("#{any()}.isTranslucencyCapable()") |
| 143 | + .build() |
| 144 | + .apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0)) |
| 145 | + .withPrefix(mi.getPrefix()); |
| 146 | + } |
| 147 | + if (setWindowOpacityPatternMethod.matches(mi)) { |
| 148 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 149 | + return JavaTemplate.builder("#{any()}.setOpacity(#{any()})") |
| 150 | + .build() |
| 151 | + .apply(getCursor(), mi.getCoordinates().replace(), |
| 152 | + mi.getArguments().get(0), |
| 153 | + mi.getArguments().get(1)) |
| 154 | + .withPrefix(mi.getPrefix()); |
| 155 | + } |
| 156 | + if (getWindowOpacityPatternMethod.matches(mi)) { |
| 157 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 158 | + return JavaTemplate.builder("#{any()}.getOpacity()") |
| 159 | + .build() |
| 160 | + .apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0)) |
| 161 | + .withPrefix(mi.getPrefix()); |
| 162 | + } |
| 163 | + if (getWindowShapePatternMethod.matches(mi)) { |
| 164 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 165 | + return JavaTemplate.builder("#{any()}.getShape()") |
| 166 | + .build() |
| 167 | + .apply(getCursor(), mi.getCoordinates().replace(), mi.getArguments().get(0)) |
| 168 | + .withPrefix(mi.getPrefix()); |
| 169 | + } |
| 170 | + if (setComponentMixingCutoutShapePatternMethod.matches(mi)) { |
| 171 | + maybeRemoveImport(mi.getMethodType().getDeclaringType().getFullyQualifiedName()); |
| 172 | + return JavaTemplate.builder("#{any()}.setMixingCutoutShape(#{any()})") |
| 173 | + .build() |
| 174 | + .apply(getCursor(), mi.getCoordinates().replace(), |
| 175 | + mi.getArguments().get(0), |
| 176 | + mi.getArguments().get(1)) |
| 177 | + .withPrefix(mi.getPrefix()); |
| 178 | + } |
| 179 | + return mi; |
| 180 | + } |
| 181 | + }; |
| 182 | + } |
| 183 | +} |
0 commit comments