Created
November 17, 2014 12:13
-
-
Save raphw/baeb31d81322d3dba482 to your computer and use it in GitHub Desktop.
NullPointerException demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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