public EvalError toEvalError ( SimpleNode node, CallStack callstack ) { return toEvalError( null, node, callstack ); }
public EvalError toEvalError ( SimpleNode node, CallStack callstack ) { return toEvalError( null, node, callstack ); }
/** 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() ); } }
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 LHS toLHS( CallStack callstack, Interpreter interpreter) throws EvalError { try { return getName( callstack.top() ).toLHS( callstack, interpreter ); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack ); } }
public LHS ambiguousNameToLHS( BSHAmbiguousName node ) throws EvalError { try { return node.getName( callstack.top() ).toLHS( this ); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); } }
/** 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() ); } }
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 ); } }
/** array index. Must handle toLHS case. */ private Object doIndex( BSHPrimarySuffix node, Object obj, boolean toLHS ) throws EvalError, ReflectError { int index = BshInterpreterUtil.getIndexAux( obj, this, node ); if ( toLHS ) return new LHS(obj, index); else try { return Reflect.getIndex(obj, index); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); } }
Object toObject( CallStack callstack, Interpreter interpreter, boolean forceClass ) throws EvalError { try { return getName( callstack.top() ).toObject( callstack, interpreter, forceClass ); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack ); } }
public Object ambiguousNameToObject( BSHAmbiguousName node, boolean forceClass ) throws EvalError { try { return node.getName( callstack.top() ).toObject( this, forceClass ); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); } }
@Override public Object visit(BSHCastExpression node) { NameSpace namespace = callstack.top(); Class toType = getType(((BSHType)node.jjtGetChild(0))); SimpleNode expression = (SimpleNode)node.jjtGetChild(1); // evaluate the expression Object fromValue = expression.accept(this); Class fromType = fromValue.getClass(); // TODO: need to add isJavaCastable() test for strictJava // (as opposed to isJavaAssignable()) try { return Types.castObject( fromValue, toType, Types.CAST ); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); } }
/** @return the result of the cast. */ public Object eval( CallStack callstack, Interpreter interpreter ) throws EvalError { Class toType = ((BSHType)jjtGetChild(0)).getType( callstack, interpreter ); SimpleNode expression = (SimpleNode)jjtGetChild(1); // evaluate the expression Object fromValue = expression.eval(callstack, interpreter); // TODO: need to add isJavaCastable() test for strictJava // (as opposed to isJavaAssignable()) try { return Types.castObject( fromValue, toType, Types.CAST ); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack ); } }
@Override public Object visit(BSHMethodDeclaration node) { node.returnType = evalMethodReturnType(node); evalNodes(node); // Install an *instance* of this method in the namespace. // See notes in BshMethod // This is not good... // need a way to update eval without re-installing... // so that we can re-eval params, etc. when classloader changes // look into this NameSpace namespace = callstack.top(); BshMethod bshMethod = new BshMethod( node, namespace, node.modifiers ); try { namespace.setMethod( bshMethod ); } catch ( UtilEvalError e ) { throw e.toEvalError(node,callstack); } return Primitive.VOID; }
/** */ static int getIndexAux(Object obj, int idx, CallStack callstack, Interpreter interpreter, SimpleNode callerInfo ) throws EvalError { int index; try { Object indexVal = ((SimpleNode) callerInfo.jjtGetChild(idx)).eval( callstack, interpreter ); if ( !(indexVal instanceof Primitive) ) indexVal = Types.castObject( indexVal, Integer.TYPE, Types.ASSIGNMENT ); index = ((Primitive) indexVal).intValue(); } catch( UtilEvalError e ) { Interpreter.debug("doIndex: "+e); throw e.toEvalError( "Arrays may only be indexed by integer types.", callerInfo, callstack ); } return index; }
@Override public Object visit(BSHUnaryExpression node) { SimpleNode simpleNode = (SimpleNode)node.jjtGetChild(0); // If this is a unary increment of decrement (either pre or postfix) // then we need an LHS to which to assign the result. Otherwise // just do the unary operation for the value. try { if ( node.kind == ParserConstants.INCR || node.kind == ParserConstants.DECR ) { LHS lhs = primaryExprToLHS((BSHPrimaryExpression) simpleNode); return node.lhsUnaryOperation(lhs, interpreter.getStrictJava()); } else return node.unaryOperation(simpleNode.accept(this), node.kind); } catch ( UtilEvalError e ) { throw e.toEvalError( node, callstack ); } }
public Object eval( CallStack callstack, Interpreter interpreter) throws EvalError { SimpleNode node = (SimpleNode)jjtGetChild(0); // If this is a unary increment of decrement (either pre or postfix) // then we need an LHS to which to assign the result. Otherwise // just do the unary operation for the value. try { if ( kind == INCR || kind == DECR ) { LHS lhs = ((BSHPrimaryExpression)node).toLHS( callstack, interpreter ); return lhsUnaryOperation( lhs, interpreter.getStrictJava() ); } else return unaryOperation( node.eval(callstack, interpreter), kind ); } catch ( UtilEvalError e ) { throw e.toEvalError( this, callstack ); } }
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; }
/** Assign the value to the name. name may evaluate to anything assignable. e.g. a variable or field. */ public void set( String name, Object value ) throws EvalError { // map null to Primtive.NULL coming in... if ( value == null ) value = Primitive.NULL; CallStack callstack = new CallStack(); try { if ( Name.isCompound( name ) ) { LHS lhs = globalNameSpace.getNameResolver( name ).toLHS( new BshEvaluatingVisitor(callstack, this) ); lhs.assign( value, false ); } else // optimization for common case globalNameSpace.setVariable( name, value, false ); } catch ( UtilEvalError e ) { throw e.toEvalError( SimpleNode.JAVACODE, callstack ); } }
/** * Invoke method on arbitrary object instance. * invocation may be static (through the object instance) or dynamic. * Object may be a bsh scripted object (bsh.This type). * * @return the result of the method call */ public static Object invokeObjectMethod(Object object, String methodName, Object[] args, BshEvaluatingVisitor visitor, SimpleNode callerInfo) throws ReflectError, EvalError, InvocationTargetException { // Bsh scripted object if (object instanceof This && !This.isExposedThisMethod(methodName)) { return ((This) object).invokeMethod(methodName, args, visitor.getInterpreter(), visitor.getCallstack(), callerInfo, false/*delcaredOnly*/); } // Plain Java object, find the java method try { BshClassManager bcm = visitor.getInterpreter() == null ? null : visitor.getInterpreter().getClassManager(); Class clas = object.getClass(); Method method = resolveExpectedJavaMethod(bcm, clas, object, methodName, args, false); return invokeMethod(method, object, args); } catch (UtilEvalError e) { throw e.toEvalError(callerInfo, visitor.getCallstack()); } }