File: NullableOnContainingClass.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 (27 lines) | stat: -rw-r--r-- 908 bytes parent folder | download
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
The correct syntax to apply a `TYPE_USE` annotation to an inner class is
`A.@Nullable B`.

For a `TYPE_USE` `@Nullable` annotation, `@Nullable A.B` is legal Java if `B` is
a non-static inner class:

```java
class A {
  @Target(TYPE_USE)
  @interface Nullable {}

  class B {}
  static class C {}

  void test(A.@Nullable B x) {} // B is annotated ('A' is the enclosing instance type)
  void test(A.@Nullable C x) {} // C is annotated ('A' is a 'scoping construct' here)
}
```

```java
  void test(@Nullable A.B x) {} // compiles, but likely incorrect: annotates the enclosing instance type 'A', which can never be null
  void test(@Nullable A.C x) {} // compile error: 'A' cannot be annotated
```

However, for `@Nullable` (and `@NonNull`, and friends), annotating the outer
class is meaningless. The reference to the outer class (`A.this`) can never be
`null`, so any nullability annotations are redundant.