Skip to content

Commit

Permalink
Add recipe to prefer java standard library over guava `Maps.newHashMa… (
Browse files Browse the repository at this point in the history
#431)

* Add recipe to prefer java standard library over guava `Maps.newHashMap` (#412)

* Remove replacing newHashMapWithExpectedSize in NoGuavaMapsNewHashMap

* Removed some whitespace in tests
updated copyright

* Revert "Removed some whitespace in tests"

This reverts commit a27de85.

* Removed some whitespace in tests

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Peter Streef <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent b64a002 commit bd0bf9b
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.migrate.guava;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;

import java.util.Collections;
import java.util.Set;

public class NoGuavaMapsNewHashMap extends Recipe {
private static final MethodMatcher NEW_HASH_MAP = new MethodMatcher("com.google.common.collect.Maps newHashMap()");
private static final MethodMatcher NEW_HASH_MAP_WITH_MAP = new MethodMatcher("com.google.common.collect.Maps newHashMap(java.util.Map)");

@Override
public String getDisplayName() {
return "Prefer `new HashMap<>()`";
}

@Override
public String getDescription() {
return "Prefer the Java standard library over third-party usage of Guava in simple cases like this.";
}

@Override
public Set<String> getTags() {
return Collections.singleton("guava");
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(Preconditions.or(
new UsesMethod<>(NEW_HASH_MAP),
new UsesMethod<>(NEW_HASH_MAP_WITH_MAP)), new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (NEW_HASH_MAP.matches(method)) {
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.HashMap");
return JavaTemplate.builder("new HashMap<>()")
.contextSensitive()
.imports("java.util.HashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace());
} else if (NEW_HASH_MAP_WITH_MAP.matches(method)) {
maybeRemoveImport("com.google.common.collect.Maps");
maybeAddImport("java.util.HashMap");
return JavaTemplate.builder("new HashMap<>(#{any(java.util.Map)})")
.contextSensitive()
.imports("java.util.HashMap")
.build()
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
}
return super.visitMethodInvocation(method, ctx);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openrewrite.java.migrate.guava;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;


class NoGuavaMapsNewHashMapTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.recipe(new NoGuavaMapsNewHashMap())
.parser(JavaParser.fromJavaVersion().classpath("guava"));
}

@DocumentExample
@Test
void replaceWithNewHashMap() {
//language=java
rewriteRun(
java(
"""
import com.google.common.collect.*;
import java.util.Map;
class Test {
Map<Integer, Integer> cardinalsWorldSeries = Maps.newHashMap();
}
""",
"""
import java.util.HashMap;
import java.util.Map;
class Test {
Map<Integer, Integer> cardinalsWorldSeries = new HashMap<>();
}
"""
)
);
}

@Test
void replaceWithNewHashMapWithMap() {
//language=java
rewriteRun(
java(
"""
import com.google.common.collect.*;
import java.util.Collections;
import java.util.Map;
class Test {
Map<Integer, Integer> m = Collections.emptyMap();
Map<Integer, Integer> cardinalsWorldSeries = Maps.newHashMap(m);
}
""",
"""
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
class Test {
Map<Integer, Integer> m = Collections.emptyMap();
Map<Integer, Integer> cardinalsWorldSeries = new HashMap<>(m);
}
"""
)
);
}
}

0 comments on commit bd0bf9b

Please sign in to comment.