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