@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; } }
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 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; }
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 synchronized String getMessage() { return super.getMessage() + "Caused by: " + printTargetError( getCause() ); }
private void set( Interpreter interpreter, String name, Object value ) throws MojoFailureException { try { interpreter.set( name, value ); } catch ( EvalError ee ) { MojoFailureException mfe = new MojoFailureException( "cannot define Beanshell global variable '" + name + "': " + ee.getMessage() ); mfe.initCause( ee ); throw mfe; } } }
private void set( Interpreter interpreter, String name, Object value ) throws MojoFailureException { try { interpreter.set( name, value ); } catch ( EvalError ee ) { MojoFailureException mfe = new MojoFailureException( "cannot define Beanshell global variable '" + name + "': " + ee.getMessage() ); mfe.initCause( ee ); throw mfe; } } }
public String getMessage() { return super.getMessage() + "\nTarget exception: " + printTargetError( getCause() ); }
private ScriptException constructScriptException(final EvalError e) { return new ScriptException(e.getMessage(), e.getErrorSourceFile(), e.getErrorLineNumber()); }
private ScriptException constructScriptException(final EvalError e) { return new ScriptException(e.getMessage(), e.getErrorSourceFile(), e.getErrorLineNumber()); }
/** * */ public JRBshEvaluator(String bshScript) throws JRException { super(); this.bshScript = bshScript; interpreter = new Interpreter(); interpreter.setClassLoader(Thread.currentThread().getContextClassLoader()); try { interpreter.eval(new StringReader(bshScript)); } catch(EvalError e) { throw new JRException( "Error evaluating report expressions BeanShell script." + "\nMessage : " + e.getMessage() + "\nLine " + e.getErrorLineNumber() + " : " + extractLineContent(e) ); } }
private Object interpretBeanShell(String text, Map<String, Object> params) throws SensorException { bsh.Interpreter bsh = new bsh.Interpreter(); try { for (String name : params.keySet()) { Object val = params.get(name); if (val instanceof Number) { if (val instanceof Integer) { bsh.set(name, ((Integer) val).intValue()); } else if (val instanceof Long) { bsh.set(name, ((Long) val).longValue()); } else if (val instanceof Double) { bsh.set(name, ((Double) val).doubleValue()); } else if (val instanceof Float) { bsh.set(name, ((Float) val).floatValue()); } else { bsh.set(name, val); } } else if (val instanceof Boolean) { bsh.set(name, ((Boolean) val).booleanValue()); } else { bsh.set(name, params.get(name)); } } return bsh.eval(text); } catch (EvalError e) { throw new SensorException("Error executing bean script: " + e.getMessage(), e); } }
+ "\nMessage : " + e.getMessage() + "\nLine " + e.getErrorLineNumber() + " : " + extractLineContent(e) );
/** If a writable property exists for the given name, set it and return * true, otherwise do nothing and return false. * @param propName the prop name * @param value the value * @param interp the interp * @return true, if successful * @throws UtilEvalError the util eval error */ boolean attemptSetPropertyValue(final String propName, final Object value, final Interpreter interp) throws UtilEvalError { final String accessorName = Reflect.accessorName(Reflect.SET_PREFIX, propName); Object val = Primitive.unwrap(value); final Class<?>[] classArray = new Class<?>[] { val == null ? null : val.getClass()}; final BshMethod m = this.getMethod(accessorName, classArray); if (m != null) try { this.invokeMethod(accessorName, new Object[] {value}, interp); // m.invoke(new Object[] {value}, interp); return true; } catch (final EvalError ee) { throw new UtilEvalError( "'This' property accessor threw exception: " + ee.getMessage(), ee); } return false; }
log(request, "Error running custom beanshell rule (" + id + ") - " + e.getMessage());
@Test public void testExecuteThrowsEvalError1() throws Throwable { Map<Object, Comparable<String>> arguments = new HashMap(); try { new BSHMethod("testBSHMethod\rBshData", false).execute(arguments, "testBSHMethodResultName"); fail("Expected EvalError to be thrown"); } catch (EvalError ex) { assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod BshData;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod BshData;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals("(HashMap) arguments.size()", 0, arguments.size()); } }
@Test public void testExecuteThrowsEvalError() throws Throwable { Collection returnNames = new ArrayList(); try { new BSHMethod("testBSHMethod\rBshData", false).execute(arguments, returnNames); fail("Expected EvalError to be thrown"); } catch (EvalError ex) { assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod BshData;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod BshData;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals("(HashMap) arguments.size()", 0, arguments.size()); assertEquals("(ArrayList) returnNames.size()", 0, returnNames.size()); } }
@Test public void testExecuteMethodThrowsEvalError() throws Throwable { BSHTransactionParticipant bSHGroupSelector = new BSHGroupSelector(); try { bSHGroupSelector.executeMethod(new BSHMethod("testBSHTransactionParticipantBshData", false), 100L, new CharConversionException(), new LogEvent(), "sh.evalOnly"); fail("Expected EvalError to be thrown"); } catch (EvalError ex) { assertEquals("ex.getMessage()", "Class or variable not found: sh.evalOnly", ex.getMessage()); assertEquals("ex.getMessage()", "Class or variable not found: sh.evalOnly", ex.getMessage()); } }
/** Get a property from a scripted object or Primitive.VOID if no such * property exists. * @param propName the prop name * @param interp the interp * @return the property value * @throws UtilEvalError the util eval error */ Object getPropertyValue(final String propName, final Interpreter interp) throws UtilEvalError { String accessorName = Reflect.accessorName(Reflect.GET_PREFIX, propName); final Class<?>[] classArray = Reflect.ZERO_TYPES; BshMethod m = this.getMethod(accessorName, classArray); try { if (m != null) return m.invoke((Object[]) null, interp); accessorName = Reflect.accessorName(Reflect.IS_PREFIX, propName); m = this.getMethod(accessorName, classArray); if (m != null && m.getReturnType() == Boolean.TYPE) return m.invoke((Object[]) null, interp); return Primitive.VOID; } catch (final EvalError ee) { throw new UtilEvalError("'This' property accessor threw exception: " + ee.getMessage(), ee); } }
@Test public void testInitInterpreterThrowsEvalError() throws Throwable { Element e = new Element("testBSHMethodName", Namespace.NO_NAMESPACE); e.addContent("testBSHMethod\rStr"); e.setAttributes(new ArrayList()); BSHMethod bshMethod = BSHMethod.createBshMethod(e); try { bshMethod.initInterpreter(arguments); fail("Expected EvalError to be thrown"); } catch (EvalError ex) { assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod Str;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals( "ex.getMessage()", "Sourced file: inline evaluation of: ``testBSHMethod Str;'' : Typed variable declaration : Class: testBSHMethod not found in namespace", ex.getMessage()); assertEquals("(HashMap) arguments.size()", 0, arguments.size()); } }