vm/src/test/java/org/apidesign/vm4brwsr/Classes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 03 Feb 2013 19:39:34 +0100
branchreflection
changeset 653 bcdfc29fd004
parent 652 f095ea52f417
child 654 26a86cc00224
permissions -rw-r--r--
Some annotation types work
jaroslav@222
     1
/**
jaroslav@222
     2
 * Back 2 Browser Bytecode Translator
jaroslav@222
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@222
     4
 *
jaroslav@222
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@222
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@222
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@222
     8
 *
jaroslav@222
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@222
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@222
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@222
    12
 * GNU General Public License for more details.
jaroslav@222
    13
 *
jaroslav@222
    14
 * You should have received a copy of the GNU General Public License
jaroslav@222
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@222
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@222
    17
 */
jaroslav@222
    18
package org.apidesign.vm4brwsr;
jaroslav@222
    19
jaroslav@222
    20
import java.io.IOException;
jaroslav@237
    21
import java.lang.annotation.Annotation;
jaroslav@653
    22
import java.lang.annotation.RetentionPolicy;
jaroslav@261
    23
import java.lang.reflect.Method;
jaroslav@222
    24
import java.net.MalformedURLException;
jaroslav@264
    25
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@222
    26
jaroslav@222
    27
/**
jaroslav@222
    28
 *
jaroslav@222
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@222
    30
 */
jaroslav@653
    31
@ClassesMarker(number = 10, nicknames = { "Ten", "Deset" }, count = ClassesMarker.E.TWO)
jaroslav@237
    32
@ClassesNamer(name = "my text")
jaroslav@222
    33
public class Classes {
jaroslav@303
    34
    public static String nameOfIO() {
jaroslav@303
    35
        return nameFor(IOException.class);
jaroslav@303
    36
    }
jaroslav@303
    37
    
jaroslav@303
    38
    private static String nameFor(Class<?> c) {
jaroslav@303
    39
        return c.getName();
jaroslav@303
    40
    }
jaroslav@303
    41
    
jaroslav@649
    42
    private static final Class<?> PRELOAD = Runnable.class;
jaroslav@652
    43
    private static final Class<?> PRELOAD2 = ClassesMarker.E.class;
jaroslav@653
    44
    private static final Class<?> PRELOAD3 = RetentionPolicy.class;
jaroslav@649
    45
    
jaroslav@355
    46
    public static boolean isInterface(String s) throws ClassNotFoundException {
jaroslav@355
    47
        return Class.forName(s).isInterface();
jaroslav@355
    48
    }
jaroslav@355
    49
    
jaroslav@222
    50
    public static boolean equalsClassesOfExceptions() {
jaroslav@222
    51
        return MalformedURLException.class.getSuperclass() == IOException.class;
jaroslav@222
    52
    }
jaroslav@222
    53
    public static boolean differenceInClasses() {
jaroslav@222
    54
        Class<?> c1 = MalformedURLException.class;
jaroslav@222
    55
        Class<?> c2 = IOException.class;
jaroslav@222
    56
        return c1 != c2;
jaroslav@222
    57
    }
jaroslav@222
    58
    
jaroslav@225
    59
    public static String classForInstance() {
jaroslav@225
    60
        return new IOException().getClass().getName().toString();
jaroslav@225
    61
    }
jaroslav@225
    62
    
jaroslav@652
    63
    @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
jaroslav@222
    64
    public static String name() {
jaroslav@223
    65
        return IOException.class.getName().toString();
jaroslav@222
    66
    }
jaroslav@222
    67
    public static String simpleName() {
jaroslav@222
    68
        return IOException.class.getSimpleName();
jaroslav@222
    69
    }
jaroslav@222
    70
    public static String canonicalName() {
jaroslav@222
    71
        return IOException.class.getCanonicalName();
jaroslav@222
    72
    }
jaroslav@231
    73
    public static boolean newInstance() throws Exception {
jaroslav@231
    74
        IOException ioe = IOException.class.newInstance();
jaroslav@231
    75
        if (ioe instanceof IOException) {
jaroslav@231
    76
            return ioe.getClass() == IOException.class;
jaroslav@231
    77
        }
jaroslav@231
    78
        throw new IllegalStateException("Not a subtype: " + ioe);
jaroslav@222
    79
    }
jaroslav@397
    80
    public static String newInstanceNoPubConstructor() throws Exception {
jaroslav@397
    81
        try {
jaroslav@397
    82
            Float f = Float.class.newInstance();
jaroslav@397
    83
            return "wrong, can't instantiate: " + f;
jaroslav@397
    84
        } catch (Exception ex) {
jaroslav@397
    85
            return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
jaroslav@397
    86
        }
jaroslav@397
    87
    }
jaroslav@235
    88
    public static int getMarker() {
jaroslav@235
    89
        if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
jaroslav@235
    90
            return -2;
jaroslav@235
    91
        }
jaroslav@235
    92
        ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
jaroslav@235
    93
        return cm == null ? -1 : cm.number();
jaroslav@235
    94
    }
