emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/reflect/AnnotationImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 04 Feb 2013 23:18:51 +0100
branchreflection
changeset 662 7832188e26b8
parent 654 26a86cc00224
child 663 09225928b9fb
permissions -rw-r--r--
Can obtain values of inner annotation types
jaroslav@378
     1
/**
jaroslav@378
     2
 * Back 2 Browser Bytecode Translator
jaroslav@378
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@378
     4
 *
jaroslav@378
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@378
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@378
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@378
     8
 *
jaroslav@378
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@378
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@378
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@378
    12
 * GNU General Public License for more details.
jaroslav@378
    13
 *
jaroslav@378
    14
 * You should have received a copy of the GNU General Public License
jaroslav@378
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@378
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@235
    17
 */
jaroslav@555
    18
package org.apidesign.bck2brwsr.emul.reflect;
jaroslav@235
    19
jaroslav@235
    20
import java.lang.annotation.Annotation;
jaroslav@652
    21
import java.lang.reflect.Method;
jaroslav@652
    22
import java.lang.reflect.Modifier;
jaroslav@235
    23
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@235
    24
jaroslav@235
    25
/**
jaroslav@235
    26
 *
jaroslav@235
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@235
    28
 */
jaroslav@266
    29
public final class AnnotationImpl implements Annotation {
jaroslav@654
    30
    private final Class<? extends Annotation> type;
jaroslav@654
    31
jaroslav@654
    32
    public AnnotationImpl(Class<? extends Annotation> type) {
jaroslav@654
    33
        this.type = type;
jaroslav@654
    34
    }
jaroslav@654
    35
    
jaroslav@235
    36
    public Class<? extends Annotation> annotationType() {
jaroslav@654
    37
        return type;
jaroslav@235
    38
    }
jaroslav@235
    39
jaroslav@652
    40
    @JavaScriptBody(args = { "a", "n", "arr", "values" }, body = ""
jaroslav@662
    41
        + "function f(v, p, c) {\n"
jaroslav@253
    42
        + "  var val = v;\n"
jaroslav@253
    43
        + "  var prop = p;\n"
jaroslav@662
    44
        + "  var clazz = c;\n"
jaroslav@253
    45
        + "  return function() {\n"
jaroslav@662
    46
        + "    if (clazz == null) return val[prop];\n"
jaroslav@662
    47
        + "    return CLS.prototype.c__Ljava_lang_Object_2Ljava_lang_Class_2Ljava_lang_Object_2(clazz, val[prop]);\n"
jaroslav@253
    48
        + "  };\n"
jaroslav@253
    49
        + "}\n"
jaroslav@662
    50
        + "for (var i = 0; i < arr.length; i += 3) {\n"
jaroslav@652
    51
        + "  var m = arr[i];\n"
jaroslav@652
    52
        + "  var p = arr[i + 1];\n"
jaroslav@662
    53
        + "  var c = arr[i + 2];\n"
jaroslav@662
    54
        + "  a[m] = new f(values, p, c);\n"
jaroslav@253
    55
        + "}\n"
jaroslav@253
    56
        + "a['$instOf_' + n] = true;\n"
jaroslav@235
    57
        + "return a;"
jaroslav@235
    58
    )
jaroslav@652
    59
    private static native <T extends Annotation> T create(
jaroslav@662
    60
        AnnotationImpl a, String n, Object[] methodsAndProps, Object values
jaroslav@652
    61
    );
jaroslav@652
    62
    
jaroslav@662
    63
    private static Object c(Class<? extends Annotation> a, Object v) {
jaroslav@662
    64
        return create(a, v);
jaroslav@662
    65
    }
jaroslav@662
    66
    
jaroslav@266
    67
    public static <T extends Annotation> T create(Class<T> annoClass, Object values) {
jaroslav@654
    68
        return create(new AnnotationImpl(annoClass), 
jaroslav@652
    69
            annoClass.getName().replace('.', '_'), 
jaroslav@652
    70
            findProps(annoClass), values
jaroslav@652
    71
        );
jaroslav@235
    72
    }
jaroslav@238
    73
jaroslav@266
    74
    public static Annotation[] create(Object anno) {
jaroslav@238
    75
        String[] names = findNames(anno);
jaroslav@238
    76
        Annotation[] ret = new Annotation[names.length];
jaroslav@238
    77
        for (int i = 0; i < names.length; i++) {
jaroslav@652
    78
            String annoNameSlash = names[i].substring(1, names[i].length() - 1);
jaroslav@654
    79
            Class<? extends Annotation> annoClass;
jaroslav@652
    80
            try {
jaroslav@654
    81
                annoClass = (Class<? extends Annotation>)Class.forName(annoNameSlash.replace('/', '.'));
jaroslav@652
    82
            } catch (ClassNotFoundException ex) {
jaroslav@652
    83
                throw new IllegalStateException("Can't find annotation class " + annoNameSlash);
jaroslav@652
    84
            }
jaroslav@652
    85
            ret[i] = create(
jaroslav@654
    86
                new AnnotationImpl(annoClass), 
jaroslav@652
    87
                annoNameSlash.replace('/', '_'),
jaroslav@652
    88
                findProps(annoClass),
jaroslav@652
    89
                findData(anno, names[i])
jaroslav@652
    90
            );
jaroslav@238
    91
        }
jaroslav@238
    92
        return ret;
jaroslav@238
    93
    }
jaroslav@238
    94
    @JavaScriptBody(args = "anno", body =
jaroslav@238
    95
          "var arr = new Array();"
jaroslav@254
    96
        + "var props = Object.getOwnPropertyNames(anno);\n"
jaroslav@254
    97
        + "for (var i = 0; i < props.length; i++) {\n"
jaroslav@254
    98
        + "  var p = props[i];\n"
jaroslav@238
    99
        + "  arr.push(p);"
jaroslav@238
   100
        + "}"
jaroslav@238
   101
        + "return arr;"
jaroslav@238
   102
    )
jaroslav@652
   103
    private static native String[] findNames(Object anno);
jaroslav@238
   104
jaroslav@238
   105
    @JavaScriptBody(args={ "anno", "p"}, body="return anno[p];")
jaroslav@652
   106
    private static native Object findData(Object anno, String p);
jaroslav@652
   107
jaroslav@662
   108
    private static Object[] findProps(Class<?> annoClass) {
jaroslav@652
   109
        final Method[] marr = MethodImpl.findMethods(annoClass, Modifier.PUBLIC);
jaroslav@662
   110
        Object[] arr = new Object[marr.length * 3];
jaroslav@652
   111
        int pos = 0;
jaroslav@652
   112
        for (Method m : marr) {
jaroslav@652
   113
            arr[pos++] = MethodImpl.toSignature(m);
jaroslav@652
   114
            arr[pos++] = m.getName();
jaroslav@662
   115
            arr[pos++] = m.getReturnType().isAnnotation() ? m.getReturnType() : null;
jaroslav@652
   116
        }
jaroslav@652
   117
        return arr;
jaroslav@238
   118
    }
jaroslav@235
   119
}