emul/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java
branchreflection
changeset 266 2e2e6f946208
parent 254 d0013f44e9b1
child 378 ccb1544a88bc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java	Wed Dec 05 10:03:58 2012 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +package org.apidesign.bck2brwsr.emul;
     1.9 +
    1.10 +import java.lang.annotation.Annotation;
    1.11 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.12 +
    1.13 +/**
    1.14 + *
    1.15 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.16 + */
    1.17 +public final class AnnotationImpl implements Annotation {
    1.18 +    public Class<? extends Annotation> annotationType() {
    1.19 +        return getClass();
    1.20 +    }
    1.21 +
    1.22 +    @JavaScriptBody(args = { "a", "n", "values" }, body = ""
    1.23 +        + "function f(v, p) {\n"
    1.24 +        + "  var val = v;\n"
    1.25 +        + "  var prop = p;\n"
    1.26 +        + "  return function() {\n"
    1.27 +        + "    return val[prop];\n"
    1.28 +        + "  };\n"
    1.29 +        + "}\n"
    1.30 +        + "var props = Object.getOwnPropertyNames(values);\n"
    1.31 +        + "for (var i = 0; i < props.length; i++) {\n"
    1.32 +        + "  var p = props[i];\n"
    1.33 +        + "  a[p] = new f(values, p);\n"
    1.34 +        + "}\n"
    1.35 +        + "a['$instOf_' + n] = true;\n"
    1.36 +        + "return a;"
    1.37 +    )
    1.38 +    private static <T extends Annotation> T create(AnnotationImpl a, String n, Object values) {
    1.39 +        return null;
    1.40 +    }
    1.41 +    public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
    1.42 +        return create(new AnnotationImpl(), annoClass.getName().replace('.', '_'), values);
    1.43 +    }
    1.44 +
    1.45 +    public static Annotation[] create(Object anno) {
    1.46 +        String[] names = findNames(anno);
    1.47 +        Annotation[] ret = new Annotation[names.length];
    1.48 +        for (int i = 0; i < names.length; i++) {
    1.49 +            String n = names[i].substring(1, names[i].length() - 1).replace('/', '_');
    1.50 +            ret[i] = create(new AnnotationImpl(), n, findData(anno, names[i]));
    1.51 +        }
    1.52 +        return ret;
    1.53 +    }
    1.54 +    @JavaScriptBody(args = "anno", body =
    1.55 +          "var arr = new Array();"
    1.56 +        + "var props = Object.getOwnPropertyNames(anno);\n"
    1.57 +        + "for (var i = 0; i < props.length; i++) {\n"
    1.58 +        + "  var p = props[i];\n"
    1.59 +        + "  arr.push(p);"
    1.60 +        + "}"
    1.61 +        + "return arr;"
    1.62 +    )
    1.63 +    private static String[] findNames(Object anno) {
    1.64 +        throw new UnsupportedOperationException();
    1.65 +    }
    1.66 +
    1.67 +    @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
    1.68 +    private static Object findData(Object anno, String p) {
    1.69 +        throw new UnsupportedOperationException();
    1.70 +    }
    1.71 +}