15
15
*/
16
16
package org .openrewrite .java .migrate .lang .var ;
17
17
18
- import org .openrewrite .*;
18
+ import org .openrewrite .ExecutionContext ;
19
+ import org .openrewrite .Preconditions ;
20
+ import org .openrewrite .Recipe ;
21
+ import org .openrewrite .TreeVisitor ;
19
22
import org .openrewrite .java .JavaIsoVisitor ;
20
- import org .openrewrite .java .JavaParser ;
21
- import org .openrewrite .java .JavaTemplate ;
22
23
import 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 ;
30
27
31
28
public class UseVarForGenericMethodInvocations extends Recipe {
32
29
@ Override
@@ -50,9 +47,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
50
47
}
51
48
52
49
static final class UseVarForGenericsVisitor extends JavaIsoVisitor <ExecutionContext > {
53
- private final JavaTemplate template = JavaTemplate .builder ("var #{} = #{any()}" )
54
- .javaParser (JavaParser .fromJavaVersion ()).build ();
55
-
56
50
@ Override
57
51
public J .VariableDeclarations visitVariableDeclarations (J .VariableDeclarations vd , ExecutionContext ctx ) {
58
52
vd = super .visitVariableDeclarations (vd , ctx );
@@ -62,34 +56,42 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
62
56
return vd ;
63
57
}
64
58
65
- // recipe specific
59
+ // Recipe specific
66
60
boolean isPrimitive = DeclarationCheck .isPrimitive (vd );
67
61
boolean usesNoGenerics = !DeclarationCheck .useGenerics (vd );
68
62
boolean usesTernary = DeclarationCheck .initializedByTernary (vd );
69
63
if (isPrimitive || usesTernary || usesNoGenerics ) {
70
64
return vd ;
71
65
}
72
66
73
- //now we deal with generics, check for method invocations
67
+ // Now we deal with generics, check for method invocations
74
68
Expression initializer = vd .getVariables ().get (0 ).getInitializer ();
75
69
boolean isMethodInvocation = initializer != null && initializer .unwrap () instanceof J .MethodInvocation ;
76
70
if (!isMethodInvocation ) {
77
71
return vd ;
78
72
}
79
73
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
81
75
boolean hasNoTypeParams = ((J .MethodInvocation ) initializer ).getTypeParameters () == null ;
82
76
boolean argumentsEmpty = allArgumentsEmpty ((J .MethodInvocation ) initializer );
83
77
if (hasNoTypeParams && argumentsEmpty ) {
84
78
return vd ;
85
79
}
86
80
87
- // mark imports for removal if unused
88
81
if (vd .getType () instanceof JavaType .FullyQualified ) {
89
- maybeRemoveImport ( (JavaType .FullyQualified ) vd .getType () );
82
+ maybeRemoveImport ((JavaType .FullyQualified ) vd .getType ());
90
83
}
91
84
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
+ });*/
93
95
}
94
96
95
97
private static boolean allArgumentsEmpty (J .MethodInvocation invocation ) {
@@ -100,40 +102,5 @@ private static boolean allArgumentsEmpty(J.MethodInvocation invocation) {
100
102
}
101
103
return true ;
102
104
}
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
- }
138
105
}
139
106
}
0 commit comments