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