Fix NPE in Field#getAnnotation

When the class whose field is to be inspected has no annotations at all,
at least my javac here (1.6.0_51 on MacOSX) does not produce any class
addendum.

Therefore, let's verify that the addendum is not null before proceeding.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2013-10-25 15:35:52 -05:00
parent 513f02ebd3
commit ff50034206
2 changed files with 9 additions and 1 deletions

View File

@ -225,7 +225,7 @@ public class Field<T> extends AccessibleObject {
}
public <T extends Annotation> T getAnnotation(Class<T> class_) {
if (vmField.addendum.annotationTable != null) {
if (vmField.addendum != null && vmField.addendum.annotationTable != null) {
Object[] table = (Object[]) vmField.addendum.annotationTable;
for (int i = 0; i < table.length; ++i) {
Object[] a = (Object[]) table[i];

View File

@ -47,8 +47,16 @@ public class Reflection {
expect(Hello.class == inner[0]);
}
private int egads;
private static void annotations() throws Exception {
Field egads = Reflection.class.getDeclaredField("egads");
expect(egads.getAnnotation(Deprecated.class) == null);
}
public static void main(String[] args) throws Exception {
innerClasses();
annotations();
Class system = Class.forName("java.lang.System");
Field out = system.getDeclaredField("out");