Skip to content

Commit

Permalink
SONARJAVA-2516 Include java9 'Set.of()' and guava 'ImmutableSet.of()'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops authored and saberduck committed Dec 15, 2017
1 parent b7f5f92 commit 35352e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.java.matcher.MethodMatcher;
import org.sonar.java.matcher.MethodMatcherCollection;
import org.sonar.java.matcher.NameCriteria;
import org.sonar.java.resolve.ParametrizedTypeJavaType;
import org.sonar.java.resolve.TypeVariableJavaType;
Expand All @@ -39,7 +40,12 @@
public class EnumSetCheck extends IssuableSubscriptionVisitor {

private static final MethodMatcher COLLECTIONS_UNMODIFIABLE = MethodMatcher.create().typeDefinition("java.util.Collections").name("unmodifiableSet").withAnyParameters();
private static final MethodMatcher GUAVA_SETS_ANY_METHOD = MethodMatcher.create().typeDefinition("com.google.common.collect.Sets").name(NameCriteria.any()).withAnyParameters();
private static final MethodMatcherCollection SET_CREATION_METHODS = MethodMatcherCollection.create(
// Java 9 factory methods
MethodMatcher.create().typeDefinition("java.util.Set").name("of").withAnyParameters(),
// guava
MethodMatcher.create().typeDefinition("com.google.common.collect.ImmutableSet").name("of").withAnyParameters(),
MethodMatcher.create().typeDefinition("com.google.common.collect.Sets").name(NameCriteria.any()).withAnyParameters());

@Override
public List<Kind> nodesToVisit() {
Expand All @@ -61,7 +67,7 @@ public void visitNode(Tree tree) {
if (COLLECTIONS_UNMODIFIABLE.matches(mit)) {
// check the collection used as parameter
initializer = mit.arguments().get(0);
} else if (!GUAVA_SETS_ANY_METHOD.matches(mit) || "immutableEnumSet".equals(mit.symbol().name())) {
} else if (!SET_CREATION_METHODS.anyMatch(mit) || "immutableEnumSet".equals(mit.symbol().name())) {
// Methods from Guava 'Sets' except 'immutableEnumSet' should be checked,
// but discard any other method invocations (killing the noise)
return;
Expand Down
7 changes: 7 additions & 0 deletions java-checks/src/test/files/checks/EnumSetCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

Expand Down Expand Up @@ -41,6 +42,12 @@ public void doSomething(Set<COLOR> param) { // compliant, we ignore parameters.
Set<COLOR> col2 = Collections.unmodifiableSet(EnumSet.of(COLOR.RED, COLOR.ORANGE));
Set<COLOR> col3 = Collections.unmodifiableSet(new HashSet<COLOR>()); // Noncompliant
Set<COLOR> col4 = rgb(); // Compliant

Set<COLOR> col5 = ImmutableSet.<COLOR>of(); // Noncompliant
Set<COLOR> col6 = ImmutableSet.of(COLOR.RED, COLOR.ORANGE); // Noncompliant
Set<COLOR> col7 = ImmutableSet.of(COLOR.RED, COLOR.BLUE, COLOR.RED, COLOR.ORANGE, COLOR.GREEN, COLOR.ORANGE, COLOR.BLUE); // Noncompliant

Set<COLOR> col8 = Set.of(COLOR.RED); // Compliant - not resolved (but should be non-compliant with java 9)
}

private Set<COLOR> rgb() {
Expand Down

0 comments on commit 35352e3

Please sign in to comment.