16
16
17
17
package org .openrewrite .java .migrate ;
18
18
19
+ import com .fasterxml .jackson .annotation .JsonCreator ;
20
+ import lombok .AllArgsConstructor ;
19
21
import lombok .EqualsAndHashCode ;
20
22
import lombok .Value ;
21
23
import org .jspecify .annotations .Nullable ;
30
32
31
33
@ Value
32
34
@ EqualsAndHashCode (callSuper = false )
33
- class ReplaceAWTGetPeerMethod extends Recipe {
35
+ @ AllArgsConstructor
36
+ public class ReplaceAWTGetPeerMethod extends Recipe {
34
37
35
38
@ Option (displayName = "Method pattern to replace" ,
36
39
description = "The method pattern to match and replace." ,
37
- example = "java.awt.* getPeer()" )
40
+ example = "java.awt.* getPeer()" ,
41
+ required = false )
38
42
String getPeerMethodPattern ;
39
43
40
44
@ Option (displayName = "The LightweightPeer interface FQCN" ,
41
45
description = "The fully qualified class name of the LightweightPeer interface to replace in `instanceof`." ,
42
- example = "java.awt.peer.LightweightPeer" )
46
+ example = "java.awt.peer.LightweightPeer" ,
47
+ required = false )
43
48
String lightweightPeerFQCN ;
44
49
50
+ @ JsonCreator
51
+ public ReplaceAWTGetPeerMethod () {
52
+ getPeerMethodPattern = "java.awt.* getPeer()" ;
53
+ lightweightPeerFQCN = "java.awt.peer.LightweightPeer" ;
54
+ }
55
+
45
56
@ Override
46
57
public String getDisplayName () {
47
58
return "Replace AWT `getPeer()` method" ;
@@ -50,8 +61,8 @@ public String getDisplayName() {
50
61
@ Override
51
62
public String getDescription () {
52
63
return "This recipe replaces the use of `getPeer()` method in `java.awt.*` classes. " +
53
- "`component.getPeer() != null` is replaced with `component.isDisplayable()` and " +
54
- "`component.getPeer() instanceof LightweightPeer` is replaced with `component.isLightweight()`." ;
64
+ "`component.getPeer() != null` is replaced with `component.isDisplayable()` and " +
65
+ "`component.getPeer() instanceof LightweightPeer` is replaced with `component.isLightweight()`." ;
55
66
}
56
67
57
68
@ Override
@@ -81,10 +92,10 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
81
92
J .MethodInvocation mi = null ;
82
93
if (binaryCondition .getOperator () == J .Binary .Type .NotEqual ) {
83
94
if (binaryCondition .getLeft () instanceof J .MethodInvocation &&
84
- binaryCondition .getRight () instanceof J .Literal ) {
95
+ binaryCondition .getRight () instanceof J .Literal ) {
85
96
mi = (J .MethodInvocation ) binaryCondition .getLeft ();
86
97
} else if (binaryCondition .getLeft () instanceof J .Literal &&
87
- binaryCondition .getRight () instanceof J .MethodInvocation ) {
98
+ binaryCondition .getRight () instanceof J .MethodInvocation ) {
88
99
mi = (J .MethodInvocation ) binaryCondition .getRight ();
89
100
}
90
101
}
0 commit comments