-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for false positives
EI_EXPOSE_REP
in case of unmodifiable colle…
…ctions (#2141) * Fix for false positives `EI_EXPOSE_REP` in case of unmodifiable collections The methods `of` and `copyOf` of `List`, `Map` and `Set` return an unmodifiable collection. Thus if final fields are initiablized using these methods then returning them is not dangerous. * Added handling of methods from java.util.Collestions Co-authored-by: Kengo TODA <skypencil@gmail.com>
- Loading branch information
Showing
6 changed files
with
312 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/Issue1771Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
import static edu.umd.cs.findbugs.test.CountMatcher.containsExactly; | ||
import static org.junit.Assume.assumeFalse; | ||
import static org.junit.Assume.assumeThat; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; | ||
|
||
public class Issue1771Test extends AbstractIntegrationTest { | ||
@Before | ||
public void verifyJavaVersion() { | ||
assumeFalse(System.getProperty("java.specification.version").startsWith("1.")); | ||
int javaVersion = Integer.parseInt(System.getProperty("java.specification.version")); | ||
assumeThat(javaVersion, is(greaterThanOrEqualTo(11))); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
performAnalysis("../java11/ghIssues/Issue1771.class"); | ||
BugInstanceMatcher matcher = new BugInstanceMatcherBuilder() | ||
.bugType("EI_EXPOSE_REP").build(); | ||
assertThat(getBugCollection(), containsExactly(0, matcher)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package ghIssues; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.NavigableSet; | ||
import java.util.Set; | ||
import java.util.SortedSet; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
import java.util.TreeSet; | ||
|
||
public class Issue1771 { | ||
public Issue1771(List<String> list, Map<String, String> map, Set<String> set) { | ||
this.list = List.copyOf(list); | ||
this.map = Map.copyOf(map); | ||
this.set = Set.copyOf(set); | ||
|
||
list2 = List.of("foo", "bar"); | ||
map2 = Map.of("FOO", "foo", "BAR", "bar"); | ||
set2 = Set.of("foo", "bar"); | ||
|
||
List<String> l = new ArrayList<>(); | ||
l.add("foo"); | ||
l.add("bar"); | ||
|
||
Map<String, String> m = new HashMap(); | ||
m.put("FOO", "foo"); | ||
m.put("BAR", "bar"); | ||
|
||
NavigableMap<String, String> nm = new TreeMap(); | ||
nm.put("FOO", "foo"); | ||
nm.put("BAR", "bar"); | ||
|
||
SortedMap<String, String> sm = new TreeMap(); | ||
sm.put("FOO", "foo"); | ||
sm.put("BAR", "bar"); | ||
|
||
Set<String> s = new HashSet(); | ||
s.add("foo"); | ||
s.add("bar"); | ||
|
||
NavigableSet<String> ns = new TreeSet(); | ||
ns.add("foo"); | ||
ns.add("bar"); | ||
|
||
SortedSet<String> ss = new TreeSet(); | ||
ss.add("foo"); | ||
ss.add("bar"); | ||
|
||
list3 = Collections.unmodifiableList(l); | ||
map3 = Collections.unmodifiableMap(m); | ||
navigableMap3 = Collections.unmodifiableNavigableMap(nm); | ||
sortedMap3 = Collections.unmodifiableSortedMap(sm); | ||
set3 = Collections.unmodifiableSet(s); | ||
navigableSet3 = Collections.unmodifiableNavigableSet(ns); | ||
sortedSet3 = Collections.unmodifiableSortedSet(ss); | ||
|
||
list4 = Collections.singletonList("foo"); | ||
map4 = Collections.singletonMap("FOO", "foo"); | ||
set4 = Collections.singleton("foo"); | ||
|
||
list5 = Collections.emptyList(); | ||
map5 = Collections.emptyMap(); | ||
navigableMap5 = Collections.emptyNavigableMap(); | ||
sortedMap5 = Collections.emptySortedMap(); | ||
set5 = Collections.emptySet(); | ||
navigableSet5 = Collections.emptyNavigableSet(); | ||
sortedSet5 = Collections.emptySortedSet(); | ||
} | ||
|
||
private final List<String> list; | ||
private final Map<String, String> map; | ||
private final Set<String> set; | ||
|
||
private final List<String> list2; | ||
private final Map<String, String> map2; | ||
private final Set<String> set2; | ||
|
||
private final List<String> list3; | ||
private final Map<String, String> map3; | ||
private final NavigableMap<String, String> navigableMap3; | ||
private final SortedMap<String, String> sortedMap3; | ||
private final Set<String> set3; | ||
private final NavigableSet<String> navigableSet3; | ||
private final SortedSet<String> sortedSet3; | ||
|
||
private final List<String> list4; | ||
private final Map<String, String> map4; | ||
private final Set<String> set4; | ||
|
||
private final List<String> list5; | ||
private final Map<String, String> map5; | ||
private final NavigableMap<String, String> navigableMap5; | ||
private final SortedMap<String, String> sortedMap5; | ||
private final Set<String> set5; | ||
private final NavigableSet<String> navigableSet5; | ||
private final SortedSet<String> sortedSet5; | ||
|
||
public List<String> getList() { | ||
return list; | ||
} | ||
|
||
public Map<String, String> getMap() { | ||
return map; | ||
} | ||
|
||
public Set<String> getSet() { | ||
return set; | ||
} | ||
|
||
public List<String> getList2() { | ||
return list2; | ||
} | ||
|
||
public Map<String, String> getMap2() { | ||
return map2; | ||
} | ||
|
||
public Set<String> getSet2() { | ||
return set2; | ||
} | ||
|
||
public List<String> getList3() { | ||
return list3; | ||
} | ||
|
||
public Map<String, String> getMap3() { | ||
return map3; | ||
} | ||
|
||
public NavigableMap<String, String> getNavigableMap3() { | ||
return navigableMap3; | ||
} | ||
|
||
public SortedMap<String, String> getSortedMap3() { | ||
return sortedMap3; | ||
} | ||
|
||
public Set<String> getSet3() { | ||
return set3; | ||
} | ||
|
||
public NavigableSet<String> getNavigableSet3() { | ||
return navigableSet3; | ||
} | ||
|
||
public SortedSet<String> getSortedSet3() { | ||
return sortedSet3; | ||
} | ||
|
||
public List<String> getList4() { | ||
return list4; | ||
} | ||
|
||
public Map<String, String> getMap4() { | ||
return map4; | ||
} | ||
|
||
public Set<String> getSet4() { | ||
return set4; | ||
} | ||
|
||
public List<String> getList5() { | ||
return list5; | ||
} | ||
|
||
public Map<String, String> getMap5() { | ||
return map5; | ||
} | ||
|
||
public NavigableMap<String, String> getNavigableMap5() { | ||
return navigableMap5; | ||
} | ||
|
||
public SortedMap<String, String> getSortedMap5() { | ||
return sortedMap5; | ||
} | ||
|
||
public Set<String> getSet5() { | ||
return set5; | ||
} | ||
|
||
public NavigableSet<String> getNavigableSet5() { | ||
return navigableSet5; | ||
} | ||
|
||
public SortedSet<String> getSortedSet5() { | ||
return sortedSet5; | ||
} | ||
} |