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