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 no 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 NullnessExample { 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 /*@NonNull*/ String bar = "bar"; 026 027 foo = bar; 028 bar = foo; 029 030 } 031 032 @SuppressWarnings("unused") 033 public/* @NonNull */String exampleGenerics() { 034 035 List</*@NonNull*/ String> foo = new LinkedList</*@NonNull*/ String>(); 036 List</*@NonNull*/ String> bar = foo; 037 038 /*@NonNull*/ String quux = "quux"; 039 foo.add(quux); 040 foo.add("quux"); 041 /*@NonNull*/ String baz = foo.get(0); 042 return baz; 043 044 } 045 046}