This example return EI_EXPOSE_REP, but emptyList(), emptySet() and emptyMap() are immutable collections.
public record UnmodifiableRecord(List<String> list, Set<String> set, Map<String, String> map) {
public UnmodifiableRecord {
list = list != null ? List.copyOf(list) : Collections.emptyList();
set = set != null ? Set.copyOf(set) : Collections.emptySet();
map = map != null ? Map.copyOf(map) : Collections.emptyMap();
}
}
public class UnmodifiableClass {
private final List<String> list;
private final Set<String> set;
private final Map<String, String> map;
public UnmodifiableClass(List<String> list, Set<String> set, Map<String, String> map) {
this.list = list != null ? List.copyOf(list) : Collections.emptyList();
this.set = set != null ? Set.copyOf(set) : Collections.emptySet();
this.map = map != null ? Map.copyOf(map) : Collections.emptyMap();
}
// getters
}
Here a example to reproduce with tests to show immutable of classes.
spotBugsIssue.zip
This example return EI_EXPOSE_REP, but
emptyList(),emptySet()andemptyMap()are immutable collections.Here a example to reproduce with tests to show immutable of classes.
spotBugsIssue.zip