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(final String body, final ClassLoader loader, final String ownName, final Map<String,String> ownMethods) {
81 return new JsCallback() {
83 protected CharSequence callMethod(
84 String ident, String fqn, String method, String params
87 StringBuilder sb = new StringBuilder();
89 if (fqn.equals(ownName.replace('/', '.'))) {
90 if (!ownMethods.containsKey(method + params)) {
91 throw new IllegalStateException("Wrong reference to " + method + params);
93 sb.append("['").append(method).append("(");
94 final Type[] argTps = Type.getArgumentTypes(params);
95 Class<?>[] argCls = new Class<?>[argTps.length];
97 for (int i = 0; i < argCls.length; i++) {
98 sb.append(sep).append(toClass(argTps[i], loader).getName());
103 Class<?> clazz = Class.forName(fqn, false, loader);
104 final Type[] argTps = Type.getArgumentTypes(params);
105 Class<?>[] argCls = new Class<?>[argTps.length];
106 for (int i = 0; i < argCls.length; i++) {
107 argCls[i] = toClass(argTps[i], loader);
109 Method m = clazz.getMethod(method, argCls);
110 sb.append("['").append(m.getName()).append("(");
112 for (Class<?> pt : m.getParameterTypes()) {
113 sb.append(sep).append(pt.getName());
119 } catch (ClassNotFoundException ex) {
120 throw new IllegalStateException("Can't parse " + body, ex);
121 } catch (NoSuchMethodException ex) {
122 throw new IllegalStateException("Can't parse " + body, ex);
129 static void loadScript(JsClassLoader jcl, String resource) {
130 final InputStream script = jcl.getResourceAsStream(resource);
131 if (script == null) {
132 throw new NullPointerException("Can't find " + resource);
137 isr = new InputStreamReader(script, "UTF-8");
144 } catch (Exception ex) {
145 throw new IllegalStateException("Can't execute " + resource, ex);
148 static Class<?> toClass(final Type t, ClassLoader loader) throws ClassNotFoundException {
149 if (t == Type.INT_TYPE) {
151 } else if (t == Type.VOID_TYPE) {
153 } else if (t == Type.BOOLEAN_TYPE) {
155 } else if (t == Type.BYTE_TYPE) {
157 } else if (t == Type.CHAR_TYPE) {
158 return Character.TYPE;
159 } else if (t == Type.SHORT_TYPE) {
161 } else if (t == Type.DOUBLE_TYPE) {
163 } else if (t == Type.FLOAT_TYPE) {
165 } else if (t == Type.LONG_TYPE) {
168 return Class.forName(t.getClassName(), false, loader);