emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 04 Feb 2013 23:22:54 +0100
branchreflection
changeset 663 09225928b9fb
parent 662 7832188e26b8
child 665 799a8cfefe35
permissions -rw-r--r--
Simplifying the JavaScript code a bit
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.emul.reflect;
    19 
    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;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public final class AnnotationImpl implements Annotation {
    30     private final Class<? extends Annotation> type;
    31 
    32     public AnnotationImpl(Class<? extends Annotation> type) {
    33         this.type = type;
    34     }
    35     
    36     public Class<? extends Annotation> annotationType() {
    37         return type;
    38     }
    39 
    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"
    45         + "  };\n"
    46         + "}\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"
    52         + "}\n"
    53         + "a['$instOf_' + n] = true;\n"
    54         + "return a;"
    55     )
    56     private static native <T extends Annotation> T create(
    57         AnnotationImpl a, String n, Object[] methodsAndProps, Object values
    58     );
    59     
    60     private static Object c(Class<? extends Annotation> a, Object v) {
    61         return create(a, v);
    62     }
    63     
    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
    68         );
    69     }
    70 
    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;
    77             try {
    78                 annoClass = (Class<? extends Annotation>)Class.forName(annoNameSlash.replace('/', '.'));
    79             } catch (ClassNotFoundException ex) {
    80                 throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
    81             }
    82             ret[i] = create(
    83                 new AnnotationImpl(annoClass), 
    84                 annoNameSlash.replace('/', '_'),
    85                 findProps(annoClass),
    86                 findData(anno, names[i])
    87             );
    88         }
    89         return ret;
    90     }
    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"
    96         + "  arr.push(p);"
    97         + "}"
    98         + "return arr;"
    99     )
   100     private static native String[] findNames(Object anno);
   101 
   102     @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
   103     private static native Object findData(Object anno, String p);
   104 
   105     private static Object[] findProps(Class<?> annoClass) {
   106         final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
   107         Object[] arr = new Object[marr.length * 3];
   108         int pos = 0;
   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;
   113         }
   114         return arr;
   115     }
   116 }