
org.openrewrite.java.migrate.guava.NoGuavaAtomicsNewReference Maven / Gradle / Ivy
Show all versions of rewrite-migrate-java Show documentation
/*
* Copyright 2021 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 NoGuavaAtomicsNewReference extends Recipe {
private static final MethodMatcher NEW_ATOMIC_REFERENCE = new MethodMatcher("com.google.common.util.concurrent.Atomics newReference(..)");
@Override
public String getDisplayName() {
return "Prefer `new AtomicReference<>()`";
}
@Override
public String getDescription() {
return "Prefer the Java standard library over third-party usage of Guava in simple cases like this.";
}
@Override
public Set getTags() {
return Collections.singleton("guava");
}
@Override
public TreeVisitor, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(NEW_ATOMIC_REFERENCE), new JavaVisitor() {
private final JavaTemplate newAtomicReference = JavaTemplate.builder("new AtomicReference<>()")
.contextSensitive()
.imports("java.util.concurrent.atomic.AtomicReference")
.build();
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (NEW_ATOMIC_REFERENCE.matches(method)) {
maybeRemoveImport("com.google.common.util.concurrent.Atomics");
maybeAddImport("java.util.concurrent.atomic.AtomicReference");
return ((J.NewClass) newAtomicReference.apply(getCursor(), method.getCoordinates().replace()))
.withArguments(method.getArguments());
}
return super.visitMethodInvocation(method, ctx);
}
});
}
}