File: NullnessExample.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (38 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download | duplicates (3)
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
38
import java.util.LinkedList;
import java.util.List;
import org.checkerframework.checker.nullness.qual.*;

/**
 * This class illustrates use of nullness type annotations. The class doesn't do anything -- it is
 * merely meant to be compiled. Compilation will produce no warning messages.
 *
 * <p>There are two related files that differ only slightly: {@link NullnessExample}, an example of
 * correct use, and {@link NullnessExampleWithWarnings}, an example of incorrect use. See the
 * Nullness Checker documentation for larger examples of annotated code.
 */
public class NullnessExample {

    public void example() {

        // In general, you do not have to annotate local variables, because the
        // Nullness Checker infers such annotations.  It is written here in the
        // example for emhpasis.
        @NonNull String foo = "foo";
        @NonNull String bar = "bar";

        foo = bar;
        bar = foo;
    }

    public @NonNull String exampleGenerics() {

        List<@NonNull String> foo = new LinkedList<@NonNull String>();
        List<@NonNull String> bar = foo;

        @NonNull String quux = "quux";
        foo.add(quux);
        foo.add("quux");
        @NonNull String baz = foo.get(0);
        return baz;
    }
}