rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java
changeset 772 d382dacfd73f
parent 665 799a8cfefe35
child 1889 e1953d8b8338
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,130 @@
     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.reflect;
    1.22 +
    1.23 +import java.lang.annotation.Annotation;
    1.24 +import java.lang.reflect.Method;
    1.25 +import java.lang.reflect.Modifier;
    1.26 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.27 +
    1.28 +/**
    1.29 + *
    1.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.31 + */
    1.32 +public final class AnnotationImpl implements Annotation {
    1.33 +    private final Class<? extends Annotation> type;
    1.34 +
    1.35 +    public AnnotationImpl(Class<? extends Annotation> type) {
    1.36 +        this.type = type;
    1.37 +    }
    1.38 +    
    1.39 +    public Class<? extends Annotation> annotationType() {
    1.40 +        return type;
    1.41 +    }
    1.42 +
    1.43 +    @JavaScriptBody(args = { "a", "n", "arr", "values" }, body = ""
    1.44 +        + "function f(val, prop, clazz) {\n"
    1.45 +        + "  return function() {\n"
    1.46 +        + "    if (clazz == null) return val[prop];\n"
    1.47 +        + "    if (clazz.isArray__Z()) {\n"
    1.48 +        + "      var valarr = val[prop];\n"
    1.49 +        + "      var cmp = clazz.getComponentType__Ljava_lang_Class_2();\n"
    1.50 +        + "      var retarr = vm.java_lang_reflect_Array(false).newInstance__Ljava_lang_Object_2Ljava_lang_Class_2I(cmp, valarr.length);\n"
    1.51 +        + "      for (var i = 0; i < valarr.length; i++) {\n"
    1.52 +        + "        retarr[i] = CLS.prototype.c__Ljava_lang_Object_2Ljava_lang_Class_2Ljava_lang_Object_2(cmp, valarr[i]);\n"
    1.53 +        + "      }\n"
    1.54 +        + "      return retarr;\n"
    1.55 +        + "    }\n"
    1.56 +        + "    return CLS.prototype.c__Ljava_lang_Object_2Ljava_lang_Class_2Ljava_lang_Object_2(clazz, val[prop]);\n"
    1.57 +        + "  };\n"
    1.58 +        + "}\n"
    1.59 +        + "for (var i = 0; i < arr.length; i += 3) {\n"
    1.60 +        + "  var m = arr[i];\n"
    1.61 +        + "  var p = arr[i + 1];\n"
    1.62 +        + "  var c = arr[i + 2];\n"
    1.63 +        + "  a[m] = f(values, p, c);\n"
    1.64 +        + "}\n"
    1.65 +        + "a['$instOf_' + n] = true;\n"
    1.66 +        + "return a;"
    1.67 +    )
    1.68 +    private static native <T extends Annotation> T create(
    1.69 +        AnnotationImpl a, String n, Object[] methodsAndProps, Object values
    1.70 +    );
    1.71 +    
    1.72 +    private static Object c(Class<? extends Annotation> a, Object v) {
    1.73 +        return create(a, v);
    1.74 +    }
    1.75 +    
    1.76 +    public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
    1.77 +        return create(new AnnotationImpl(annoClass), 
    1.78 +            annoClass.getName().replace('.', '_'), 
    1.79 +            findProps(annoClass), values
    1.80 +        );
    1.81 +    }
    1.82 +
    1.83 +    public static Annotation[] create(Object anno) {
    1.84 +        String[] names = findNames(anno);
    1.85 +        Annotation[] ret = new Annotation[names.length];
    1.86 +        for (int i = 0; i < names.length; i++) {
    1.87 +            String annoNameSlash = names[i].substring(1, names[i].length() - 1);
    1.88 +            Class<? extends Annotation> annoClass;
    1.89 +            try {
    1.90 +                annoClass = (Class<? extends Annotation>)Class.forName(annoNameSlash.replace('/', '.'));
    1.91 +            } catch (ClassNotFoundException ex) {
    1.92 +                throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
    1.93 +            }
    1.94 +            ret[i] = create(
    1.95 +                new AnnotationImpl(annoClass), 
    1.96 +                annoNameSlash.replace('/', '_'),
    1.97 +                findProps(annoClass),
    1.98 +                findData(anno, names[i])
    1.99 +            );
   1.100 +        }
   1.101 +        return ret;
   1.102 +    }
   1.103 +    @JavaScriptBody(args = "anno", body =
   1.104 +          "var arr = new Array();"
   1.105 +        + "var props = Object.getOwnPropertyNames(anno);\n"
   1.106 +        + "for (var i = 0; i < props.length; i++) {\n"
   1.107 +        + "  var p = props[i];\n"
   1.108 +        + "  arr.push(p);"
   1.109 +        + "}"
   1.110 +        + "return arr;"
   1.111 +    )
   1.112 +    private static native String[] findNames(Object anno);
   1.113 +
   1.114 +    @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
   1.115 +    private static native Object findData(Object anno, String p);
   1.116 +
   1.117 +    private static Object[] findProps(Class<?> annoClass) {
   1.118 +        final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
   1.119 +        Object[] arr = new Object[marr.length * 3];
   1.120 +        int pos = 0;
   1.121 +        for (Method m : marr) {
   1.122 +            arr[pos++] = MethodImpl.toSignature(m);
   1.123 +            arr[pos++] = m.getName();
   1.124 +            final Class<?> rt = m.getReturnType();
   1.125 +            if (rt.isArray()) {
   1.126 +                arr[pos++] = rt.getComponentType().isAnnotation() ? rt : null;
   1.127 +            } else {
   1.128 +                arr[pos++] = rt.isAnnotation() ? rt : null;
   1.129 +            }
   1.130 +        }
   1.131 +        return arr;
   1.132 +    }
   1.133 +}