vm/src/test/java/org/apidesign/vm4brwsr/Classes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 03 Feb 2013 18:58:09 +0100
branchreflection
changeset 652 f095ea52f417
parent 649 4b16b7e23cab
child 653 bcdfc29fd004
permissions -rw-r--r--
Supporting annotations with arrays
     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.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.lang.annotation.Annotation;
    22 import java.lang.reflect.Method;
    23 import java.net.MalformedURLException;
    24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 @ClassesMarker(number = 10, nicknames = { "Ten", "Deset" })
    31 @ClassesNamer(name = "my text")
    32 public class Classes {
    33     public static String nameOfIO() {
    34         return nameFor(IOException.class);
    35     }
    36     
    37     private static String nameFor(Class<?> c) {
    38         return c.getName();
    39     }
    40     
    41     private static final Class<?> PRELOAD = Runnable.class;
    42     private static final Class<?> PRELOAD2 = ClassesMarker.E.class;
    43     
    44     public static boolean isInterface(String s) throws ClassNotFoundException {
    45         return Class.forName(s).isInterface();
    46     }
    47     
    48     public static boolean equalsClassesOfExceptions() {
    49         return MalformedURLException.class.getSuperclass() == IOException.class;
    50     }
    51     public static boolean differenceInClasses() {
    52         Class<?> c1 = MalformedURLException.class;
    53         Class<?> c2 = IOException.class;
    54         return c1 != c2;
    55     }
    56     
    57     public static String classForInstance() {
    58         return new IOException().getClass().getName().toString();
    59     }
    60     
    61     @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
    62     public static String name() {
    63         return IOException.class.getName().toString();
    64     }
    65     public static String simpleName() {
    66         return IOException.class.getSimpleName();
    67     }
    68     public static String canonicalName() {
    69         return IOException.class.getCanonicalName();
    70     }
    71     public static boolean newInstance() throws Exception {
    72         IOException ioe = IOException.class.newInstance();
    73         if (ioe instanceof IOException) {
    74             return ioe.getClass() == IOException.class;
    75         }
    76         throw new IllegalStateException("Not a subtype: " + ioe);
    77     }
    78     public static String newInstanceNoPubConstructor() throws Exception {
    79         try {
    80             Float f = Float.class.newInstance();
    81             return "wrong, can't instantiate: " + f;
    82         } catch (Exception ex) {
    83             return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
    84         }
    85     }
    86     public static int getMarker() {
    87         if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
    88             return -2;
    89         }
    90         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
    91         return cm == null ? -1 : cm.number();
    92     }
    93     public static String getMarkerNicknames() {
    94         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
    95         if (cm == null) {
    96             return null;
    97         }
    98         StringBuilder sb = new StringBuilder();
    99         for (String s : cm.nicknames()) {
   100             sb.append(s).append("\n");
   101         }
   102         return sb.toString().toString();
   103     }
   104     public static String getNamer(boolean direct) {
   105         if (direct) {
   106             ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
   107             return cm == null ? null : cm.name();
   108         }
   109         for (Annotation a : Classes.class.getAnnotations()) {
   110             if (a instanceof ClassesNamer) {
   111                 return ((ClassesNamer)a).name();
   112             }
   113         }
   114         return null;
   115     }
   116     
   117     public static String intType() {
   118         return Integer.TYPE.getName();
   119     }
   120     
   121     public static int primitive() {
   122         return 1;
   123     }
   124     public static boolean primitiveB() {
   125         return true;
   126     }
   127     
   128     public static String primitiveType(String method) throws Exception {
   129         return reflectiveMethodCall(false, method).getClass().getName();
   130     }
   131     
   132     @JavaScriptBody(args = "msg", body = "throw msg;")
   133     private static native void thrw(String msg);
   134     
   135     public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
   136         Method find = null;
   137         StringBuilder sb = new StringBuilder();
   138         if (!direct) {
   139             final Class<? extends Annotation> v = ClassesMarker.class;
   140             for (Method m : Classes.class.getMethods()) {
   141                 sb.append("\n").append(m.getName());
   142                 if (mn != null) {
   143                     if (m.getName().equals(mn)) {
   144                         find = m;
   145                         break;
   146                     }
   147                 } else {
   148                     if (m.getAnnotation(v) != null) {
   149                         find = m;
   150                         break;
   151                     }
   152                 }
   153             }
   154         } else {
   155             find = Classes.class.getMethod(mn);
   156         }
   157         if (find == null) {
   158             thrw(sb.toString());
   159             throw new NullPointerException(sb.toString());
   160         }
   161         return find.invoke(null);
   162     }
   163     
   164     public static int reflectiveSum(int a, int b) throws Exception {
   165         Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
   166         return (int) m.invoke(null, a, b);
   167     }
   168 }