001 package ps2;
002
003 import java.util.*;
004
005 /**
006 * This class illustrates use of nullness type annotations.
007 * The class doesn't do anything -- it is merely meant to be compiled.
008 * Compilation will produce warning messages.
009 * <p>
010 *
011 * There are two related files that differ only slightly:
012 * {@link NullnessExample}, an example of correct use, and {@link
013 * NullnessExampleWithWarnings}, an example of incorrect use.
014 * See the Nullness checker documentation for larger examples of annotated code.
015 **/
016 public class NullnessExampleWithWarnings {
017
018 public void example() {
019
020 // In general, you do not have to annotate local variables, because the
021 // Nullness Checker infers such annotations. It is written here in the
022 // example for emhpasis.
023 /*@NonNull*/ String foo = "foo";
024 String bar = null;
025
026 foo = bar;
027 bar = foo;
028
029 }
030
031 public /*@NonNull*/ String exampleGenerics() {
032
033 List</*@NonNull*/ String> foo = new LinkedList</*@NonNull*/ String>();
034 List<String> bar = foo;
035
036 String quux = null;
037 foo.add(quux);
038 foo.add("quux");
039 /*@NonNull*/ String baz = foo.get(0);
040 return baz;
041
042 }
043
044 }