From b74f9e32e90d33017df89bb2619e6076ada63704 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 17 Apr 2014 13:16:21 -0600 Subject: [PATCH] fix NPE in Field.getAnnotations --- classpath/java/lang/reflect/Field.java | 9 +++++++-- test/Reflection.java | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/classpath/java/lang/reflect/Field.java b/classpath/java/lang/reflect/Field.java index 05512f108..6ffc344d4 100644 --- a/classpath/java/lang/reflect/Field.java +++ b/classpath/java/lang/reflect/Field.java @@ -348,8 +348,13 @@ private Annotation getAnnotation(Object[] a) { return (Annotation) a[0]; } + private boolean hasAnnotations() { + return vmField.addendum != null + && vmField.addendum.annotationTable != null; + } + public T getAnnotation(Class class_) { - if (vmField.addendum != null && vmField.addendum.annotationTable != null) { + if (hasAnnotations()) { Object[] table = (Object[]) vmField.addendum.annotationTable; for (int i = 0; i < table.length; ++i) { Object[] a = (Object[]) table[i]; @@ -362,7 +367,7 @@ public T getAnnotation(Class class_) { } public Annotation[] getAnnotations() { - if (vmField.addendum.annotationTable != null) { + if (hasAnnotations()) { Object[] table = (Object[]) vmField.addendum.annotationTable; Annotation[] array = new Annotation[table.length]; for (int i = 0; i < table.length; ++i) { diff --git a/test/Reflection.java b/test/Reflection.java index 2a59548be..96305010e 100644 --- a/test/Reflection.java +++ b/test/Reflection.java @@ -252,6 +252,9 @@ public void run() { expect(avian.testing.annotations.Test.class.getPackage().getName().equals ("avian.testing.annotations")); + + expect(Baz.class.getField("foo").getAnnotation(Ann.class) == null); + expect(Baz.class.getField("foo").getAnnotations().length == 0); } protected static class Baz { @@ -280,3 +283,5 @@ interface A { } interface B extends A { } + +@interface Ann { }