Skip to content

Instantly share code, notes, and snippets.

@raphw
Created November 17, 2014 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphw/baeb31d81322d3dba482 to your computer and use it in GitHub Desktop.
Save raphw/baeb31d81322d3dba482 to your computer and use it in GitHub Desktop.
NullPointerException demo
import java.lang.annotation.*;
public class ManifestAnnotation implements SampleAnnotation {
private final String value;
public ManifestAnnotation(String value) {
this.value = value;
}
public Class<? extends Annotation> annotationType() {
return ManifestAnnotation.class;
}
public String value() {
return value;
}
}
@SampleAnnotation
public class NullPointerSample {
public static void main(String[] args) {
System.out.println("-- hash code --"); // results in 0
System.out.println(NullPointerSample.class.getAnnotation(SampleAnnotation.class).hashCode());
System.out.println("-- to string --"); // ignores missing default value
System.out.println(NullPointerSample.class.getAnnotation(SampleAnnotation.class).toString());
System.out.println("-- equals --"); // throws NullPointerException, but should be false?
System.out.println(NullPointerSample.class.getAnnotation(SampleAnnotation.class)
.equals(new ManifestAnnotation("bar")));
}
}
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
public @interface SampleAnnotation {
// NullPointerSample is compiled with default value of "foo".
String value(); // default "foo";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment