
org.sonar.java.checks.ReplaceGuavaWithJavaCheck Maven / Gradle / Ivy
The newest version!
/*
* SonarQube Java
* Copyright (C) 2012-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
import org.sonarsource.analyzer.commons.collections.MapBuilder;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
@Rule(key = "S4738")
public class ReplaceGuavaWithJavaCheck extends AbstractMethodDetection implements JavaVersionAwareVisitor {
private static final String USE_INSTEAD = "Use \"%s\" instead.";
private static final String GUAVA_BASE_ENCODING = "com.google.common.io.BaseEncoding";
private static final String GUAVA_OPTIONAL = "com.google.common.base.Optional";
private static final String GUAVA_FILES = "com.google.common.io.Files";
private static final String GUAVA_IMMUTABLE_LIST = "com.google.common.collect.ImmutableList";
private static final Map GUAVA_TO_JAVA_UTIL_TYPES = MapBuilder.newMap()
.put("com.google.common.base.Predicate", "java.util.function.Predicate")
.put("com.google.common.base.Function", "java.util.function.Function")
.put("com.google.common.base.Supplier", "java.util.function.Supplier")
.put(GUAVA_OPTIONAL, "java.util.Optional")
.build();
private static final Map GUAVA_OPTIONAL_TO_JAVA_UTIL_METHODS = MapBuilder.newMap()
.put("of", "of")
.put("absent", "empty")
.put("fromNullable", "ofNullable")
.build();
@Override
public boolean isCompatibleWithJavaVersion(JavaVersion version) {
return version.isJava8Compatible();
}
@Override
protected MethodMatchers getMethodInvocationMatchers() {
return MethodMatchers.or(
MethodMatchers.create().ofTypes(GUAVA_BASE_ENCODING).names("base64", "base64Url").addWithoutParametersMatcher().build(),
MethodMatchers.create().ofTypes(GUAVA_OPTIONAL).names("absent").addWithoutParametersMatcher().build(),
MethodMatchers.create().ofTypes(GUAVA_OPTIONAL).names("fromNullable", "of").withAnyParameters().build(),
MethodMatchers.create().ofTypes(GUAVA_FILES).names("createTempDir").addWithoutParametersMatcher().build(),
MethodMatchers.create().ofTypes(GUAVA_IMMUTABLE_LIST)
.names("of").withAnyParameters().build());
}
@Override
public List nodesToVisit() {
ArrayList kinds = new ArrayList<>(super.nodesToVisit());
kinds.add(Tree.Kind.VARIABLE);
return kinds;
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
checkTypeToReplace((VariableTree) tree);
} else {
super.visitNode(tree);
}
}
private void checkTypeToReplace(VariableTree variableTree) {
String fullyQualifiedTypeName = variableTree.type().symbolType().fullyQualifiedName();
if (GUAVA_TO_JAVA_UTIL_TYPES.containsKey(fullyQualifiedTypeName)) {
reportIssue(variableTree.type(), replacementMessage(GUAVA_TO_JAVA_UTIL_TYPES.get(fullyQualifiedTypeName)));
}
}
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
switch(mit.methodSymbol().owner().type().fullyQualifiedName()) {
case GUAVA_BASE_ENCODING:
reportIssue(mit, replacementMessage("java.util.Base64"));
break;
case GUAVA_OPTIONAL:
reportIssue(mit, replacementMessage("java.util.Optional." + GUAVA_OPTIONAL_TO_JAVA_UTIL_METHODS.get(mit.methodSymbol().name())));
break;
case GUAVA_FILES:
reportIssue(mit, replacementMessage("java.nio.file.Files.createTempDirectory"));
break;
case GUAVA_IMMUTABLE_LIST:
reportJava9Issue(mit, "java.util.List.of()");
break;
default:
break;
}
}
private void reportJava9Issue(MethodInvocationTree mit, String replacement) {
if (context.getJavaVersion().isJava9Compatible()) {
reportIssue(mit, replacementMessage(replacement));
}
}
private String replacementMessage(String replacement) {
return String.format(USE_INSTEAD, replacement) + context.getJavaVersion().java8CompatibilityMessage();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy