From ff50034206a34a19650c0cefc757217b8fd05fbf Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 25 Oct 2013 15:35:52 -0500 Subject: [PATCH] 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 --- classpath/java/lang/reflect/Field.java | 2 +- test/Reflection.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/classpath/java/lang/reflect/Field.java b/classpath/java/lang/reflect/Field.java index 91ad0f9670..5b686a0fa2 100644 --- a/classpath/java/lang/reflect/Field.java +++ b/classpath/java/lang/reflect/Field.java @@ -225,7 +225,7 @@ public class Field extends AccessibleObject { } public T getAnnotation(Class 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]; diff --git a/test/Reflection.java b/test/Reflection.java index 306b6959c2..8d889837b4 100644 --- a/test/Reflection.java +++ b/test/Reflection.java @@ -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");