1515 */
1616package org .openrewrite .java .migrate .lang .var ;
1717
18- import org .openrewrite .*;
18+ import org .openrewrite .ExecutionContext ;
19+ import org .openrewrite .Preconditions ;
20+ import org .openrewrite .Recipe ;
21+ import org .openrewrite .TreeVisitor ;
1922import org .openrewrite .java .JavaIsoVisitor ;
20- import org .openrewrite .java .JavaParser ;
21- import org .openrewrite .java .JavaTemplate ;
2223import org .openrewrite .java .search .UsesJavaVersion ;
23- import org .openrewrite .java .tree .*;
24- import org .openrewrite .marker .Markers ;
25-
26- import java .util .ArrayList ;
27- import java .util .List ;
28-
29- import static java .util .Collections .emptyList ;
24+ import org .openrewrite .java .tree .Expression ;
25+ import org .openrewrite .java .tree .J ;
26+ import org .openrewrite .java .tree .JavaType ;
3027
3128public class UseVarForGenericMethodInvocations extends Recipe {
3229 @ Override
@@ -50,9 +47,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5047 }
5148
5249 static final class UseVarForGenericsVisitor extends JavaIsoVisitor <ExecutionContext > {
53- private final JavaTemplate template = JavaTemplate .builder ("var #{} = #{any()}" )
54- .javaParser (JavaParser .fromJavaVersion ()).build ();
55-
5650 @ Override
5751 public J .VariableDeclarations visitVariableDeclarations (J .VariableDeclarations vd , ExecutionContext ctx ) {
5852 vd = super .visitVariableDeclarations (vd , ctx );
@@ -62,34 +56,42 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
6256 return vd ;
6357 }
6458
65- // recipe specific
59+ // Recipe specific
6660 boolean isPrimitive = DeclarationCheck .isPrimitive (vd );
6761 boolean usesNoGenerics = !DeclarationCheck .useGenerics (vd );
6862 boolean usesTernary = DeclarationCheck .initializedByTernary (vd );
6963 if (isPrimitive || usesTernary || usesNoGenerics ) {
7064 return vd ;
7165 }
7266
73- //now we deal with generics, check for method invocations
67+ // Now we deal with generics, check for method invocations
7468 Expression initializer = vd .getVariables ().get (0 ).getInitializer ();
7569 boolean isMethodInvocation = initializer != null && initializer .unwrap () instanceof J .MethodInvocation ;
7670 if (!isMethodInvocation ) {
7771 return vd ;
7872 }
7973
80- //if no type paramters are present and no arguments we assume the type is hard to determine a needs manual action
74+ // If no type parameters and no arguments are present, we assume the type is too hard to determine
8175 boolean hasNoTypeParams = ((J .MethodInvocation ) initializer ).getTypeParameters () == null ;
8276 boolean argumentsEmpty = allArgumentsEmpty ((J .MethodInvocation ) initializer );
8377 if (hasNoTypeParams && argumentsEmpty ) {
8478 return vd ;
8579 }
8680
87- // mark imports for removal if unused
8881 if (vd .getType () instanceof JavaType .FullyQualified ) {
89- maybeRemoveImport ( (JavaType .FullyQualified ) vd .getType () );
82+ maybeRemoveImport ((JavaType .FullyQualified ) vd .getType ());
9083 }
9184
92- return transformToVar (vd , new ArrayList <>(), new ArrayList <>());
85+ return DeclarationCheck .transformToVar (vd );
86+ // TODO implement to support cases like `var strs = List.<String>of();`
87+ /*J.VariableDeclarations finalVd = vd;
88+ return DeclarationCheck.<J.MethodInvocation>transformToVar(vd, it -> {
89+ // If left has generics but right has not, copy types parameters
90+ if (finalVd.getTypeExpression() instanceof J.ParameterizedType && !((J.ParameterizedType) finalVd.getTypeExpression()).getTypeParameters().isEmpty() && it.getTypeParameters() == null) {
91+ return it.withTypeParameters(((J.ParameterizedType) finalVd.getTypeExpression()).getPadding().getTypeParameters());
92+ }
93+ return it;
94+ });*/
9395 }
9496
9597 private static boolean allArgumentsEmpty (J .MethodInvocation invocation ) {
@@ -100,40 +102,5 @@ private static boolean allArgumentsEmpty(J.MethodInvocation invocation) {
100102 }
101103 return true ;
102104 }
103-
104- private J .VariableDeclarations transformToVar (J .VariableDeclarations vd , List <JavaType > leftTypes , List <JavaType > rightTypes ) {
105- Expression initializer = vd .getVariables ().get (0 ).getInitializer ();
106- String simpleName = vd .getVariables ().get (0 ).getSimpleName ();
107-
108- // if left is defined but not right, copy types to initializer
109- if (rightTypes .isEmpty () && !leftTypes .isEmpty ()) {
110- // we need to switch type infos from left to right here
111- List <Expression > typeArgument = new ArrayList <>();
112- for (JavaType t : leftTypes ) {
113- typeArgument .add (new J .Identifier (Tree .randomId (), Space .EMPTY , Markers .EMPTY , emptyList (), ((JavaType .Class ) t ).getClassName (), t , null ));
114- }
115- J .ParameterizedType typedInitializerClazz = ((J .ParameterizedType ) ((J .NewClass ) initializer ).getClazz ()).withTypeParameters (typeArgument );
116- initializer = ((J .NewClass ) initializer ).withClazz (typedInitializerClazz );
117- }
118-
119- J .VariableDeclarations result = template .<J .VariableDeclarations >apply (getCursor (), vd .getCoordinates ().replace (), simpleName , initializer )
120- .withPrefix (vd .getPrefix ());
121-
122- // apply modifiers like final
123- List <J .Modifier > modifiers = vd .getModifiers ();
124- boolean hasModifiers = !modifiers .isEmpty ();
125- if (hasModifiers ) {
126- result = result .withModifiers (modifiers );
127- }
128-
129- // apply prefix to type expression
130- TypeTree resultingTypeExpression = result .getTypeExpression ();
131- boolean resultHasTypeExpression = resultingTypeExpression != null ;
132- if (resultHasTypeExpression ) {
133- result = result .withTypeExpression (resultingTypeExpression .withPrefix (vd .getTypeExpression ().getPrefix ()));
134- }
135-
136- return result ;
137- }
138105 }
139106}
0 commit comments