Java BeanShell在嵌入式代码中的调用方法
我需要在代码中调用一些java代码。我用豆沙做的。 因此,我可以这样做:Java BeanShell在嵌入式代码中的调用方法,java,beanshell,Java,Beanshell,我需要在代码中调用一些java代码。我用豆沙做的。 因此,我可以这样做: public void testInterpreter() { Interpreter i = new Interpreter(); i.eval("System.out.println("test1")); } 但如果我想调用解释器中的其他方法呢?我想要这样的东西: public void testInterpreter() { Interpreter i = new Interpreter()
public void testInterpreter() {
Interpreter i = new Interpreter();
i.eval("System.out.println("test1"));
}
但如果我想调用解释器中的其他方法呢?我想要这样的东西:
public void testInterpreter() {
Interpreter i = new Interpreter();
i.eval("testMethod()");
}
public void testMethod() {
System.out.println("test2")
}
但是我得到一个错误“Command not found”将类的实例设置为解释器上的变量:
i.set("instance", this);
i.eval("instance.testMethod()");
我终于找到了解决办法 我正在使用
Janino编译器()
String javaClass=“扩展现有Class1的新java Class2的代码”;
SimpleCompiler sc=新的SimpleCompiler();
理学士库克(javaClass);
类executeClass=sc.getClassLoader().loadClass(“Class2”);
Class1实例=(Class1)executeClass.getConstructor().newInstance();
现在我们有一个Class2的实例。请注意,新的Class2应该扩展现有的Class1,我们只能调用Class1中声明的方法。检查这是否有帮助
package beanshell;
import bsh.EvalError;
import bsh.Interpreter;
public class DemoExample {
public static void main( String [] args ) throws EvalError {
Interpreter i = new bsh.Interpreter();
String usrIp = "if(\"abc\".equals(\"abc\")){"
+ "demoExmp.printValue(\"Rohit\");"
+ "}";
i.eval(""
+ "import beanshell.DemoExample;"
+ "DemoExample demoExmp = new beanshell.DemoExample();"
+ ""+usrIp);
}
public static void printValue(String strVal){
System.out.println("Printing Value "+strVal);
}
}
请尝试以下内容:
package beanshell;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import bsh.EvalError;
import bsh.Interpreter;
public class Demo_Eval {
public static Interpreter i = new Interpreter();
public static void main(String[] args) throws FileNotFoundException, IOException, EvalError, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
String userInput = "printValue()";
Object result = i.eval(""
+ "public class EvalUserInput extends beanshell.Demo_Eval{"
+ "public static void getUserInput(){"
+ userInput+";"
+ "}"
+ "}");
Class<?> noparams[] = {};
Class cls = (Class) result;
Object obj = cls.newInstance();
cls.getDeclaredMethod("getUserInput", noparams).invoke(obj, null);
}
public static void printValue(){
System.out.println("Printed");
}
}
packagebeanshell;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.lang.reflect.InvocationTargetException;
进口bsh.EvalError;
导入bsh.解释器;
公开课演示评估{
公共静态解释器i=新解释器();
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException、EvalError、InstantiationException、IllegalAccessException、IllegalArgumentException、InvocationTargetException、NoSuchMethodException、SecurityException{
字符串userInput=“printValue()”;
对象结果=i.eval(“”)
+“公共类EvalUserInput扩展了beanshell.Demo_Eval{”
+“公共静态无效getUserInput(){”
+用户输入+“;”
+ "}"
+ "}");
类noparams[]={};
类别cls=(类别)结果;
Object obj=cls.newInstance();
getDeclaredMethod(“getUserInput”,noparams).invoke(obj,null);
}
公共静态void printValue(){
系统输出打印项次(“打印”);
}
}
这似乎与问题在任何方面、形状或形式上都无关。
package beanshell;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import bsh.EvalError;
import bsh.Interpreter;
public class Demo_Eval {
public static Interpreter i = new Interpreter();
public static void main(String[] args) throws FileNotFoundException, IOException, EvalError, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException{
String userInput = "printValue()";
Object result = i.eval(""
+ "public class EvalUserInput extends beanshell.Demo_Eval{"
+ "public static void getUserInput(){"
+ userInput+";"
+ "}"
+ "}");
Class<?> noparams[] = {};
Class cls = (Class) result;
Object obj = cls.newInstance();
cls.getDeclaredMethod("getUserInput", noparams).invoke(obj, null);
}
public static void printValue(){
System.out.println("Printed");
}
}