rt/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 05 Sep 2013 09:36:06 +0200
changeset 1256 7b379a47e3a9
parent 1251 65be08df3969
child 1636 eb97a082741b
permissions -rw-r--r--
Need to make sure an object implements all transitive interfaces, not just those specified directly
     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     @Retention(RetentionPolicy.CLASS)
   124     @interface Ann {
   125     }
   126     
   127     public static String getRetention() throws Exception {
   128         Retention r = Ann.class.getAnnotation(Retention.class);
   129         assert r != null : "Annotation is present";
   130         assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
   131         return r.annotationType().getName();
   132     }
   133     public static String getMarkerE() {
   134         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
   135         if (cm == null) {
   136             return null;
   137         }
   138         return cm.count().name();
   139     }
   140     public static String getNamer(boolean direct) {
   141         if (direct) {
   142             ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
   143             return cm == null ? null : cm.name();
   144         }
   145         for (Annotation a : Classes.class.getAnnotations()) {
   146             if (a instanceof ClassesNamer) {
   147                 return ((ClassesNamer)a).name();
   148             }
   149         }
   150         return null;
   151     }
   152     public static int getInnerNamer() {
   153         ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
   154         assert cm != null : "ClassesNamer is present";
   155         return cm.anno().value();
   156     }
   157     public static int getInnerNamers() {
   158         ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
   159         assert cm != null : "ClassesNamer is present";
   160         int sum = 0;
   161         for (ClassesMarker.Anno anno : cm.subs()) {
   162             sum += anno.value();
   163         }
   164         return sum;
   165     }
   166     
   167     public static String intType() {
   168         return Integer.TYPE.getName();
   169     }
   170     
   171     public static int primitive() {
   172         return 1;
   173     }
   174     public static boolean primitiveB() {
   175         return true;
   176     }
   177     
   178     public static String primitiveType(String method) throws Exception {
   179         return reflectiveMethodCall(false, method).getClass().getName();
   180     }
   181     
   182     @JavaScriptBody(args = "msg", body = "throw msg;")
   183     private static native void thrw(String msg);
   184     
   185     public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
   186         Method find = null;
   187         StringBuilder sb = new StringBuilder();
   188         if (!direct) {
   189             final Class<? extends Annotation> v = ClassesMarker.class;
   190             for (Method m : Classes.class.getMethods()) {
   191                 sb.append("\n").append(m.getName());
   192                 if (mn != null) {
   193                     if (m.getName().equals(mn)) {
   194                         find = m;
   195                         break;
   196                     }
   197                 } else {
   198                     if (m.getAnnotation(v) != null) {
   199                         find = m;
   200                         break;
   201                     }
   202                 }
   203             }
   204         } else {
   205             find = Classes.class.getMethod(mn);
   206         }
   207         if (find == null) {
   208             thrw(sb.toString());
   209             throw new NullPointerException(sb.toString());
   210         }
   211         return find.invoke(null);
   212     }
   213     
   214     public static int reflectiveSum(int a, int b) throws Exception {
   215         Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
   216         return (int) m.invoke(null, a, b);
   217     }
   218     
   219     private abstract class Application {
   220         public abstract int getID();
   221     }
   222 
   223     private class MyApplication extends Application {
   224         @Override
   225         public int getID() {
   226             return 1;
   227         }
   228     }
   229 
   230     public static boolean isClassAssignable() {
   231         return Application.class.isAssignableFrom(MyApplication.class);
   232     }
   233     
   234     public static String valueEnum(String v) {
   235         return ClassesMarker.E.valueOf(v).toString();
   236     }
   237     
   238     public static String typeOfFn() {
   239         return fn().getClass().getName();
   240     }
   241     
   242     @JavaScriptBody(args = {  }, body = "return function() { alert('x'); };")
   243     private native static Object fn();
   244     
   245     public static boolean instanceOfSuperInterface() {
   246         Object obj = new SuperSerial() {
   247         };
   248         return obj instanceof Serializable;
   249     }
   250     
   251     private static interface SuperSerial extends Serializable {
   252     }
   253 }