Skip to content

Commit

Permalink
fix NPE in Field.getAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
dicej committed Apr 17, 2014
1 parent a6f0923 commit b74f9e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions classpath/java/lang/reflect/Field.java
Expand Up @@ -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 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];
Expand All @@ -362,7 +367,7 @@ public <T extends Annotation> T getAnnotation(Class<T> 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) {
Expand Down
5 changes: 5 additions & 0 deletions test/Reflection.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -280,3 +283,5 @@ interface A {
}

interface B extends A { }

@interface Ann { }

0 comments on commit b74f9e3

Please sign in to comment.