/** Gets the imported method. * @param name the name * @param sig the sig * @return the imported method * @throws UtilEvalError the util eval error */ protected BshMethod getImportedMethod(final String name, final Class<?>[] sig) throws UtilEvalError { // Try object imports for (final Object object : this.importedObjects) { final Invocable method = Reflect.resolveJavaMethod( object.getClass(), name, sig, false/* onlyStatic */); if (method != null) return new BshMethod(method, object); } // Try static imports for (final Class<?> stat : this.importedStatic) { final Invocable method = Reflect.resolveJavaMethod( stat, name, sig, true/* onlyStatic */); if (method != null) return new BshMethod(method, null/* object */); } return null; }
/** Find a static method member of baseClass, for the given name. * @param baseClass class to query * @param methodName method name to find * @return a BshMethod wrapped Method. */ static BshMethod staticMethodImport(Class<?> baseClass, String methodName) { Invocable method = BshClassManager.memberCache.get(baseClass) .findStaticMethod(methodName); if (null != method) return new BshMethod(method, null); return null; }
protected BshMethod getImportedMethod( String name, Class [] sig ) throws UtilEvalError { // Try object imports if ( importedObjects != null ) for(int i=0; i<importedObjects.size(); i++) { Object object = importedObjects.get(i); Class clas = object.getClass(); Method method = Reflect.resolveJavaMethod( getClassManager(), clas, name, sig, false/*onlyStatic*/ ); if ( method != null ) return new BshMethod( method, object ); } // Try static imports if ( importedStatic!= null ) for(int i=0; i<importedStatic.size(); i++) { Class clas = importedStatic.get(i); Method method = Reflect.resolveJavaMethod( getClassManager(), clas, name, sig, true/*onlyStatic*/ ); if ( method != null ) return new BshMethod( method, null/*object*/ ); } return null; }
/** Evaluate the declaration of the method. That is, determine the structure of the method and install it into the caller's namespace. */ public Object eval( CallStack callstack, Interpreter interpreter ) throws EvalError { returnType = evalReturnType( callstack, interpreter ); evalNodes( callstack, interpreter ); // 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( this, namespace, modifiers ); namespace.setMethod( bshMethod ); return Primitive.VOID; }
@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; }
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 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); }