jaroslav@652
    95
    public static String getMarkerNicknames() {
jaroslav@652
    96
        ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
jaroslav@652
    97
        if (cm == null) {
jaroslav@652
    98
            return null;
jaroslav@652
    99
        }
jaroslav@652
   100
        StringBuilder sb = new StringBuilder();
jaroslav@652
   101
        for (String s : cm.nicknames()) {
jaroslav@652
   102
            sb.append(s).append("\n");
jaroslav@652
   103
        }
jaroslav@652
   104
        return sb.toString().toString();
jaroslav@652
   105
    }
jaroslav@653
   106
    public static String getMarkerE() {
jaroslav@653
   107
        ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
jaroslav@653
   108
        if (cm == null) {
jaroslav@653
   109
            return null;
jaroslav@653
   110
        }
jaroslav@653
   111
        return cm.count().name();
jaroslav@653
   112
    }
jaroslav@237
   113
    public static String getNamer(boolean direct) {
jaroslav@237
   114
        if (direct) {
jaroslav@237
   115
            ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
jaroslav@237
   116
            return cm == null ? null : cm.name();
jaroslav@237
   117
        }
jaroslav@237
   118
        for (Annotation a : Classes.class.getAnnotations()) {
jaroslav@237
   119
            if (a instanceof ClassesNamer) {
jaroslav@237
   120
                return ((ClassesNamer)a).name();
jaroslav@237
   121
            }
jaroslav@237
   122
        }
jaroslav@237
   123
        return null;
jaroslav@237
   124
    }
jaroslav@261
   125
    
jaroslav@353
   126
    public static String intType() {
jaroslav@353
   127
        return Integer.TYPE.getName();
jaroslav@353
   128
    }
jaroslav@353
   129
    
jaroslav@354
   130
    public static int primitive() {
jaroslav@354
   131
        return 1;
jaroslav@354
   132
    }
jaroslav@355
   133
    public static boolean primitiveB() {
jaroslav@355
   134
        return true;
jaroslav@355
   135
    }
jaroslav@354
   136
    
jaroslav@355
   137
    public static String primitiveType(String method) throws Exception {
jaroslav@355
   138
        return reflectiveMethodCall(false, method).getClass().getName();
jaroslav@354
   139
    }
jaroslav@354
   140
    
jaroslav@264
   141
    @JavaScriptBody(args = "msg", body = "throw msg;")
jaroslav@264
   142
    private static native void thrw(String msg);
jaroslav@264
   143
    
jaroslav@265
   144
    public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
jaroslav@264
   145
        Method find = null;
jaroslav@264
   146
        StringBuilder sb = new StringBuilder();
jaroslav@261
   147
        if (!direct) {
jaroslav@261
   148
            final Class<? extends Annotation> v = ClassesMarker.class;
jaroslav@264
   149
            for (Method m : Classes.class.getMethods()) {
jaroslav@264
   150
                sb.append("\n").append(m.getName());
jaroslav@265
   151
                if (mn != null) {
jaroslav@265
   152
                    if (m.getName().equals(mn)) {
jaroslav@265
   153
                        find = m;
jaroslav@265
   154
                        break;
jaroslav@265
   155
                    }
jaroslav@265
   156
                } else {
jaroslav@265
   157
                    if (m.getAnnotation(v) != null) {
jaroslav@265
   158
                        find = m;
jaroslav@265
   159
                        break;
jaroslav@265
   160
                    }
jaroslav@261
   161
                }
jaroslav@261
   162
            }
jaroslav@264
   163
        } else {
jaroslav@265
   164
            find = Classes.class.getMethod(mn);
jaroslav@261
   165
        }
jaroslav@264
   166
        if (find == null) {
jaroslav@264
   167
            thrw(sb.toString());
jaroslav@264
   168
            throw new NullPointerException(sb.toString());
jaroslav@264
   169
        }
jaroslav@264
   170
        return find.invoke(null);
jaroslav@261
   171
    }
jaroslav@418
   172
    
jaroslav@418
   173
    public static int reflectiveSum(int a, int b) throws Exception {
jaroslav@418
   174
        Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
jaroslav@418
   175
        return (int) m.invoke(null, a, b);
jaroslav@418
   176
    }
jaroslav@222
   177
}