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