2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.emul.reflect;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public final class AnnotationImpl implements Annotation {
30 public Class<? extends Annotation> annotationType() {
34 @JavaScriptBody(args = { "a", "n", "arr", "values" }, body = ""
35 + "function f(v, p) {\n"
38 + " return function() {\n"
39 + " return val[prop];\n"
42 + "for (var i = 0; i < arr.length; i += 2) {\n"
43 + " var m = arr[i];\n"
44 + " var p = arr[i + 1];\n"
45 + " a[m] = new f(values, p);\n"
47 + "a['$instOf_' + n] = true;\n"
50 private static native <T extends Annotation> T create(
51 AnnotationImpl a, String n, String[] methodsAndProps, Object values
54 public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
55 return create(new AnnotationImpl(),
56 annoClass.getName().replace('.', '_'),
57 findProps(annoClass), values
61 public static Annotation[] create(Object anno) {
62 String[] names = findNames(anno);
63 Annotation[] ret = new Annotation[names.length];
64 for (int i = 0; i < names.length; i++) {
65 String annoNameSlash = names[i].substring(1, names[i].length() - 1);
68 annoClass = Class.forName(annoNameSlash.replace('/', '.'));
69 } catch (ClassNotFoundException ex) {
70 throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
74 annoNameSlash.replace('/', '_'),
76 findData(anno, names[i])
81 @JavaScriptBody(args = "anno", body =
82 "var arr = new Array();"
83 + "var props = Object.getOwnPropertyNames(anno);\n"
84 + "for (var i = 0; i < props.length; i++) {\n"
85 + " var p = props[i];\n"
90 private static native String[] findNames(Object anno);
92 @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
93 private static native Object findData(Object anno, String p);
95 private static String[] findProps(Class<?> annoClass) {
96 final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
97 String[] arr = new String[marr.length * 2];
99 for (Method m : marr) {
100 arr[pos++] = MethodImpl.toSignature(m);
101 arr[pos++] = m.getName();