emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 03 Feb 2013 22:58:42 +0100
branchreflection
changeset 654 26a86cc00224
parent 652 f095ea52f417
child 662 7832188e26b8
permissions -rw-r--r--
Correct getDeclaringClass for returned methods
     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(v, p) {\n"
    42         + "  var val = v;\n"
    43         + "  var prop = p;\n"
    44         + "  return function() {\n"
    45         + "    return val[prop];\n"
    46         + "  };\n"
    47         + "}\n"
    48         + "for (var i = 0; i < arr.length; i += 2) {\n"
    49         + "  var m = arr[i];\n"
    50         + "  var p = arr[i + 1];\n"
    51         + "  a[m] = new f(values, p);\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, String[] methodsAndProps, Object values
    58     );
    59     
    60     public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
    61         return create(new AnnotationImpl(annoClass), 
    62             annoClass.getName().replace('.', '_'), 
    63             findProps(annoClass), values
    64         );
    65     }
    66 
    67     public static Annotation[] create(Object anno) {
    68         String[] names = findNames(anno);
    69         Annotation[] ret = new Annotation[names.length];
    70         for (int i = 0; i < names.length; i++) {
    71             String annoNameSlash = names[i].substring(1, names[i].length() - 1);
    72             Class<? extends Annotation> annoClass;
    73             try {
    74                 annoClass = (Class<? extends Annotation>)Class.forName(annoNameSlash.replace('/', '.'));
    75             } catch (ClassNotFoundException ex) {
    76                 throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
    77             }
    78             ret[i] = create(
    79                 new AnnotationImpl(annoClass), 
    80                 annoNameSlash.replace('/', '_'),
    81                 findProps(annoClass),
    82                 findData(anno, names[i])
    83             );
    84         }
    85         return ret;
    86     }
    87     @JavaScriptBody(args = "anno", body =
    88           "var arr = new Array();"
    89         + "var props = Object.getOwnPropertyNames(anno);\n"
    90         + "for (var i = 0; i < props.length; i++) {\n"
    91         + "  var p = props[i];\n"
    92         + "  arr.push(p);"
    93         + "}"
    94         + "return arr;"
    95     )
    96     private static native String[] findNames(Object anno);
    97 
    98     @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
    99     private static native Object findData(Object anno, String p);
   100 
   101     private static String[] findProps(Class<?> annoClass) {
   102         final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
   103         String[] arr = new String[marr.length * 2];
   104         int pos = 0;
   105         for (Method m : marr) {
   106             arr[pos++] = MethodImpl.toSignature(m);
   107             arr[pos++] = m.getName();
   108         }
   109         return arr;
   110     }
   111 }