jaroslav@323: /** jaroslav@323: * Back 2 Browser Bytecode Translator jaroslav@323: * Copyright (C) 2012 Jaroslav Tulach jaroslav@323: * jaroslav@323: * This program is free software: you can redistribute it and/or modify jaroslav@323: * it under the terms of the GNU General Public License as published by jaroslav@323: * the Free Software Foundation, version 2 of the License. jaroslav@323: * jaroslav@323: * This program is distributed in the hope that it will be useful, jaroslav@323: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@323: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@323: * GNU General Public License for more details. jaroslav@323: * jaroslav@323: * You should have received a copy of the GNU General Public License jaroslav@323: * along with this program. Look for COPYING file in the top folder. jaroslav@323: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@323: */ jaroslav@323: package org.apidesign.bck2brwsr.launcher; jaroslav@323: jaroslav@332: import java.io.InputStream; jaroslav@332: import java.lang.reflect.InvocationTargetException; jaroslav@323: import java.lang.reflect.Method; jaroslav@332: import java.net.URL; jaroslav@323: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@323: jaroslav@323: /** jaroslav@323: * jaroslav@323: * @author Jaroslav Tulach jaroslav@323: */ jaroslav@323: public class Console { jaroslav@323: public static String welcome() { jaroslav@323: final String msg = "Hello from Bck2Brwsr!"; jaroslav@323: alert(msg); jaroslav@323: return msg; jaroslav@323: } jaroslav@323: jaroslav@323: @JavaScriptBody(args = "msg", body = "alert(msg);") jaroslav@323: private static native void alert(String msg); jaroslav@323: jaroslav@323: @JavaScriptBody(args = {"id", "attr"}, body = jaroslav@323: "return window.document.getElementById(id)[attr].toString();") jaroslav@323: private static native Object getAttr(String id, String attr); jaroslav@323: jaroslav@323: @JavaScriptBody(args = {"id", "attr", "value"}, body = jaroslav@323: "window.document.getElementById(id)[attr] = value;") jaroslav@323: private static native void setAttr(String id, String attr, Object value); jaroslav@323: jaroslav@323: public static void execute() throws Exception { jaroslav@323: String clazz = (String) getAttr("clazz", "value"); jaroslav@323: String method = (String) getAttr("method", "value"); jaroslav@332: Object res = invokeMethod(clazz, method); jaroslav@332: setAttr("result", "value", res); jaroslav@332: } jaroslav@332: jaroslav@332: public static void harness() { jaroslav@332: try { jaroslav@332: URL u = new URL("/execute/data"); jaroslav@332: String data = (String) u.getContent(new Class[] { String.class }); jaroslav@332: setAttr("result", "value", data); jaroslav@332: } catch (Exception ex) { jaroslav@332: setAttr("result", "value", ex.getMessage()); jaroslav@332: } jaroslav@332: } jaroslav@323: jaroslav@332: private static Object invokeMethod(String clazz, String method) jaroslav@332: throws ClassNotFoundException, InvocationTargetException, jaroslav@332: SecurityException, IllegalAccessException, IllegalArgumentException { jaroslav@323: Method found = null; jaroslav@323: Class c = Class.forName(clazz); jaroslav@323: for (Method m : c.getMethods()) { jaroslav@323: if (m.getName().equals(method)) { jaroslav@323: found = m; jaroslav@323: } jaroslav@323: } jaroslav@323: Object res; jaroslav@323: if (found != null) { jaroslav@323: res = found.invoke(null); jaroslav@323: } else { jaroslav@323: res = "Can't find method " + method + " in " + clazz; jaroslav@323: } jaroslav@332: return res; jaroslav@323: } jaroslav@323: }