1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,233 @@
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.annotation.Retention;
1.26 +import java.lang.annotation.RetentionPolicy;
1.27 +import java.lang.reflect.Method;
1.28 +import java.net.MalformedURLException;
1.29 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.30 +
1.31 +/**
1.32 + *
1.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.34 + */
1.35 +@ClassesMarker(number = 10, nicknames = { "Ten", "Deset" }, count = ClassesMarker.E.TWO, subs = {
1.36 + @ClassesMarker.Anno(Integer.SIZE),
1.37 + @ClassesMarker.Anno(Integer.MIN_VALUE)
1.38 +})
1.39 +@ClassesNamer(name = "my text", anno = @ClassesMarker.Anno(333))
1.40 +public class Classes {
1.41 + public static String nameOfIO() {
1.42 + return nameFor(IOException.class);
1.43 + }
1.44 +
1.45 + private static String nameFor(Class<?> c) {
1.46 + return c.getName();
1.47 + }
1.48 +
1.49 + private static final Class<?> PRELOAD = Runnable.class;
1.50 +
1.51 + public static boolean isInterface(String s) throws ClassNotFoundException {
1.52 + return Class.forName(s).isInterface();
1.53 + }
1.54 +
1.55 + public static boolean equalsClassesOfExceptions() {
1.56 + return MalformedURLException.class.getSuperclass() == IOException.class;
1.57 + }
1.58 + public static boolean differenceInClasses() {
1.59 + Class<?> c1 = MalformedURLException.class;
1.60 + Class<?> c2 = IOException.class;
1.61 + return c1 != c2;
1.62 + }
1.63 +
1.64 + public static String classForInstance() {
1.65 + return new IOException().getClass().getName().toString();
1.66 + }
1.67 +
1.68 + @ClassesMarker(number = 1, nicknames = { "One", "Jedna" } )
1.69 + public static String name() {
1.70 + return IOException.class.getName().toString();
1.71 + }
1.72 + public static String simpleName() {
1.73 + return IOException.class.getSimpleName();
1.74 + }
1.75 + public static String canonicalName() {
1.76 + return IOException.class.getCanonicalName();
1.77 + }
1.78 +
1.79 + public static String objectName() throws NoSuchMethodException {
1.80 + return IOException.class.getMethod("wait").getDeclaringClass().getName();
1.81 + }
1.82 +
1.83 + public static boolean newInstance() throws Exception {
1.84 + IOException ioe = IOException.class.newInstance();
1.85 + if (ioe instanceof IOException) {
1.86 + return ioe.getClass() == IOException.class;
1.87 + }
1.88 + throw new IllegalStateException("Not a subtype: " + ioe);
1.89 + }
1.90 + public static String newInstanceNoPubConstructor() throws Exception {
1.91 + try {
1.92 + Float f = Float.class.newInstance();
1.93 + return "wrong, can't instantiate: " + f;
1.94 + } catch (Exception ex) {
1.95 + return (ex.getClass().getName() + ":" + ex.getMessage()).toString().toString();
1.96 + }
1.97 + }
1.98 + public static int getMarker() {
1.99 + if (!Classes.class.isAnnotationPresent(ClassesMarker.class)) {
1.100 + return -2;
1.101 + }
1.102 + ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
1.103 + assert cm instanceof Object : "Is object " + cm;
1.104 + assert cm instanceof Annotation : "Is annotation " + cm;
1.105 + assert !((Object)cm instanceof Class) : "Is not Class " + cm;
1.106 + return cm == null ? -1 : cm.number();
1.107 + }
1.108 + public static String getMarkerNicknames() {
1.109 + ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
1.110 + if (cm == null) {
1.111 + return null;
1.112 + }
1.113 +
1.114 + final Object[] arr = cm.nicknames();
1.115 + assert arr instanceof Object[] : "Instance of Object array: " + arr;
1.116 + assert arr instanceof String[] : "Instance of String array: " + arr;
1.117 + assert !(arr instanceof Integer[]) : "Not instance of Integer array: " + arr;
1.118 +
1.119 + StringBuilder sb = new StringBuilder();
1.120 + for (String s : cm.nicknames()) {
1.121 + sb.append(s).append("\n");
1.122 + }
1.123 + return sb.toString().toString();
1.124 + }
1.125 + @Retention(RetentionPolicy.CLASS)
1.126 + @interface Ann {
1.127 + }
1.128 +
1.129 + public static String getRetention() throws Exception {
1.130 + Retention r = Ann.class.getAnnotation(Retention.class);
1.131 + assert r != null : "Annotation is present";
1.132 + assert r.value() == RetentionPolicy.CLASS : "Policy value is OK: " + r.value();
1.133 + return r.annotationType().getName();
1.134 + }
1.135 + public static String getMarkerE() {
1.136 + ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
1.137 + if (cm == null) {
1.138 + return null;
1.139 + }
1.140 + return cm.count().name();
1.141 + }
1.142 + public static String getNamer(boolean direct) {
1.143 + if (direct) {
1.144 + ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
1.145 + return cm == null ? null : cm.name();
1.146 + }
1.147 + for (Annotation a : Classes.class.getAnnotations()) {
1.148 + if (a instanceof ClassesNamer) {
1.149 + return ((ClassesNamer)a).name();
1.150 + }
1.151 + }
1.152 + return null;
1.153 + }
1.154 + public static int getInnerNamer() {
1.155 + ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
1.156 + assert cm != null : "ClassesNamer is present";
1.157 + return cm.anno().value();
1.158 + }
1.159 + public static int getInnerNamers() {
1.160 + ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
1.161 + assert cm != null : "ClassesNamer is present";
1.162 + int sum = 0;
1.163 + for (ClassesMarker.Anno anno : cm.subs()) {
1.164 + sum += anno.value();
1.165 + }
1.166 + return sum;
1.167 + }
1.168 +
1.169 + public static String intType() {
1.170 + return Integer.TYPE.getName();
1.171 + }
1.172 +
1.173 + public static int primitive() {
1.174 + return 1;
1.175 + }
1.176 + public static boolean primitiveB() {
1.177 + return true;
1.178 + }
1.179 +
1.180 + public static String primitiveType(String method) throws Exception {
1.181 + return reflectiveMethodCall(false, method).getClass().getName();
1.182 + }
1.183 +
1.184 + @JavaScriptBody(args = "msg", body = "throw msg;")
1.185 + private static native void thrw(String msg);
1.186 +
1.187 + public static Object reflectiveMethodCall(boolean direct, String mn) throws Exception {
1.188 + Method find = null;
1.189 + StringBuilder sb = new StringBuilder();
1.190 + if (!direct) {
1.191 + final Class<? extends Annotation> v = ClassesMarker.class;
1.192 + for (Method m : Classes.class.getMethods()) {
1.193 + sb.append("\n").append(m.getName());
1.194 + if (mn != null) {
1.195 + if (m.getName().equals(mn)) {
1.196 + find = m;
1.197 + break;
1.198 + }
1.199 + } else {
1.200 + if (m.getAnnotation(v) != null) {
1.201 + find = m;
1.202 + break;
1.203 + }
1.204 + }
1.205 + }
1.206 + } else {
1.207 + find = Classes.class.getMethod(mn);
1.208 + }
1.209 + if (find == null) {
1.210 + thrw(sb.toString());
1.211 + throw new NullPointerException(sb.toString());
1.212 + }
1.213 + return find.invoke(null);
1.214 + }
1.215 +
1.216 + public static int reflectiveSum(int a, int b) throws Exception {
1.217 + Method m = StaticMethod.class.getMethod("sum", int.class, int.class);
1.218 + return (int) m.invoke(null, a, b);
1.219 + }
1.220 +
1.221 + private abstract class Application {
1.222 + public abstract int getID();
1.223 + }
1.224 +
1.225 + private class MyApplication extends Application {
1.226 + @Override
1.227 + public int getID() {
1.228 + return 1;
1.229 + }
1.230 + }
1.231 +
1.232 + public static boolean isClassAssignable() {
1.233 + return Application.class.isAssignableFrom(MyApplication.class);
1.234 + }
1.235 +
1.236 +}