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 private final Class<? extends Annotation> type;
32 public AnnotationImpl(Class<? extends Annotation> type) {
36 public Class<? extends Annotation> annotationType() {
40 @JavaScriptBody(args = { "a", "n", "arr", "values" }, body = ""
41 + "function f(val, prop, clazz) {\n"
42 + " return function() {\n"
43 + " if (clazz == null) return val[prop];\n"
44 + " return CLS.prototype.c__Ljava_lang_Object_2Ljava_lang_Class_2Ljava_lang_Object_2(clazz, val[prop]);\n"
47 + "for (var i = 0; i < arr.length; i += 3) {\n"
48 + " var m = arr[i];\n"
49 + " var p = arr[i + 1];\n"
50 + " var c = arr[i + 2];\n"
51 + " a[m] = f(values, p, c);\n"
53 + "a['$instOf_' + n] = true;\n"
56 private static native <T extends Annotation> T create(
57 AnnotationImpl a, String n, Object[] methodsAndProps, Object values
60 private static Object c(Class<? extends Annotation> a, Object v) {
64 public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
65 return create(new AnnotationImpl(annoClass),
66 annoClass.getName().replace('.', '_'),
67 findProps(annoClass), values
71 public static Annotation[] create(Object anno) {
72 String[] names = findNames(anno);
73 Annotation[] ret = new Annotation[names.length];
74 for (int i = 0; i < names.length; i++) {
75 String annoNameSlash = names[i].substring(1, names[i].length() - 1);
76 Class<? extends Annotation> annoClass;
78 annoClass = (Class<? extends Annotation>)Class.forName(annoNameSlash.replace('/', '.'));
79 } catch (ClassNotFoundException ex) {
80 throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
83 new AnnotationImpl(annoClass),
84 annoNameSlash.replace('/', '_'),
86 findData(anno, names[i])
91 @JavaScriptBody(args = "anno", body =
92 "var arr = new Array();"
93 + "var props = Object.getOwnPropertyNames(anno);\n"
94 + "for (var i = 0; i < props.length; i++) {\n"
95 + " var p = props[i];\n"
100 private static native String[] findNames(Object anno);
102 @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
103 private static native Object findData(Object anno, String p);
105 private static Object[] findProps(Class<?> annoClass) {
106 final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
107 Object[] arr = new Object[marr.length * 3];
109 for (Method m : marr) {
110 arr[pos++] = MethodImpl.toSignature(m);
111 arr[pos++] = m.getName();
112 arr[pos++] = m.getReturnType().isAnnotation() ? m.getReturnType() : null;