1+ /*
2+ * Copyright 2021 the original author or authors.
3+ * <p>
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ * <p>
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ * <p>
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
116package org .openrewrite .java .migrate .guava ;
217
318import org .openrewrite .ExecutionContext ;
1227
1328public class NoGuavaMapsNewLinkedHashMap extends Recipe {
1429 private static final MethodMatcher NEW_LINKED_HASH_MAP = new MethodMatcher ("com.google.common.collect.Maps newLinkedHashMap()" );
15- private static final MethodMatcher NEW_LINKED_HASH_MAP_ITERABLE = new MethodMatcher ("com.google.common.collect.Maps newLinkedHashMap(java.util.Map)" );
30+ private static final MethodMatcher NEW_LINKED_HASH_MAP_WITH_MAP = new MethodMatcher ("com.google.common.collect.Maps newLinkedHashMap(java.util.Map)" );
1631 private static final MethodMatcher NEW_LINKED_HASH_MAP_CAPACITY = new MethodMatcher ("com.google.common.collect.Maps newLinkedHashMapWithExpectedSize(int)" );
1732
1833 @ Override
@@ -31,7 +46,7 @@ protected TreeVisitor<?, ExecutionContext> getApplicableTest() {
3146 @ Override
3247 public J .CompilationUnit visitCompilationUnit (J .CompilationUnit cu , ExecutionContext executionContext ) {
3348 doAfterVisit (new UsesMethod <>(NEW_LINKED_HASH_MAP ));
34- doAfterVisit (new UsesMethod <>(NEW_LINKED_HASH_MAP_ITERABLE ));
49+ doAfterVisit (new UsesMethod <>(NEW_LINKED_HASH_MAP_WITH_MAP ));
3550 doAfterVisit (new UsesMethod <>(NEW_LINKED_HASH_MAP_CAPACITY ));
3651 return cu ;
3752 }
@@ -45,7 +60,7 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
4560 .imports ("java.util.LinkedHashMap" )
4661 .build ();
4762
48- private final JavaTemplate newLinkedHashMapIterable = JavaTemplate .builder (this ::getCursor , "new LinkedHashMap<>(#{any(java.util.Map)})" )
63+ private final JavaTemplate newLinkedHashMapWithMap = JavaTemplate .builder (this ::getCursor , "new LinkedHashMap<>(#{any(java.util.Map)})" )
4964 .imports ("java.util.LinkedHashMap" )
5065 .build ();
5166
@@ -59,10 +74,10 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext execu
5974 maybeRemoveImport ("com.google.common.collect.Maps" );
6075 maybeAddImport ("java.util.LinkedHashMap" );
6176 return method .withTemplate (newLinkedHashMap , method .getCoordinates ().replace ());
62- } else if (NEW_LINKED_HASH_MAP_ITERABLE .matches (method )) {
77+ } else if (NEW_LINKED_HASH_MAP_WITH_MAP .matches (method )) {
6378 maybeRemoveImport ("com.google.common.collect.Maps" );
6479 maybeAddImport ("java.util.LinkedHashMap" );
65- return method .withTemplate (newLinkedHashMapIterable , method .getCoordinates ().replace (),
80+ return method .withTemplate (newLinkedHashMapWithMap , method .getCoordinates ().replace (),
6681 method .getArguments ().get (0 ));
6782 } else if (NEW_LINKED_HASH_MAP_CAPACITY .matches (method )) {
6883 maybeRemoveImport ("com.google.common.collect.Maps" );
0 commit comments