rt/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 25 Jun 2014 22:50:33 +0200
branchdefprop
changeset 1636 eb97a082741b
parent 1256 7b379a47e3a9
child 1637 4156b1bd4b82
permissions -rw-r--r--
Methods on object need to be manually enumerated
     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.io.Serializable;
    22 import java.lang.annotation.Annotation;
    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, subs = {
    34     @ClassesMarker.Anno(Integer.SIZE),
    35     @ClassesMarker.Anno(Integer.MIN_VALUE)
    36 })
    37 @ClassesNamer(name = "my text", anno = @ClassesMarker.Anno(333))
    38 public class Classes {
    39     public static String nameOfIO() {
    40         return nameFor(IOException.class);
    41     }
    42     
    43     private static String nameFor(Class<?> c) {
    44         return c.getName();
    45     }
    46     
    47     private static final Class<?> PRELOAD = Runnable.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         assert cm instanceof Object : "Is object " + cm;
   102         assert cm instanceof Annotation : "Is annotation " + cm;
   103         assert !((Object)cm instanceof Class) : "Is not Class " + cm;
   104         return cm == null ? -1 : cm.number();
   105     }
   106     public static String getMarkerNicknames() {
   107         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
   108         if (cm == null) {
   109             return null;
   110         }
   111         
   112         final Object[] arr = cm.nicknames();
   113         assert arr instanceof Object[] : "Instance of Object array: " + arr;
   114         assert arr instanceof String[] : "Instance of String array: " + arr;
   115         assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
   116         
   117         StringBuilder sb = new StringBuilder();
   118         for (String s : cm.nicknames()) {
   119             sb.append(s).append("\n");
   120         }
   121         return sb.toString().toString();
   122     }
   123 
   124     static String listObject() {
   125         StringBuilder sb = new StringBuilder();
   126         for (Method m : Object.class.getMethods()) {
   127             sb.append(m.getName()).append("\n");
   128         }
   129         return sb.toString().toString();
   130     }
   131     @Retention(RetentionPolicy.CLASS)
   132     @interface Ann {
   133     }
   134     
   135     public static String getRetention() throws Exception {
   136         Retention r = Ann.class.getAnnotation(Retention.class);
   137         assert r != null : "Annotation is present";
   138         assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
   139         return r.annotationType().getName();
   140     }
   141     public static String getMarkerE() {
   142         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
   143         if (cm == null) {
   144             return null;
   145         }
   146         return cm.count().name();
   147     }
   148     public static String getNamer(boolean direct) {
   149         if (direct) {
   150             ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
   151             return cm == null ? null : cm.name();
   152         }
   153         for (Annotation a : Classes.class.getAnnotations()) {
   154             if (a instanceof ClassesNamer) {
   155                 return ((ClassesNamer)a).name();
   156             }
   157         }
   158         return null;
   159     }
   160     public static int getInnerNamer() {
   161         ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
   162         assert cm != null : "ClassesNamer is present";
   163         return cm.anno().value();
   164     }
   165     public static int getInnerNamers() {
   166         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
   167         assert cm != null : "ClassesNamer is present";
   168         int sum = 0;
   169         for (ClassesMarker.Anno anno : cm.subs()) {
   170             sum += anno.value();
   171         }
   172         return sum;
   173     }
   174     
   175     public static String intType() {
   176         return Integer.TYPE.getName();
   177     }
   178     
   179     public static int primitive() {
   180         return 1;
   181     }
   182     public static boolean primitiveB() {
   183         return true;
   184     }
   185     
   186     public static String primitiveType(String method) throws Exception {
   187         return reflectiveMethodCall(false, method).getClass().getName();
   188     }
   189     
   190     @JavaScriptBody(args = "msg", body = "throw msg;")
   191     private static native void thrw(String msg);
   192     
   193     public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
   194         Method find = null;
   195         StringBuilder sb = new StringBuilder();
   196         if (!direct) {
   197             final Class<? extends Annotation> v = ClassesMarker.class;
   198             for (Method m : Classes.class.getMethods()) {
   199                 sb.append("\n").append(m.getName());
   200                 if (mn != null) {
   201                     if (m.getName().equals(mn)) {
   202                         find = m;
   203                         break;
   204                     }
   205                 } else {
   206                     if (m.getAnnotation(v) != null) {
   207                         find = m;
   208                         break;
   209                     }
   210                 }
   211             }
   212         } else {
   213             find = Classes.class.getMethod(mn);
   214         }
   215         if (find == null) {
   216             thrw(sb.toString());
   217             throw new NullPointerException(sb.toString());
   218         }
   219         return find.invoke(null);
   220     }
   221     
   222     public static int reflectiveSum(int a, int b) throws Exception {
   223         Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
   224         return (int) m.invoke(null, a, b);
   225     }
   226     
   227     private abstract class Application {
   228         public abstract int getID();
   229     }
   230 
   231     private class MyApplication extends Application {
   232         @Override
   233         public int getID() {
   234             return 1;
   235         }
   236     }
   237 
   238     public static boolean isClassAssignable() {
   239         return Application.class.isAssignableFrom(MyApplication.class);
   240     }
   241     
   242     public static String valueEnum(String v) {
   243         return ClassesMarker.E.valueOf(v).toString();
   244     }
   245     
   246     public static String typeOfFn() {
   247         return fn().getClass().getName();
   248     }
   249     
   250     @JavaScriptBody(args = {  }, body = "return function() { alert('x'); };")
   251     private native static Object fn();
   252     
   253     public static boolean instanceOfSuperInterface() {
   254         Object obj = new SuperSerial() {
   255         };
   256         return obj instanceof Serializable;
   257     }
   258     
   259     private static interface SuperSerial extends Serializable {
   260     }
   261 }