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