2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.boot.impl;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Enumeration;
31 import java.util.List;
33 import org.apidesign.html.boot.spi.Fn;
34 import org.objectweb.asm.Type;
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 public final class FnUtils {
44 public static Fn define(Class<?> caller, String code, String... names) {
45 JsClassLoader cl = (JsClassLoader)caller.getClassLoader();
46 return cl.defineFn(code, names);
49 public static ClassLoader newLoader(final FindResources f, final Fn.Presenter d, ClassLoader parent) {
50 return new JsClassLoader(parent) {
52 protected URL findResource(String name) {
53 List<URL> l = res(name, true);
54 return l.isEmpty() ? null : l.get(0);
58 protected Enumeration<URL> findResources(String name) {
59 return Collections.enumeration(res(name, false));
62 private List<URL> res(String name, boolean oneIsEnough) {
63 List<URL> l = new ArrayList<URL>();
64 f.findResources(name, l, oneIsEnough);
69 protected Fn defineFn(String code, String... names) {
70 return d.defineFn(code, names);
74 protected void loadScript(Reader code) throws Exception {
80 static String callback(String body, ClassLoader loader, String ownName, Map<String,String> ownMethods) {
82 return callbackImpl(body, loader, ownName, ownMethods);
83 } catch (ClassNotFoundException ex) {
84 throw new IllegalStateException("Can't parse " + body, ex);
85 } catch (NoSuchMethodException ex) {
86 throw new IllegalStateException("Can't parse " + body, ex);
90 private static String callbackImpl(
91 String body, ClassLoader loader, String ownName, Map<String,String> ownMethods
92 ) throws ClassNotFoundException, NoSuchMethodException {
93 StringBuilder sb = new StringBuilder();
96 int next = body.indexOf(".@", pos);
98 sb.append(body.substring(pos));
101 sb.append(body.substring(pos, next));
103 int sigBeg = body.indexOf('(', next);
104 int sigEnd = body.indexOf(')', sigBeg);
106 int colon4 = body.indexOf("::", next);
108 if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
109 throw new IllegalStateException("Malformed body " + body);
112 String fqn = body.substring(next + 2, colon4);
113 String method = body.substring(colon4 + 2, sigBeg);
114 String params = body.substring(sigBeg, sigEnd + 1);
116 if (fqn.equals(ownName.replace('/', '.'))) {
117 if (!ownMethods.containsKey(method + params)) {
118 throw new IllegalStateException("Wrong refernece to " + method + params);
120 sb.append("['").append(method).append("(");
121 final Type[] argTps = Type.getArgumentTypes(params);
122 Class<?>[] argCls = new Class<?>[argTps.length];
124 for (int i = 0; i < argCls.length; i++) {
125 sb.append(sep).append(toClass(argTps[i], loader).getName());
130 Class<?> clazz = Class.forName(fqn, false, loader);
131 final Type[] argTps = Type.getArgumentTypes(params);
132 Class<?>[] argCls = new Class<?>[argTps.length];
133 for (int i = 0; i < argCls.length; i++) {
134 argCls[i] = toClass(argTps[i], loader);
136 Method m = clazz.getMethod(method, argCls);
138 sb.append("['").append(m.getName()).append("(");
140 for (Class<?> pt : m.getParameterTypes()) {
141 sb.append(sep).append(pt.getName());
151 private static Class<?> toClass(final Type t, ClassLoader loader) throws ClassNotFoundException {
152 if (t == Type.INT_TYPE) {
154 } else if (t == Type.VOID_TYPE) {
156 } else if (t == Type.BOOLEAN_TYPE) {
158 } else if (t == Type.BYTE_TYPE) {
160 } else if (t == Type.CHAR_TYPE) {
161 return Character.TYPE;
162 } else if (t == Type.SHORT_TYPE) {
164 } else if (t == Type.DOUBLE_TYPE) {
166 } else if (t == Type.FLOAT_TYPE) {
168 } else if (t == Type.LONG_TYPE) {
171 return Class.forName(t.getClassName(), false, loader);
174 static void loadScript(JsClassLoader jcl, String resource) {
175 final InputStream script = jcl.getResourceAsStream(resource);
176 if (script == null) {
177 throw new NullPointerException("Can't find " + resource);
182 isr = new InputStreamReader(script, "UTF-8");
189 } catch (Exception ex) {
190 throw new IllegalStateException("Can't execute " + resource, ex);