|
| 1 | +package org.openrewrite.java.migrate.guava; |
| 2 | + |
| 3 | +import org.openrewrite.ExecutionContext; |
| 4 | +import org.openrewrite.Recipe; |
| 5 | +import org.openrewrite.TreeVisitor; |
| 6 | +import org.openrewrite.java.JavaIsoVisitor; |
| 7 | +import org.openrewrite.java.JavaTemplate; |
| 8 | +import org.openrewrite.java.JavaVisitor; |
| 9 | +import org.openrewrite.java.MethodMatcher; |
| 10 | +import org.openrewrite.java.search.UsesMethod; |
| 11 | +import org.openrewrite.java.tree.J; |
| 12 | + |
| 13 | +public class NoGuavaMapsNewLinkedHashMap extends Recipe { |
| 14 | + 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)"); |
| 16 | + private static final MethodMatcher NEW_LINKED_HASH_MAP_CAPACITY = new MethodMatcher("com.google.common.collect.Maps newLinkedHashMapWithExpectedSize(int)"); |
| 17 | + |
| 18 | + @Override |
| 19 | + public String getDisplayName() { |
| 20 | + return "Use `new LinkedHashMap<>()` instead of Guava"; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public String getDescription() { |
| 25 | + return "Prefer the Java standard library over third-party usage of Guava in simple cases like this."; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected TreeVisitor<?, ExecutionContext> getApplicableTest() { |
| 30 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 31 | + @Override |
| 32 | + public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext executionContext) { |
| 33 | + doAfterVisit(new UsesMethod<>(NEW_LINKED_HASH_MAP)); |
| 34 | + doAfterVisit(new UsesMethod<>(NEW_LINKED_HASH_MAP_ITERABLE)); |
| 35 | + doAfterVisit(new UsesMethod<>(NEW_LINKED_HASH_MAP_CAPACITY)); |
| 36 | + return cu; |
| 37 | + } |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected TreeVisitor<?, ExecutionContext> getVisitor() { |
| 43 | + return new JavaVisitor<ExecutionContext>() { |
| 44 | + private final JavaTemplate newLinkedHashMap = JavaTemplate.builder(this::getCursor, "new LinkedHashMap<>()") |
| 45 | + .imports("java.util.LinkedHashMap") |
| 46 | + .build(); |
| 47 | + |
| 48 | + private final JavaTemplate newLinkedHashMapIterable = JavaTemplate.builder(this::getCursor, "new LinkedHashMap<>(#{any(java.util.Map)})") |
| 49 | + .imports("java.util.LinkedHashMap") |
| 50 | + .build(); |
| 51 | + |
| 52 | + private final JavaTemplate newLinkedHashMapCapacity = JavaTemplate.builder(this::getCursor, "new LinkedHashMap<>(#{any(int)})") |
| 53 | + .imports("java.util.LinkedHashMap") |
| 54 | + .build(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { |
| 58 | + if (NEW_LINKED_HASH_MAP.matches(method)) { |
| 59 | + maybeRemoveImport("com.google.common.collect.Maps"); |
| 60 | + maybeAddImport("java.util.LinkedHashMap"); |
| 61 | + return method.withTemplate(newLinkedHashMap, method.getCoordinates().replace()); |
| 62 | + } else if (NEW_LINKED_HASH_MAP_ITERABLE.matches(method)) { |
| 63 | + maybeRemoveImport("com.google.common.collect.Maps"); |
| 64 | + maybeAddImport("java.util.LinkedHashMap"); |
| 65 | + return method.withTemplate(newLinkedHashMapIterable, method.getCoordinates().replace(), |
| 66 | + method.getArguments().get(0)); |
| 67 | + } else if (NEW_LINKED_HASH_MAP_CAPACITY.matches(method)) { |
| 68 | + maybeRemoveImport("com.google.common.collect.Maps"); |
| 69 | + maybeAddImport("java.util.LinkedHashMap"); |
| 70 | + return method.withTemplate(newLinkedHashMapCapacity, method.getCoordinates().replace(), |
| 71 | + method.getArguments().get(0)); |
| 72 | + } |
| 73 | + return super.visitMethodInvocation(method, executionContext); |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | +} |
0 commit comments