office-gobmx/external/beanshell/beanshell-invoke.patch
Xisco Fauli 9f025bdad9 beanshell: upgrade to 2.1.1
* Adapt external/beanshell/java9.patch.0 to fix

[javac] error: Source option 6 is no longer supported. Use 7 or later.
[javac] error: Target option 6 is no longer supported. Use 7 or later.

Downloaded from https://github.com/beanshell/beanshell/releases/download/2.1.1/bsh-2.1.1-src.zip

Change-Id: I969813fd2bb2a910004b6c28f5ed9ba95c39895f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167764
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
2024-05-17 11:50:35 +02:00

76 lines
2.5 KiB
Diff

--- old/beanshell/engine/src/bsh/engine/BshScriptEngine.java
+++ new/beanshell/engine/src/bsh/engine/BshScriptEngine.java
@@ -281,6 +281,11 @@
}
}
+ public Object invoke( Object thiz, String name, Object... args )
+ throws ScriptException, NoSuchMethodException
+ {
+ return invokeMethod( thiz, name, args );
+ }
/**
* Same as invoke(Object, String, Object...) with {@code null} as the
@@ -298,6 +303,11 @@
return invokeMethod(getGlobal(), name, args);
}
+ public Object invoke( String name, Object... args )
+ throws ScriptException, NoSuchMethodException
+ {
+ return invokeFunction( name, args );
+ }
/**
* Returns an implementation of an interface using procedures compiled in the
--- old/beanshell/engine/src/bsh/TestBshScriptEngine.java
+++ new/beanshell/engine/src/bsh/TestBshScriptEngine.java
@@ -2,11 +2,12 @@
import java.io.*;
import javax.script.*;
import static javax.script.ScriptContext.*;
+import java.lang.reflect.*;
public class TestBshScriptEngine
{
public static void main( String [] args )
- throws ScriptException, NoSuchMethodException, IOException
+ throws ScriptException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException
{
ScriptEngineManager manager =
new ScriptEngineManager( bsh.Interpreter.class.getClassLoader() );
@@ -39,11 +40,23 @@
assertTrue( engine.get("bar").equals("gee") );
assertTrue( engine.eval("bar").equals("gee") );
+ // use reflection to pick available method
+ Method invokeMe = null;
+ try {
+ invokeMe = Invocable.class.getMethod( "invokeFunction", String.class, Object[].class );
+ } catch ( Exception e ) { }
+ if (invokeMe == null)
+ {
+ try {
+ invokeMe = Invocable.class.getMethod( "invoke", String.class, Object[].class );
+ } catch ( Exception e ) { }
+ }
+
// install and invoke a method
engine.eval("foo() { return foo+1; }");
// invoke a method
Invocable invocable = (Invocable) engine;
- int foo = (Integer)invocable.invokeFunction( "foo" );
+ int foo = (Integer)invokeMe.invoke( invocable, "foo", (Object) new Object[]{} );
assertTrue( foo == 43 );
// get interface
@@ -58,7 +71,7 @@
engine.eval(
"flag2=false; myObj() { run() { flag2=true; } return this; }");
assertTrue( (Boolean)engine.get("flag2") == false );
- Object scriptedObject = invocable.invokeFunction("myObj");
+ Object scriptedObject = invokeMe.invoke( invocable, "myObj", (Object) new Object[]{} );
assertTrue( scriptedObject instanceof bsh.This );
runnable =
(Runnable)invocable.getInterface( scriptedObject, Runnable.class );