File: NonCanonicalType.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (18 lines) | stat: -rw-r--r-- 733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Types being referred to by non-canonical names can be confusing. For example,

```java
public final class Entries {
  private final ImmutableList<ImmutableMap.Entry<String, Long>> entries;

  public Entries(Map<String, Long> map) {
    this.entries = ImmutableList.copyOf(map.entrySet());
  }
}
```

There is nothing special about `ImmutableMap.Entry`; it is precisely the same
type as `Map.Entry`. This example makes it look deceptively as though
`ImmutableList<ImmutableMap.Entry<?, ?>>` is an immutable type and therefore
safe to store indefinitely, when really it offers no more safety than
`ImmutableList<Map.Entry<?, ?>>`. You should use `ImmutableList<Map.Entry<?,
?>>` instead, so it's obvious what type you're referring to.