15
15
*/
16
16
package org .openrewrite .java .spring .util ;
17
17
18
+ import lombok .RequiredArgsConstructor ;
18
19
import lombok .Value ;
19
20
import org .openrewrite .ExecutionContext ;
21
+ import org .openrewrite .internal .ListUtils ;
20
22
import org .openrewrite .internal .StringUtils ;
23
+ import org .openrewrite .java .JavaIsoVisitor ;
21
24
import org .openrewrite .java .JavaTemplate ;
22
25
import org .openrewrite .java .JavaVisitor ;
23
26
import org .openrewrite .java .VariableNameUtils ;
26
29
27
30
import java .util .ArrayList ;
28
31
import java .util .List ;
32
+ import java .util .Optional ;
29
33
30
34
import static java .util .stream .Collectors .joining ;
31
35
@@ -50,14 +54,16 @@ public J visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx)
50
54
mr .getContaining (),
51
55
mr .getReference ().getSimpleName ());
52
56
} else {
53
- templateCode = String .format ("(%s) -> %s.%s()" ,
54
- args .stream ().map (Param ::toString ).collect (joining (", " )),
57
+ templateCode = String .format ("%1$s -> %1$s.%2$s()" ,
55
58
args .get (0 ).getName (),
56
59
mr .getReference ().getSimpleName ());
57
60
}
58
- return JavaTemplate .builder (templateCode )
61
+ J . Lambda lambda = JavaTemplate .builder (templateCode )
59
62
.contextSensitive ()
60
- .build ().apply (getCursor (), mr .getCoordinates ().replace ())
63
+ .build ()
64
+ .apply (getCursor (), mr .getCoordinates ().replace ());
65
+
66
+ return new AddLambdaTypeInformation (mr , args ).visitNonNull (lambda , ctx )
61
67
.withPrefix (mr .getPrefix ());
62
68
}
63
69
@@ -76,13 +82,41 @@ private List<Param> getLambdaArgNames(JavaType.Method methodType) {
76
82
}
77
83
if (params .isEmpty ()) {
78
84
JavaType .FullyQualified type = methodType .getDeclaringType ();
79
- String uniqueVariableName = VariableNameUtils .generateVariableName (StringUtils .uncapitalize (type .getClassName ()), getCursor (), VariableNameUtils .GenerationStrategy .INCREMENT_NUMBER );
85
+ String uniqueVariableName = VariableNameUtils .generateVariableName (StringUtils .uncapitalize (keepFromLastCapitalLetter ( type .getClassName () )), getCursor (), VariableNameUtils .GenerationStrategy .INCREMENT_NUMBER );
80
86
81
87
params .add (new Param (type , uniqueVariableName ));
82
88
}
83
89
return params ;
84
90
}
85
91
92
+ @ RequiredArgsConstructor
93
+ private static class AddLambdaTypeInformation extends JavaIsoVisitor <ExecutionContext > {
94
+
95
+ private final J .MemberReference mr ;
96
+ private final List <Param > args ;
97
+
98
+ @ Override
99
+ public J .Identifier visitIdentifier (J .Identifier ident , ExecutionContext ctx ) {
100
+ Optional <Param > arg = args .stream ().filter (a -> a .getName ().equals (ident .getSimpleName ())).findFirst ();
101
+ if (arg .isPresent ()) {
102
+ JavaType type = arg .get ().getType ();
103
+ return ident .withType (type ).withFieldType (ident .getFieldType () == null ? null : ident .getFieldType ().withType (type ));
104
+ }
105
+ return super .visitIdentifier (ident , ctx );
106
+ }
107
+
108
+ @ Override
109
+ public J .VariableDeclarations visitVariableDeclarations (J .VariableDeclarations mv , ExecutionContext ctx ) {
110
+ return mv .withVariables (ListUtils .map (mv .getVariables (), (index , variable ) -> variable .withType (args .get (index ).getType ())));
111
+ }
112
+
113
+ @ Override
114
+ public J .MethodInvocation visitMethodInvocation (J .MethodInvocation mi , ExecutionContext ctx ) {
115
+ return mi .withMethodType (mr .getMethodType ());
116
+ }
117
+
118
+ }
119
+
86
120
@ Value
87
121
class Param {
88
122
JavaType type ;
@@ -93,4 +127,13 @@ public String toString() {
93
127
return String .format ("%s %s" , type .toString ().replaceFirst ("^java.lang." , "" ), name );
94
128
}
95
129
}
130
+
131
+ private static String keepFromLastCapitalLetter (String input ) {
132
+ for (int i = input .length () - 1 ; i >= 0 ; i --) {
133
+ if (Character .isUpperCase (input .charAt (i ))) {
134
+ return input .substring (i );
135
+ }
136
+ }
137
+ return input ;
138
+ }
96
139
}
0 commit comments