1515 */
1616package org .openrewrite .java .spring .util ;
1717
18+ import lombok .RequiredArgsConstructor ;
1819import lombok .Value ;
1920import org .openrewrite .ExecutionContext ;
21+ import org .openrewrite .internal .ListUtils ;
2022import org .openrewrite .internal .StringUtils ;
23+ import org .openrewrite .java .JavaIsoVisitor ;
2124import org .openrewrite .java .JavaTemplate ;
2225import org .openrewrite .java .JavaVisitor ;
2326import org .openrewrite .java .VariableNameUtils ;
2629
2730import java .util .ArrayList ;
2831import java .util .List ;
32+ import java .util .Optional ;
2933
3034import static java .util .stream .Collectors .joining ;
3135
@@ -50,14 +54,16 @@ public J visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx)
5054 mr .getContaining (),
5155 mr .getReference ().getSimpleName ());
5256 } 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()" ,
5558 args .get (0 ).getName (),
5659 mr .getReference ().getSimpleName ());
5760 }
58- return JavaTemplate .builder (templateCode )
61+ J . Lambda lambda = JavaTemplate .builder (templateCode )
5962 .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 )
6167 .withPrefix (mr .getPrefix ());
6268 }
6369
@@ -76,13 +82,41 @@ private List<Param> getLambdaArgNames(JavaType.Method methodType) {
7682 }
7783 if (params .isEmpty ()) {
7884 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 );
8086
8187 params .add (new Param (type , uniqueVariableName ));
8288 }
8389 return params ;
8490 }
8591
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+
86120 @ Value
87121 class Param {
88122 JavaType type ;
@@ -93,4 +127,13 @@ public String toString() {
93127 return String .format ("%s %s" , type .toString ().replaceFirst ("^java.lang." , "" ), name );
94128 }
95129 }
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+ }
96139}
0 commit comments