New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive EI_EXPOSE_REP when java.util.Map.copyOf
is used
#1771
Comments
Thanks for opening your first issue here! 😃 |
The same is true for unmodifiable collections created by from streams using for example package spotbugs.test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Foo {
public Foo(Map<String, String> map, List<String> list, Set<String> set) {
this.map = Map.copyOf(map);
this.list = List.copyOf(list);
this.set = Set.copyOf(set);
}
private final Map<String, String> map;
private final List<String> list;
private final Set<String> set;
private final List list2 = Stream.of().collect(Collectors.toUnmodifiableList());
public Map<String, String> getMap() {
return map;
}
public List<String> getList() {
return list;
}
public Set<String> getSet() {
return set;
}
public List getList2() {
return list2;
}
} Updated sample project: sample-project.zip Findings:
|
Thank you for reporting this! I think that I understand the problem and I have an idea how to solve it. However, it is not easy because we need to find all possible assignments to the field and check whether all of them assigns an immutable concrete type. |
I think that I found the solution. Fix is in progress. Fortunately, we have field summaries thus it is not so difficult at all. |
#2141 fixes the original issue. |
These false positives correspond to regular object oriented design. SpotBugs has just become too stringent (see spotbugs/spotbugs#1601, spotbugs/spotbugs#1771, spotbugs/spotbugs#2083, spotbugs/spotbugs#2344, spotbugs/spotbugs#2356, spotbugs/spotbugs-gradle-plugin#731)
The following code triggers EI_EXPOSE_REP
Since
Map.copyOf
returns an unmodifiable map (java.util.ImmutableCollections.AbstractImmutableMap
), this is a false positive.The same is true for
List.copyOf
andSet.copyOf
.Sample project:
copyof-issue.zip
The text was updated successfully, but these errors were encountered: