1.1 --- a/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java Wed Jun 19 22:29:41 2013 +0200
1.2 +++ b/boot/src/main/java/org/apidesign/html/boot/impl/FnUtils.java Tue Jun 25 15:34:37 2013 +0200
1.3 @@ -20,11 +20,15 @@
1.4 */
1.5 package org.apidesign.html.boot.impl;
1.6
1.7 +import java.lang.reflect.Method;
1.8 import java.net.URL;
1.9 import java.util.ArrayList;
1.10 import java.util.Collections;
1.11 import java.util.Enumeration;
1.12 import java.util.List;
1.13 +import java.util.logging.Level;
1.14 +import java.util.logging.Logger;
1.15 +import java.util.regex.Pattern;
1.16 import org.apidesign.html.boot.spi.Fn;
1.17
1.18 /**
1.19 @@ -66,5 +70,56 @@
1.20 }
1.21 };
1.22 }
1.23 +
1.24 + static String callback(String body, ClassLoader loader) {
1.25 + try {
1.26 + return callbackImpl(body, loader);
1.27 + } catch (ClassNotFoundException ex) {
1.28 + throw new IllegalStateException("Can't parse " + body, ex);
1.29 + } catch (NoSuchMethodException ex) {
1.30 + throw new IllegalStateException("Can't parse " + body, ex);
1.31 + }
1.32 + }
1.33 +
1.34 + private static String callbackImpl(String body, ClassLoader loader)
1.35 + throws ClassNotFoundException, NoSuchMethodException {
1.36 + StringBuilder sb = new StringBuilder();
1.37 + int pos = 0;
1.38 + for (;;) {
1.39 + int next = body.indexOf(".@", pos);
1.40 + if (next == -1) {
1.41 + sb.append(body.substring(pos));
1.42 + return sb.toString();
1.43 + }
1.44 + sb.append(body.substring(pos, next));
1.45 +
1.46 + int sigBeg = body.indexOf('(', next);
1.47 + int sigEnd = body.indexOf(')', sigBeg);
1.48 +
1.49 + int colon4 = body.indexOf("::", next);
1.50 +
1.51 + if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
1.52 + throw new IllegalStateException("Malformed body " + body);
1.53 + }
1.54 +
1.55 + String fqn = body.substring(next + 2, colon4);
1.56 + String method = body.substring(colon4 + 2, sigBeg);
1.57 + String params = body.substring(sigBeg + 1, sigEnd);
1.58 +
1.59 + Class<?> clazz = Class.forName(fqn, false, loader);
1.60 + Method m = clazz.getMethod(method);
1.61 +
1.62 + sb.append("['").append(m.getName()).append("(");
1.63 + String sep = "";
1.64 + for (Class<?> pt : m.getParameterTypes()) {
1.65 + sb.append(sep).append(pt.getName());
1.66 + sep = ",";
1.67 + }
1.68 + sb.append(")']");
1.69 +
1.70 + pos = sigEnd + 1;
1.71 + }
1.72 + }
1.73 +
1.74
1.75 }