Search in sources :

Example 1 with XThis

use of bsh.XThis in project spring-framework by spring-projects.

the class BshScriptUtils method evaluateBshScript.

/**
	 * Evaluate the specified BeanShell script based on the given script source,
	 * keeping a returned script Class or script Object as-is.
	 * <p>The script may either be a simple script that needs a corresponding proxy
	 * generated (implementing the specified interfaces), or declare a full class
	 * or return an actual instance of the scripted object (in which case the
	 * specified interfaces, if any, need to be implemented by that class/instance).
	 * @param scriptSource the script source text
	 * @param scriptInterfaces the interfaces that the scripted Java object is
	 * supposed to implement (may be {@code null} or empty if the script itself
	 * declares a full class or returns an actual instance of the scripted object)
	 * @param classLoader the ClassLoader to use for evaluating the script
	 * @return the scripted Java class or Java object
	 * @throws EvalError in case of BeanShell parsing failure
	 */
static Object evaluateBshScript(String scriptSource, Class<?>[] scriptInterfaces, ClassLoader classLoader) throws EvalError {
    Assert.hasText(scriptSource, "Script source must not be empty");
    Interpreter interpreter = new Interpreter();
    interpreter.setClassLoader(classLoader);
    Object result = interpreter.eval(scriptSource);
    if (result != null) {
        return result;
    } else {
        // Simple BeanShell script: Let's create a proxy for it, implementing the given interfaces.
        Assert.notEmpty(scriptInterfaces, "Given script requires a script proxy: At least one script interface is required.");
        XThis xt = (XThis) interpreter.eval("return this");
        return Proxy.newProxyInstance(classLoader, scriptInterfaces, new BshObjectInvocationHandler(xt));
    }
}
Also used : Interpreter(bsh.Interpreter) XThis(bsh.XThis)

Aggregations

Interpreter (bsh.Interpreter)1 XThis (bsh.XThis)1