@@ -80,6 +80,39 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8080 new UsesType <>(guavaType , false ));
8181 final MethodMatcher IMMUTABLE_MATCHER = new MethodMatcher (guavaType + " of(..)" );
8282 return Preconditions .check (check , new JavaVisitor <ExecutionContext >() {
83+ @ Override
84+ public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
85+ J .MethodInvocation mi = (J .MethodInvocation ) super .visitMethodInvocation (method , ctx );
86+ if (!IMMUTABLE_MATCHER .matches (mi ) || !isParentTypeDownCast (mi )) {
87+ return mi ;
88+ }
89+ maybeRemoveImport (guavaType );
90+ maybeAddImport (javaType );
91+
92+ String template ;
93+ Object [] templateArguments ;
94+ List <Expression > methodArguments = mi .getArguments ();
95+ if (methodArguments .isEmpty () || methodArguments .get (0 ) instanceof J .Empty ) {
96+ template = getShortType (javaType ) + ".of()" ;
97+ templateArguments = new Object []{};
98+ } else if ("com.google.common.collect.ImmutableMap" .equals (guavaType )) {
99+ template = getShortType (javaType ) + ".of(#{any()}, #{any()})" ;
100+ templateArguments = new Object []{methodArguments .get (0 ), methodArguments .get (1 )};
101+ } else {
102+ template = getShortType (javaType ) + ".of(#{any()})" ;
103+ templateArguments = new Object []{methodArguments .get (0 )};
104+ }
105+
106+ J .MethodInvocation m = JavaTemplate .builder (template )
107+ .imports (javaType )
108+ .build ()
109+ .apply (getCursor (), mi .getCoordinates ().replace (), templateArguments );
110+ m = m .getPadding ().withArguments (mi .getPadding ().getArguments ());
111+ JavaType .Method newType = (JavaType .Method ) visitType (mi .getMethodType (), ctx );
112+ m = m .withMethodType (newType ).withName (m .getName ().withType (newType ));
113+ return super .visitMethodInvocation (m , ctx );
114+ }
115+
83116 @ Override
84117 public J .VariableDeclarations visitVariableDeclarations (J .VariableDeclarations multiVariable , ExecutionContext ctx ) {
85118 J .VariableDeclarations mv = (J .VariableDeclarations ) super .visitVariableDeclarations (multiVariable , ctx );
@@ -130,39 +163,6 @@ private TypeTree createNewTypeExpression(TypeTree typeTree, JavaType newType) {
130163 );
131164 }
132165
133- @ Override
134- public J visitMethodInvocation (J .MethodInvocation method , ExecutionContext ctx ) {
135- J .MethodInvocation mi = (J .MethodInvocation ) super .visitMethodInvocation (method , ctx );
136- if (!IMMUTABLE_MATCHER .matches (mi ) || !isParentTypeDownCast (mi )) {
137- return mi ;
138- }
139- maybeRemoveImport (guavaType );
140- maybeAddImport (javaType );
141-
142- String template ;
143- Object [] templateArguments ;
144- List <Expression > methodArguments = mi .getArguments ();
145- if (methodArguments .isEmpty () || methodArguments .get (0 ) instanceof J .Empty ) {
146- template = getShortType (javaType ) + ".of()" ;
147- templateArguments = new Object []{};
148- } else if ("com.google.common.collect.ImmutableMap" .equals (guavaType )) {
149- template = getShortType (javaType ) + ".of(#{any()}, #{any()})" ;
150- templateArguments = new Object []{methodArguments .get (0 ), methodArguments .get (1 )};
151- } else {
152- template = getShortType (javaType ) + ".of(#{any()})" ;
153- templateArguments = new Object []{methodArguments .get (0 )};
154- }
155-
156- J .MethodInvocation m = JavaTemplate .builder (template )
157- .imports (javaType )
158- .build ()
159- .apply (getCursor (), mi .getCoordinates ().replace (), templateArguments );
160- m = m .getPadding ().withArguments (mi .getPadding ().getArguments ());
161- JavaType .Method newType = (JavaType .Method ) visitType (mi .getMethodType (), ctx );
162- m = m .withMethodType (newType ).withName (m .getName ().withType (newType ));
163- return super .visitMethodInvocation (m , ctx );
164- }
165-
166166 private boolean isParentTypeDownCast (MethodCall immutableMethod ) {
167167 J parent = getCursor ().dropParentUntil (J .class ::isInstance ).getValue ();
168168 boolean isParentTypeDownCast = false ;
0 commit comments