emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java
branchemul
changeset 554 05224402145d
parent 378 ccb1544a88bc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java	Wed Jan 23 20:39:23 2013 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.emul;
    1.22 +
    1.23 +import java.lang.annotation.Annotation;
    1.24 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.25 +
    1.26 +/**
    1.27 + *
    1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.29 + */
    1.30 +public final class AnnotationImpl implements Annotation {
    1.31 +    public Class<? extends Annotation> annotationType() {
    1.32 +        return getClass();
    1.33 +    }
    1.34 +
    1.35 +    @JavaScriptBody(args = { "a", "n", "values" }, body = ""
    1.36 +        + "function f(v, p) {\n"
    1.37 +        + "  var val = v;\n"
    1.38 +        + "  var prop = p;\n"
    1.39 +        + "  return function() {\n"
    1.40 +        + "    return val[prop];\n"
    1.41 +        + "  };\n"
    1.42 +        + "}\n"
    1.43 +        + "var props = Object.getOwnPropertyNames(values);\n"
    1.44 +        + "for (var i = 0; i < props.length; i++) {\n"
    1.45 +        + "  var p = props[i];\n"
    1.46 +        + "  a[p] = new f(values, p);\n"
    1.47 +        + "}\n"
    1.48 +        + "a['$instOf_' + n] = true;\n"
    1.49 +        + "return a;"
    1.50 +    )
    1.51 +    private static <T extends Annotation> T create(AnnotationImpl a, String n, Object values) {
    1.52 +        return null;
    1.53 +    }
    1.54 +    public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
    1.55 +        return create(new AnnotationImpl(), annoClass.getName().replace('.', '_'), values);
    1.56 +    }
    1.57 +
    1.58 +    public static Annotation[] create(Object anno) {
    1.59 +        String[] names = findNames(anno);
    1.60 +        Annotation[] ret = new Annotation[names.length];
    1.61 +        for (int i = 0; i < names.length; i++) {
    1.62 +            String n = names[i].substring(1, names[i].length() - 1).replace('/', '_');
    1.63 +            ret[i] = create(new AnnotationImpl(), n, findData(anno, names[i]));
    1.64 +        }
    1.65 +        return ret;
    1.66 +    }
    1.67 +    @JavaScriptBody(args = "anno", body =
    1.68 +          "var arr = new Array();"
    1.69 +        + "var props = Object.getOwnPropertyNames(anno);\n"
    1.70 +        + "for (var i = 0; i < props.length; i++) {\n"
    1.71 +        + "  var p = props[i];\n"
    1.72 +        + "  arr.push(p);"
    1.73 +        + "}"
    1.74 +        + "return arr;"
    1.75 +    )
    1.76 +    private static String[] findNames(Object anno) {
    1.77 +        throw new UnsupportedOperationException();
    1.78 +    }
    1.79 +
    1.80 +    @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
    1.81 +    private static Object findData(Object anno, String p) {
    1.82 +        throw new UnsupportedOperationException();
    1.83 +    }
    1.84 +}