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