File: NonCanonicalStaticImport.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 (37 lines) | stat: -rw-r--r-- 1,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Types should always be imported by their canonical name. The canonical name of a
top-level class is the fully-qualified name of the package, followed by a '.',
followed by the name of the class. The canonical name of a member class is the
canonical name of its declaring class, followed by a '.', followed by the name
of the member class.

Fully-qualified member class names are not guaranteed to be canonical. Consider
some member class M declared in a class C. There may be another class D that
extends C and inherits M. Therefore M can be accessed using the fully-qualified
name of D, followed by a '.', followed by 'M'. Since M is not declared in D,
this name is not canonical.

The JLS ยง7.5.3 requires all single static imports to *start* with a canonical
type name, but the fully-qualified name of the imported member is not required
to be canonical.

Importing types using non-canonical names is unnecessary and unclear, and should
be avoided.

Example:

```java
package a;

class One {
  static class Inner {}
}
```

```java
package a;

class Two extends One {}
```

An import of `Inner` should always refer to it using the canonical name
`a.One.Inner`, not `a.Two.Inner`.