/** Default node eval is disabled for this node type. * {@inheritDoc} */ @Override public Object eval( CallStack callstack, Interpreter interpreter ) throws EvalError { throw new EvalError( "Array initializer has no base type.", this, callstack ); }
/** Evaluate to a value object. */ private LHS primaryExprToLHS(BSHPrimaryExpression node) throws EvalError { Object obj = evalPrimaryExpr(node, true); if ( ! (obj instanceof LHS) ) throw new EvalError("Can't assign to:", node, callstack ); else return (LHS)obj; }
/** Helper function to build appropriate EvalError on type exceptions. * @param baseType the array's component type * @param initializer current array dimension * @param argNum current cell index * @param callstack call stack from eval * @throws EvalError the produced type exception */ private void throwTypeError( Class<?> baseType, Object initializer, int argNum, CallStack callstack ) throws EvalError { String rhsType = StringUtil.typeString(initializer); throw new EvalError ( "Incompatible type: " + rhsType +" in initializer of array type: "+ baseType.getSimpleName() +" at position: "+argNum, this, callstack ); }
public static boolean evaluateCondition( SimpleNode condExp, CallStack callstack, Interpreter interpreter) throws EvalError { Object obj = condExp.eval(callstack, interpreter); if ( obj == Primitive.VOID ) throw new EvalError("Condition evaluates to void type", condExp, callstack ); obj = Primitive.unwrap(obj); if( obj instanceof Boolean ) return ((Boolean) obj).booleanValue(); else throw new EvalError( "Condition must evaluate to a Boolean or boolean.", condExp, callstack ); } }
private void throwTypeError( BSHArrayInitializer node, Class baseType, Object initializer, int argNum, CallStack callstack ) throws EvalError { String rhsType; if (initializer instanceof Primitive) rhsType = ((Primitive)initializer).getType().getName(); else rhsType = Reflect.normalizeClassName( initializer.getClass()); throw new EvalError ( "Incompatible type: " + rhsType +" in initializer of array type: "+ baseType +" at position: "+argNum, node, callstack ); }
/** Re-throw as an eval error, prefixing msg to the message and specifying the node. If a node already exists the addNode is ignored. @see #setNode( bsh.SimpleNode ) <p> @param msg may be null for no additional message. */ public EvalError toEvalError( String msg, SimpleNode node, CallStack callstack ) { if ( Interpreter.DEBUG.get() ) printStackTrace(); if ( msg == null ) msg = ""; else msg += ": "; return new EvalError( msg + this.getMessage(), node, callstack, this ); }
public static boolean evaluateCondition( SimpleNode condExp, BshEvaluatingVisitor visitor) throws EvalError { CallStack callstack = visitor.getCallstack(); Interpreter interpreter = visitor.getInterpreter(); Object obj = condExp.accept(visitor); if(obj instanceof Primitive) { if ( obj == Primitive.VOID ) throw new EvalError("Condition evaluates to void type", condExp, callstack ); obj = ((Primitive)obj).getValue(); } if(obj instanceof Boolean) return ((Boolean)obj).booleanValue(); else throw new EvalError( "Condition must evaluate to a Boolean or boolean.", condExp, callstack ); }
/** Re-throw as an eval error, prefixing msg to the message and specifying the node. If a node already exists the addNode is ignored. @see #setNode(bsh.ast.SimpleNode) <p> @param msg may be null for no additional message. */ public EvalError toEvalError( String msg, SimpleNode node, CallStack callstack ) { if ( Interpreter.DEBUG ) printStackTrace(); if ( msg == null ) msg = ""; else msg = msg + ": "; return new EvalError( msg+getMessage(), node, callstack, this ); }
public Class toClass( CallStack callstack, Interpreter interpreter ) throws EvalError { try { return getName( callstack.top() ).toClass(); } catch ( ClassNotFoundException e ) { throw new EvalError( e.getMessage(), this, callstack, e ); } catch ( UtilEvalError e2 ) { // ClassPathException is a type of UtilEvalError throw e2.toEvalError( this, callstack ); } }
@Override public Object visit(BSHThrowStatement node) { Object obj = ((SimpleNode)node.jjtGetChild(0)).accept(this); // need to loosen this to any throwable... do we need to handle // that in interpreter somewhere? check first... if(!(obj instanceof Exception)) throw new EvalError("Expression in 'throw' must be Exception type", node, callstack ); // wrap the exception in a TargetException to propogate it up throw new TargetError( (Exception)obj, node, callstack ); }
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 ); } }
public Class ambiguousNameToClass( BSHAmbiguousName node ) throws EvalError { try { return node.getName( callstack.top() ).toClass(); } catch ( ClassNotFoundException e ) { throw new EvalError( e.getMessage(), node, callstack, e ); } catch ( UtilEvalError e2 ) { // ClassPathException is a type of UtilEvalError throw e2.toEvalError( node, callstack ); } }
public Object objectArrayAllocation( BSHAllocationExpression node, BSHAmbiguousName nameNode, BSHArrayDimensions dimensionsNode ) throws EvalError { NameSpace namespace = callstack.top(); Class type = ambiguousNameToClass( nameNode ); if ( type == null ) throw new EvalError( "Class " + nameNode.getName(namespace) + " not found.", node, callstack ); return arrayAllocation( node, dimensionsNode, type ); }
private Object constructWithClassBody( Class type, Object[] args, BSHBlock block, CallStack callstack, Interpreter interpreter ) throws EvalError { String anon = "anon" + (++innerClassCount); String name = callstack.top().getName() + "$" + anon; This.CONTEXT_ARGS.get().put(anon, args); Modifiers modifiers = new Modifiers(Modifiers.CLASS); Class clas = ClassGenerator.getClassGenerator().generateClass( name, modifiers, null/*interfaces*/, type/*superClass*/, block, ClassGenerator.Type.CLASS, callstack, interpreter ); try { return Reflect.constructObject( clas, args ); } catch ( Exception e ) { Throwable cause = e; if ( e instanceof InvocationTargetException ) { cause = ((InvocationTargetException) e).getTargetException(); } throw new EvalError("Error constructing inner class instance: "+e, this, callstack, cause); } }
/** Simplified method for class generated method stubs. Uses This.namespace and declaringInterpreter and retains callerInfo from namespace.getNode(). Also unwraps Primitives for convenience. */ public Object invokeMethod( String methodName, Object [] args, boolean declaredOnly ) throws EvalError { CallStack callstack = new CallStack(namespace); SimpleNode node = namespace.getNode(); namespace.setNode(null); try { return Primitive.unwrap(invokeMethod( methodName, args, declaringInterpreter, callstack, node, declaredOnly)); } catch (Exception e) { throw new EvalError(e.getMessage(), node, callstack, e); } }
public Object[] getArguments( BSHArguments node ) throws EvalError { // evaluate each child Object[] args = new Object[node.jjtGetNumChildren()]; for(int i = 0; i < args.length; i++) { args[i] = ((SimpleNode)node.jjtGetChild(i)).accept(this); if ( args[i] == Primitive.VOID ) throw new EvalError( "Undefined argument: " + ((SimpleNode)node.jjtGetChild(i)).getText(), node, callstack ); } return args; }
public Object constructWithClassBody( BSHAllocationExpression node, Class type, Object[] args, BSHBlock block ) throws EvalError { String name = callstack.top().getName() + "$" + (++node.innerClassCount); Modifiers modifiers = new Modifiers(); modifiers.addModifier( Modifiers.CLASS, "public" ); Class clas = ClassGenerator.getClassGenerator() .generateClass( name, modifiers, null/*interfaces*/, type/*superClass*/, block, false/*isInterface*/, callstack, interpreter ); try { return Reflect.constructObject( clas, args ); } catch ( Exception e ) { Throwable cause = e; if ( e instanceof InvocationTargetException ) { cause = ((InvocationTargetException) e).getTargetException(); } throw new EvalError("Error constructing inner class instance: "+e, node, callstack, cause); } }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace("BeanshellExecutable", _interpreter.getClassManager()); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.getParameterModifiers(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { final Object value = entry.getValue(); nameSpace.setVariable(entry.getKey(), value != null ? value : Primitive.NULL, false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(Reflect.ZERO_ARGS, _interpreter); if ( Types.getType(result) == Void.TYPE ) return null; return Primitive.unwrap(result); }
public Object invoke(final Map<String, ?> context) throws EvalError { final NameSpace nameSpace = new NameSpace(_interpreter.getClassManager(), "BeanshellExecutable"); nameSpace.setParent(_interpreter.getNameSpace()); final BshMethod method = new BshMethod(_method.getName(), _method.getReturnType(), _method.getParameterNames(), _method.getParameterTypes(), _method.methodBody, nameSpace, _method.getModifiers()); for (final Map.Entry<String, ?> entry : context.entrySet()) { try { nameSpace.setVariable(entry.getKey(), entry.getValue(), false); } catch (final UtilEvalError e) { throw new EvalError("cannot set variable '" + entry.getKey() + '\'', null, null, e); } } final Object result = method.invoke(new Object[0], new BshEvaluatingVisitor(null, _interpreter)); if (result instanceof Primitive) { if (( (Primitive) result).getType() == Void.TYPE) { return null; } return ( (Primitive) result).getValue(); } return result; }
public Object eval(CallStack callstack, Interpreter interpreter) throws EvalError { renderTypeNode(); this.type = evalType(callstack, interpreter); if (!AutoCloseable.class.isAssignableFrom(this.getType())) throw new EvalError("The resource type "+ this.type.getName() +" does not implement java.lang.AutoCloseable.", this, callstack); this.name = this.getDeclarators()[0].name; // we let BSHTypedVariableDeclaration do the heavy lifting super.eval(callstack, interpreter); try { this.varThis = callstack.top().getVariableImpl(this.getName(), true); } catch (UtilEvalError e) { throw e.toEvalError("Unable to evaluate the try-with-resource " + this.getName() + ". With message:" + e.getMessage(), this, callstack); } return Primitive.VOID; }