|
From: <php...@li...> - 2009-01-11 15:07:37
|
Hi,
> But I wanted to invoke a method with its name given as a
> String
Well, use the Java reflection API.
Another possibility is to re-use the PHP/Java Bridge PhpProcedure:
cca.invokeA(ccb);
is equivalent to:
((PhpProcedure)Proxy.getInvocationHandler($cca))).invoke($cca, "invokeA", new Object[]{ccb});
Note the ccb, it has been resolved using the interface IB. If you want to invoke it dynamically, too, use the same procedure as above.
This part of the PHP/Java Bridge API and implementation won't change in the future, so you may use the above code within your application.
A complete example:
package test;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.OutputStreamWriter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class TestGetInterface {
/**
* @param args
*/
public static void main(String[] args) throws Throwable {
//System.setProperty("php.java.bridge.default_log_file", "");
//System.setProperty("php.java.bridge.default_log_level", "3");
new TestGetInterface().doWork();
}
private ScriptEngine scriptEngine;
String classA = "class A{function toString(){return '::A';} function invokeA($b){$b->invokeB();}}\n";
String classB = "class B{function toString(){return '::B';} function invokeB(){echo '::B';}}\n";
String test = "<?php "+classA+classB+" $thiz=java_context()->getAttribute('thiz');\n$thiz->call(java_closure(new A()), java_closure(new B())); ?>";
private void doWork() throws Throwable {
scriptEngine = new ScriptEngineManager().getEngineByName("php-invocable");
scriptEngine.put("thiz", this);
ByteArrayOutputStream out;
OutputStreamWriter writer;
scriptEngine.getContext().setWriter(writer = new OutputStreamWriter(out = new ByteArrayOutputStream()));
scriptEngine.eval(test);
((Closeable)scriptEngine).close();
writer.close();
if(!"::B".equals(out.toString())) {
System.err.println("test failed");
System.exit(1);
}
System.out.println("test okay");
System.exit(0);
}
interface IA { public void invokeA(IB ccb); };
interface IB { public void invokeB(); };
public void call(Object $cca, Object $ccb) throws Throwable {
IA cca = (IA) ((Invocable)scriptEngine).getInterface($cca, IA.class);
IB ccb = (IB) ((Invocable)scriptEngine).getInterface($ccb, IB.class);
//cca.invokeA(ccb);
((php.java.bridge.PhpProcedure)java.lang.reflect.Proxy.getInvocationHandler($cca)).invoke($cca, "invokeA", new Object[]{ccb});
}
}
Regards,
Jost Boekemeier
|