fix NPE in Field.getAnnotations

This commit is contained in:
Joel Dice 2014-04-17 13:16:21 -06:00
parent a6f0923678
commit b74f9e32e9
2 changed files with 12 additions and 2 deletions

View File

@ -348,8 +348,13 @@ public class Field<T> extends AccessibleObject {
return (Annotation) a[0];
}
private boolean hasAnnotations() {
return vmField.addendum != null
&& vmField.addendum.annotationTable != null;
}
public <T extends Annotation> T getAnnotation(Class<T> 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 class Field<T> extends AccessibleObject {
}
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) {

View File

@ -252,6 +252,9 @@ public class Reflection {
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 { }