Class.getAnnotations somehow implemented reflection
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 15:05:36 +0100
branchreflection
changeset 2385ab1f0890a42
parent 237 84ffc347412d
child 246 89ad48aa8705
Class.getAnnotations somehow implemented
emul/src/main/java/java/lang/AnnotationImpl.java
emul/src/main/java/java/lang/Class.java
vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java
     1.1 --- a/emul/src/main/java/java/lang/AnnotationImpl.java	Sun Dec 02 14:01:17 2012 +0100
     1.2 +++ b/emul/src/main/java/java/lang/AnnotationImpl.java	Sun Dec 02 15:05:36 2012 +0100
     1.3 @@ -30,4 +30,29 @@
     1.4      static <T extends Annotation> T create(Class<T> annoClass, Object values) {
     1.5          return create(new AnnotationImpl(), annoClass.getName().replace('.', '_'), values);
     1.6      }
     1.7 +
     1.8 +    static Annotation[] create(Object anno) {
     1.9 +        String[] names = findNames(anno);
    1.10 +        Annotation[] ret = new Annotation[names.length];
    1.11 +        for (int i = 0; i < names.length; i++) {
    1.12 +            String n = names[i].substring(1, names[i].length() - 1).replace('/', '_');
    1.13 +            ret[i] = create(new AnnotationImpl(), n, findData(anno, names[i]));
    1.14 +        }
    1.15 +        return ret;
    1.16 +    }
    1.17 +    @JavaScriptBody(args = "anno", body =
    1.18 +          "var arr = new Array();"
    1.19 +        + "for (p in anno) {"
    1.20 +        + "  arr.push(p);"
    1.21 +        + "}"
    1.22 +        + "return arr;"
    1.23 +    )
    1.24 +    private static String[] findNames(Object anno) {
    1.25 +        throw new UnsupportedOperationException();
    1.26 +    }
    1.27 +
    1.28 +    @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
    1.29 +    private static Object findData(Object anno, String p) {
    1.30 +        throw new UnsupportedOperationException();
    1.31 +    }
    1.32  }
     2.1 --- a/emul/src/main/java/java/lang/Class.java	Sun Dec 02 14:01:17 2012 +0100
     2.2 +++ b/emul/src/main/java/java/lang/Class.java	Sun Dec 02 15:05:36 2012 +0100
     2.3 @@ -804,12 +804,17 @@
     2.4          return getAnnotation(annotationClass) != null;
     2.5      }
     2.6  
     2.7 +    @JavaScriptBody(args = "self", body = "return self.anno;")
     2.8 +    private Object getAnnotationData() {
     2.9 +        throw new UnsupportedOperationException();
    2.10 +    }
    2.11  
    2.12      /**
    2.13       * @since 1.5
    2.14       */
    2.15      public Annotation[] getAnnotations() {
    2.16 -        throw new UnsupportedOperationException();
    2.17 +        Object data = getAnnotationData();
    2.18 +        return data == null ? new Annotation[0] : AnnotationImpl.create(data);
    2.19      }
    2.20  
    2.21      /**
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java	Sun Dec 02 14:01:17 2012 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java	Sun Dec 02 15:05:36 2012 +0100
     3.3 @@ -80,6 +80,9 @@
     3.4      @Test public void jsStringAnnotation() throws Exception {
     3.5          assertExec("Check class annotation", Classes.class, "getNamerLjava_lang_StringZ", "my text", true);
     3.6      }
     3.7 +    @Test public void jsStringAnnotationFromArray() throws Exception {
     3.8 +        assertExec("Check class annotation", Classes.class, "getNamerLjava_lang_StringZ", "my text", false);
     3.9 +    }
    3.10      
    3.11      private static CharSequence codeSeq;
    3.12      private static Invocable code;