Permalink
Browse files

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<T>) on methods without any annotations.

Added some tests to cover these cases.
  • Loading branch information...
1 parent 0addd8c commit a5c9dd6f248c6e3254b562b0830a7bcead429eea @mkeesey mkeesey committed May 22, 2012
@@ -24,4 +24,8 @@
public MethodAddendum addendum;
public VMClass class_;
public Object code;
+
+ public boolean hasAnnotations() {
+ return addendum != null && addendum.annotationTable != null;
+ }
}
@@ -170,7 +170,7 @@ private Annotation getAnnotation(Object[] a) {
}
public <T extends Annotation> T getAnnotation(Class<T> 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 @@ private Annotation getAnnotation(Object[] a) {
}
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) {
@@ -358,7 +358,7 @@ private static Class makeClass(ClassLoader loader,
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<String,avian.VMMethod> virtualMap = new HashMap();
View
@@ -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
View
@@ -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 static void main(String[] args) throws Exception {
.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 static void main(String[] args) throws Exception {
public static void foo() {
}
-
- @Retention(RetentionPolicy.RUNTIME)
- private @interface Test {
- public String value();
- }
-
- @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
+
+ public static void noAnnotation() {
+
}
-
}
@@ -0,0 +1,5 @@
+package avian.testing.annotations;
+
+public enum Color {
+ Red, Yellow, Blue
+}
@@ -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();
+}
@@ -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();
+}
@@ -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();
+}

0 comments on commit a5c9dd6

Please sign in to comment.