1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Thu Feb 14 12:06:16 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,154 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.vm4brwsr;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.lang.annotation.Annotation;
1.25 -import java.lang.reflect.Method;
1.26 -import java.net.MalformedURLException;
1.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.28 -
1.29 -/**
1.30 - *
1.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.32 - */
1.33 -@ClassesMarker(number = 10)
1.34 -@ClassesNamer(name = "my text")
1.35 -public class Classes {
1.36 - public static String nameOfIO() {
1.37 - return nameFor(IOException.class);
1.38 - }
1.39 -
1.40 - private static String nameFor(Class<?> c) {
1.41 - return c.getName();
1.42 - }
1.43 -
1.44 - public static boolean isInterface(String s) throws ClassNotFoundException {
1.45 - return Class.forName(s).isInterface();
1.46 - }
1.47 -
1.48 - public static boolean equalsClassesOfExceptions() {
1.49 - return MalformedURLException.class.getSuperclass() == IOException.class;
1.50 - }
1.51 - public static boolean differenceInClasses() {
1.52 - Class<?> c1 = MalformedURLException.class;
1.53 - Class<?> c2 = IOException.class;
1.54 - return c1 != c2;
1.55 - }
1.56 -
1.57 - public static String classForInstance() {
1.58 - return new IOException().getClass().getName().toString();
1.59 - }
1.60 -
1.61 - @ClassesMarker(number = 1)
1.62 - public static String name() {
1.63 - return IOException.class.getName().toString();
1.64 - }
1.65 - public static String simpleName() {
1.66 - return IOException.class.getSimpleName();
1.67 - }
1.68 - public static String canonicalName() {
1.69 - return IOException.class.getCanonicalName();
1.70 - }
1.71 - public static boolean newInstance() throws Exception {
1.72 - IOException ioe = IOException.class.newInstance();
1.73 - if (ioe instanceof IOException) {
1.74 - return ioe.getClass() == IOException.class;
1.75 - }
1.76 - throw new IllegalStateException("Not a subtype: " + ioe);
1.77 - }
1.78 - public static String newInstanceNoPubConstructor() throws Exception {
1.79 - try {
1.80 - Float f = Float.class.newInstance();
1.81 - return "wrong, can't instantiate: " + f;
1.82 - } catch (Exception ex) {
1.83 - return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
1.84 - }
1.85 - }
1.86 - public static int getMarker() {
1.87 - if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
1.88 - return -2;
1.89 - }
1.90 - ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
1.91 - return cm == null ? -1 : cm.number();
1.92 - }
1.93 - public static String getNamer(boolean direct) {
1.94 - if (direct) {
1.95 - ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
1.96 - return cm == null ? null : cm.name();
1.97 - }
1.98 - for (Annotation a : Classes.class.getAnnotations()) {
1.99 - if (a instanceof ClassesNamer) {
1.100 - return ((ClassesNamer)a).name();
1.101 - }
1.102 - }
1.103 - return null;
1.104 - }
1.105 -
1.106 - public static String intType() {
1.107 - return Integer.TYPE.getName();
1.108 - }
1.109 -
1.110 - public static int primitive() {
1.111 - return 1;
1.112 - }
1.113 - public static boolean primitiveB() {
1.114 - return true;
1.115 - }
1.116 -
1.117 - public static String primitiveType(String method) throws Exception {
1.118 - return reflectiveMethodCall(false, method).getClass().getName();
1.119 - }
1.120 -
1.121 - @JavaScriptBody(args = "msg", body = "throw msg;")
1.122 - private static native void thrw(String msg);
1.123 -
1.124 - public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
1.125 - Method find = null;
1.126 - StringBuilder sb = new StringBuilder();
1.127 - if (!direct) {
1.128 - final Class<? extends Annotation> v = ClassesMarker.class;
1.129 - for (Method m : Classes.class.getMethods()) {
1.130 - sb.append("\n").append(m.getName());
1.131 - if (mn != null) {
1.132 - if (m.getName().equals(mn)) {
1.133 - find = m;
1.134 - break;
1.135 - }
1.136 - } else {
1.137 - if (m.getAnnotation(v) != null) {
1.138 - find = m;
1.139 - break;
1.140 - }
1.141 - }
1.142 - }
1.143 - } else {
1.144 - find = Classes.class.getMethod(mn);
1.145 - }
1.146 - if (find == null) {
1.147 - thrw(sb.toString());
1.148 - throw new NullPointerException(sb.toString());
1.149 - }
1.150 - return find.invoke(null);
1.151 - }
1.152 -
1.153 - public static int reflectiveSum(int a, int b) throws Exception {
1.154 - Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
1.155 - return (int) m.invoke(null, a, b);
1.156 - }
1.157 -}