From a5c9dd6f248c6e3254b562b0830a7bcead429eea Mon Sep 17 00:00:00 2001 From: Mike Keesey Date: Tue, 22 May 2012 14:00:31 -0600 Subject: [PATCH] Fixing some issues with runtime annotations within avian. We were not properly converting dots to slashes internally for package names and we did not properly handle Method.getAnnotations and Method.getAnnotation(Class) on methods without any annotations. Added some tests to cover these cases. --- classpath/avian/VMMethod.java | 4 +++ classpath/java/lang/reflect/Method.java | 4 +-- classpath/java/lang/reflect/Proxy.java | 2 +- makefile | 5 ++- test/Annotations.java | 35 ++++++++----------- test/avian/testing/annotations/Color.java | 5 +++ test/avian/testing/annotations/Test.java | 9 +++++ test/avian/testing/annotations/TestEnum.java | 8 +++++ .../testing/annotations/TestInteger.java | 8 +++++ 9 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 test/avian/testing/annotations/Color.java create mode 100644 test/avian/testing/annotations/Test.java create mode 100644 test/avian/testing/annotations/TestEnum.java create mode 100644 test/avian/testing/annotations/TestInteger.java diff --git a/classpath/avian/VMMethod.java b/classpath/avian/VMMethod.java index e68d725242..843dd6012e 100644 --- a/classpath/avian/VMMethod.java +++ b/classpath/avian/VMMethod.java @@ -24,4 +24,8 @@ public class VMMethod { public MethodAddendum addendum; public VMClass class_; public Object code; + + public boolean hasAnnotations() { + return addendum != null && addendum.annotationTable != null; + } } diff --git a/classpath/java/lang/reflect/Method.java b/classpath/java/lang/reflect/Method.java index 2c0addfaae..f90ed311a5 100644 --- a/classpath/java/lang/reflect/Method.java +++ b/classpath/java/lang/reflect/Method.java @@ -170,7 +170,7 @@ public class Method extends AccessibleObject implements Member { } public T getAnnotation(Class class_) { - if (vmMethod.addendum.annotationTable != null) { + if (vmMethod.hasAnnotations()) { Object[] table = (Object[]) vmMethod.addendum.annotationTable; for (int i = 0; i < table.length; ++i) { Object[] a = (Object[]) table[i]; @@ -183,7 +183,7 @@ public class Method extends AccessibleObject implements Member { } public Annotation[] getAnnotations() { - if (vmMethod.addendum.annotationTable != null) { + if (vmMethod.hasAnnotations()) { Object[] table = (Object[]) vmMethod.addendum.annotationTable; Annotation[] array = new Annotation[table.length]; for (int i = 0; i < table.length; ++i) { diff --git a/classpath/java/lang/reflect/Proxy.java b/classpath/java/lang/reflect/Proxy.java index 06805fc72c..cfc35a6077 100644 --- a/classpath/java/lang/reflect/Proxy.java +++ b/classpath/java/lang/reflect/Proxy.java @@ -358,7 +358,7 @@ public class Proxy { int[] interfaceIndexes = new int[interfaces.length]; for (int i = 0; i < interfaces.length; ++i) { interfaceIndexes[i] = ConstantPool.addClass - (pool, interfaces[i].getName()); + (pool, interfaces[i].getName().replace('.', '/')); } Map virtualMap = new HashMap(); diff --git a/makefile b/makefile index 86bc33e9b1..42ef1b95e7 100755 --- a/makefile +++ b/makefile @@ -685,7 +685,10 @@ vm-classes = \ avian/*.class \ avian/resource/*.class +test-support-sources = $(shell find $(test)/avian/ -name '*.java') test-sources = $(wildcard $(test)/*.java) +test-sources += $(test-support-sources) +test-support-classes = $(call java-classes, $(test-support-sources),$(test),$(test-build)) test-classes = $(call java-classes,$(test-sources),$(test),$(test-build)) test-dep = $(test-build).dep @@ -765,7 +768,7 @@ vg: build test: build $(library-path) /bin/sh $(test)/test.sh 2>/dev/null \ $(test-executable) $(mode) "$(test-flags)" \ - $(call class-names,$(test-build),$(test-classes)) \ + $(call class-names,$(test-build),$(filter-out $(test-support-classes), $(test-classes))) \ $(continuation-tests) $(tail-tests) .PHONY: tarball diff --git a/test/Annotations.java b/test/Annotations.java index 17929065d1..c0234d21c0 100644 --- a/test/Annotations.java +++ b/test/Annotations.java @@ -1,7 +1,10 @@ -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; +import avian.testing.annotations.Color; +import avian.testing.annotations.Test; +import avian.testing.annotations.TestEnum; +import avian.testing.annotations.TestInteger; + public class Annotations { private static void expect(boolean v) { if (! v) throw new RuntimeException(); @@ -18,6 +21,12 @@ public class Annotations { .equals(Color.Red)); expect(((TestInteger) m.getAnnotation(TestInteger.class)).value() == 42); + + expect(m.getAnnotations().length == 3); + + Method noAnno = Annotations.class.getMethod("noAnnotation"); + expect(noAnno.getAnnotation(Test.class) == null); + expect(noAnno.getAnnotations().length == 0); } @Test("couscous") @@ -26,24 +35,8 @@ public class Annotations { public static void foo() { } - - @Retention(RetentionPolicy.RUNTIME) - private @interface Test { - public String value(); + + public static void noAnnotation() { + } - - @Retention(RetentionPolicy.RUNTIME) - private @interface TestEnum { - public Color value(); - } - - @Retention(RetentionPolicy.RUNTIME) - private @interface TestInteger { - public int value(); - } - - private static enum Color { - Red, Yellow, Blue - } - } diff --git a/test/avian/testing/annotations/Color.java b/test/avian/testing/annotations/Color.java new file mode 100644 index 0000000000..f054b0420a --- /dev/null +++ b/test/avian/testing/annotations/Color.java @@ -0,0 +1,5 @@ +package avian.testing.annotations; + +public enum Color { + Red, Yellow, Blue +} \ No newline at end of file diff --git a/test/avian/testing/annotations/Test.java b/test/avian/testing/annotations/Test.java new file mode 100644 index 0000000000..f9ba919647 --- /dev/null +++ b/test/avian/testing/annotations/Test.java @@ -0,0 +1,9 @@ +package avian.testing.annotations; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Test { + public String value(); +} diff --git a/test/avian/testing/annotations/TestEnum.java b/test/avian/testing/annotations/TestEnum.java new file mode 100644 index 0000000000..f8a252fd54 --- /dev/null +++ b/test/avian/testing/annotations/TestEnum.java @@ -0,0 +1,8 @@ +package avian.testing.annotations; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface TestEnum { + public Color value(); +} \ No newline at end of file diff --git a/test/avian/testing/annotations/TestInteger.java b/test/avian/testing/annotations/TestInteger.java new file mode 100644 index 0000000000..b7ec78d0bc --- /dev/null +++ b/test/avian/testing/annotations/TestInteger.java @@ -0,0 +1,8 @@ +package avian.testing.annotations; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface TestInteger { + public int value(); +} \ No newline at end of file