Introducing Agent-Class to allow java.lang.instrument-like transforms of the @JavaScriptBody annotation
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.spi;
23 import java.io.Closeable;
24 import java.io.Reader;
26 import net.java.html.js.JavaScriptBody;
27 import org.apidesign.html.boot.impl.FnContext;
29 /** Represents single JavaScript function that can be invoked.
30 * Created via {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
32 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
34 public abstract class Fn {
35 private final Presenter presenter;
38 * @deprecated Ineffective as of 0.6.
39 * Provide a presenter via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}
47 /** Creates new function object and associates it with given presenter.
49 * @param presenter the browser presenter associated with this function
52 protected Fn(Presenter presenter) {
53 this.presenter = presenter;
56 /** True, if currently active presenter is the same as presenter this
57 * function has been created for via {@link #Fn(org.apidesign.html.boot.spi.Fn.Presenter)}.
59 * @return true, if proper presenter is used
61 public final boolean isValid() {
62 return FnContext.currentPresenter() == presenter;
65 /** Helper method to check if the provided instance is valid function.
66 * Checks if the parameter is non-null and if so, does {@link #isValid()}
69 * @param fnOrNull function or <code>null</code>
70 * @return true if the parameter is non-null and valid
73 public static boolean isValid(Fn fnOrNull) {
74 return fnOrNull != null && fnOrNull.isValid();
77 /** Helper method to find current presenter and ask it to define new
78 * function by calling {@link Presenter#defineFn(java.lang.String, java.lang.String...)}.
80 * @param caller the class who wishes to define the function
81 * @param code the body of the function (can reference <code>this</code> and <code>names</code> variables)
82 * @param names names of individual parameters
83 * @return the function object that can be {@link Fn#invoke(java.lang.Object, java.lang.Object...) invoked}
86 public static Fn define(Class<?> caller, String code, String... names) {
87 return FnContext.currentPresenter().defineFn(code, names);
90 /** The currently active presenter.
92 * @return the currently active presenter or <code>null</code>
95 public static Presenter activePresenter() {
96 return FnContext.currentPresenter();
99 /** Activates given presenter. Used by the code generated by
100 * {@link JavaScriptBody} annotation:
102 * try ({@link Closeable} c = Fn.activate(presenter)) {
103 * doCallsInPresenterContext();
107 * @param p the presenter that should be active until closable is closed
108 * @return the closable to close
111 public static Closeable activate(Presenter p) {
112 return FnContext.activate(p);
115 /** Invokes the defined function with specified <code>this</code> and
116 * appropriate arguments.
118 * @param thiz the meaning of <code>this</code> inside of the JavaScript
119 * function - can be <code>null</code>
120 * @param args arguments for the function
121 * @return return value from the function
122 * @throws Exception if something goes wrong, as exception may be thrown
124 public abstract Object invoke(Object thiz, Object... args) throws Exception;
126 /** The representation of a <em>presenter</em> - usually a browser window.
127 * Should be provided by a library included in the application and registered
128 * in <code>META-INF/services</code>, for example with
129 * <code>@ServiceProvider(service = Fn.Presenter.class)</code> annotation.
131 public interface Presenter {
132 /** Creates new function with given parameter names and provided body.
134 * @param code the body of the function. Can refer to variables named
135 * as <code>names</code>
136 * @param names names of parameters of the function - these will be
137 * available when the <code>code</code> body executes
139 * @return function that can be later invoked
141 public Fn defineFn(String code, String... names);
143 /** Opens the browser, loads provided page and when the
144 * page is ready, it calls back to the provider runnable.
146 * @param page the URL for the page to display
147 * @param onPageLoad callback when the page is ready
149 public void displayPage(URL page, Runnable onPageLoad);
151 /** Loads a script into the browser JavaScript interpreter and
153 * @param code the script to execute
154 * @throws Exception if something goes wrong, throw an exception
156 public void loadScript(Reader code) throws Exception;