Search in sources :

Example 1 with HtmlObject

use of com.gargoylesoftware.htmlunit.html.HtmlObject in project htmlunit by HtmlUnit.

the class HTMLObjectElement method createAppletMethodAndProperties.

private void createAppletMethodAndProperties() throws Exception {
    final HtmlObject appletNode = (HtmlObject) getDomNodeOrDie();
    final Applet applet = appletNode.getApplet();
    if (applet == null) {
        return;
    }
    // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
    for (final Method method : applet.getClass().getMethods()) {
        final Function f = new BaseFunction() {

            @Override
            public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                final Object[] realArgs = new Object[method.getParameterTypes().length];
                for (int i = 0; i < realArgs.length; i++) {
                    final Object arg;
                    if (i > args.length) {
                        arg = null;
                    } else {
                        arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
                    }
                    realArgs[i] = arg;
                }
                try {
                    return method.invoke(applet, realArgs);
                } catch (final Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        };
        ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) Applet(java.applet.Applet) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) NativeJavaObject(net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

HtmlObject (com.gargoylesoftware.htmlunit.html.HtmlObject)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 Applet (java.applet.Applet)1 Method (java.lang.reflect.Method)1 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)1 Context (net.sourceforge.htmlunit.corejs.javascript.Context)1 Function (net.sourceforge.htmlunit.corejs.javascript.Function)1 NativeJavaObject (net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1