Skip to content

Commit

Permalink
SONARJAVA-3882 Don't complain about ImmutableSet.of and ImmutableMap.…
Browse files Browse the repository at this point in the history
…of in S4738 (#4883)
  • Loading branch information
kaufco authored Sep 26, 2024
1 parent 0d7703c commit 9d76c0f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S4738",
"hasTruePositives": false,
"falseNegatives": 55,
"falseNegatives": 44,
"falsePositives": 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void tempDir() throws IOException {
}

void immutableCollections() {
ImmutableSet.of("A", "B", "C"); // Noncompliant {{Use "java.util.Set.of()" instead.}}
ImmutableSet.of("A", "B", "C"); // Compliant, because ImmutableSet.of keeps the order of the elements
ImmutableList.of("A", "B", "C"); // Noncompliant {{Use "java.util.List.of()" instead.}}
ImmutableMap.of("A", "B", "C", "D"); // Noncompliant {{Use "java.util.Map.of()" or "java.util.Map.ofEntries()" instead.}}
ImmutableMap.of("A", "B", "C", "D"); // Compliant, because ImmutableMap.of keeps the order of the elements
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public class ReplaceGuavaWithJavaCheck extends AbstractMethodDetection implement
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_SET = "com.google.common.collect.ImmutableSet";
private static final String GUAVA_IMMUTABLE_LIST = "com.google.common.collect.ImmutableList";
private static final String GUAVA_IMMUTABLE_MAP = "com.google.common.collect.ImmutableMap";

private static final Map<String, String> GUAVA_TO_JAVA_UTIL_TYPES = MapBuilder.<String, String>newMap()
.put("com.google.common.base.Predicate", "java.util.function.Predicate")
Expand All @@ -69,7 +67,7 @@ protected MethodMatchers getMethodInvocationMatchers() {
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, GUAVA_IMMUTABLE_SET, GUAVA_IMMUTABLE_MAP)
MethodMatchers.create().ofTypes(GUAVA_IMMUTABLE_LIST)
.names("of").withAnyParameters().build());
}

Expand Down Expand Up @@ -111,12 +109,6 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
case GUAVA_IMMUTABLE_LIST:
reportJava9Issue(mit, "java.util.List.of()");
break;
case GUAVA_IMMUTABLE_SET:
reportJava9Issue(mit, "java.util.Set.of()");
break;
case GUAVA_IMMUTABLE_MAP:
reportJava9Issue(mit, "java.util.Map.of()\" or \"java.util.Map.ofEntries()");
break;
default:
break;
}
Expand Down

0 comments on commit 9d76c0f

Please sign in to comment.