# HG changeset patch # User Jaroslav Tulach # Date 1458900736 -3600 # Node ID 4f4554f69892b3df093f85b24367fe606b43805b # Parent f5be41227c81766ad939561a36d9e1d03208b543 Invoke methods with string parameters diff -r f5be41227c81 -r 4f4554f69892 launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Wed Mar 23 06:07:49 2016 +0100 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri Mar 25 11:12:16 2016 +0100 @@ -36,6 +36,7 @@ private String result; private Throwable exception; String html; + String[] args; final List resources = new ArrayList<>(); private int time; @@ -52,6 +53,14 @@ public void setHtmlFragment(String html) { this.html = html; } + + /** Arguments to pass to the invoked method. + * @param args textual arguments to pass to the method + * @since 0.18 + */ + public void setArguments(String... args) { + this.args = args; + } /** HTTP resource to be available during execution. An invocation may * perform an HTTP query and obtain a resource relative to the page. diff -r f5be41227c81 -r 4f4554f69892 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Wed Mar 23 06:07:49 2016 +0100 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri Mar 25 11:12:16 2016 +0100 @@ -377,8 +377,9 @@ mi = methods.take(); caseNmbr = cnt++; } + final Writer w = response.getWriter(); if (mi == END) { - response.getWriter().write(""); + w.write(""); wait.countDown(); cnt = 0; LOG.log(Level.INFO, "End of data reached. Exiting."); @@ -395,17 +396,29 @@ final String cn = mi.clazz.getName(); final String mn = mi.methodName; LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn}); - response.getWriter().write("{" + w.write("{" + "className: '" + cn + "', " + "methodName: '" + mn + "', " + "request: " + caseNmbr ); + if (mi.args != null) { + w.write(", args: ["); + String sep = ""; + for (String a : mi.args) { + w.write(sep); + w.write("'"); + w.write(a); + w.write("'"); + sep = ", "; + } + w.write("]"); + } if (mi.html != null) { - response.getWriter().write(", html: '"); - response.getWriter().write(encodeJSON(mi.html)); - response.getWriter().write("'"); + w.write(", html: '"); + w.write(encodeJSON(mi.html)); + w.write("'"); } - response.getWriter().write("}"); + w.write("}"); } }, "/data"); diff -r f5be41227c81 -r 4f4554f69892 launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Wed Mar 23 06:07:49 2016 +0100 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Fri Mar 25 11:12:16 2016 +0100 @@ -223,7 +223,7 @@ static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, InterruptedException { - final Object r = new Case(null).invokeMethod(clazz, method); + final Object r = new Case(null).invokeMethod(clazz, method, null); return r == null ? "null" : r.toString().toString(); } @@ -315,6 +315,10 @@ public String getHtmlFragment() { return value("html", data); } + + public String[] getArgs() { + return values("args", data); + } void again(Object[] arr) { try { @@ -334,21 +338,24 @@ setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment()); } log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId()); - Object result = invokeMethod(this.getClassName(), this.getMethodName()); + Object result = invokeMethod(this.getClassName(), this.getMethodName(), this.getArgs()); setAttr("bck2brwsr.fragment", "innerHTML", ""); log("Result: " + result); result = encodeURL("" + result); return result; } - private Object invokeMethod(String clazz, String method) + private Object invokeMethod(String clazz, String method, String[] args) throws ClassNotFoundException, InvocationTargetException, InterruptedException, IllegalAccessException, IllegalArgumentException, InstantiationException { Method found = null; + if (args == null) { + args = new String[0]; + } Class c = Class.forName(clazz); for (Method m : c.getMethods()) { - if (m.getName().equals(method)) { + if (m.getName().equals(method) && m.getParameterTypes().length == args.length) { found = m; } } @@ -358,13 +365,13 @@ double now; if ((found.getModifiers() & Modifier.STATIC) != 0) { now = getTime(); - res = found.invoke(null); + res = found.invoke(null, (Object[]) args); } else { if (inst == null) { inst = c.newInstance(); } now = getTime(); - res = found.invoke(inst); + res = found.invoke(inst, (Object[]) args); } double took = Math.round((float)(getTime() - now)); time += took; @@ -392,5 +399,12 @@ + "return v.toString();" ) private static native String value(String p, Object d); + + @JavaScriptBody(args = {"p", "d"}, body = + "var v = d[p];\n" + + "if (typeof v === 'undefined') return null;\n" + + "return v;" + ) + private static native String[] values(String p, Object d); } }