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); }
@Override public boolean equals(Object o) { if (o == null) return false; if (o == this) return true; if (o.getClass() != this.getClass()) return false; BshMethod m = (BshMethod)o; if( !name.equals(m.name) || getParameterCount() != m.getParameterCount() ) return false; for( int i = 0; i < getParameterCount(); i++ ) if( !equal(getParameterTypes()[i], m.getParameterTypes()[i]) ) return false; return true; }
@Override public int hashCode() { int h = name.hashCode() + getClass().hashCode(); for (final Class<?> cparamType : getParameterTypes()) h += 3 + (cparamType == null ? 0 : cparamType.hashCode()); return h + getParameterCount(); } }
/** Produce a complete string representation of a bsh method. Shows * modifiers, return type, name and parameter types. * @param method a bsh method * @return string representation of a method */ public static String methodString(BshMethod method) { String mods = method.getModifiers().toString().substring(11); StringBuilder sb = new StringBuilder(); return sb.append(mods).append(" ") .append(getTypeName(method.getReturnType())).append(" ") .append(methodString(method.getName(), method.getParameterTypes(), method.getParameterNames())) .append(mods.contains("abstract") ? ";" : " {}").toString(); }
/** Invoke the declared method with the specified arguments and interpreter reference. This is the simplest form of invoke() for BshMethod intended to be used in reflective style access to bsh scripts. */ public Object invoke( Object[] argValues, BshEvaluatingVisitor visitor ) throws EvalError { return invoke( argValues, visitor, null, false ); }
throws EvalError if (hasModifier("abstract")) throw new EvalError( "Cannot invoke abstract method " + name, callerInfo, callstack ); Class<?> returnType = getReturnType(); Class<?> [] paramTypes = getParameterTypes(); if ( !isVarArgs() && argValues.length != getParameterCount() ) { int lastParamIndex = getParameterCount() - 1; Object varArgs = !isVarArgs() ? null : Array.newInstance( paramTypes[lastParamIndex].getComponentType(), Class<?> paramType = isVarArgs() && k == lastParamIndex ? paramTypes[k].getComponentType() : paramTypes[k]; if (isVarArgs() && i >= lastParamIndex) Array.set(varArgs, i-k, Primitive.unwrap(argValues[i])); else if (isVarArgs()) try { localNameSpace.setTypedVariable( paramNames[lastParamIndex],
.filter(m->m.hasModifier("final")&&!m.hasModifier("private")) .collect(Collectors.toList())); if (null != Reflect.getDeclaredMethod(clas, m.getName(), m.getParameterTypes())) throw new EvalError("Cannot override "+m.getName()+"() in " + StringUtil.typeString(superClass) + " overridden method is final", null, null);
/** 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); } }
/** Dissallow static vars outside of a class. * @param name is here just to allow the error message to use it protected * void checkVariableModifiers(String name, Modifiers modifiers) * throws UtilEvalError { if (modifiers!=null && * modifiers.hasModifier("static")) throw new UtilEvalError("Can't * declare static variable outside of class: "+name); } * @param method the method * @throws UtilEvalError the util eval error Note: this is primarily for * internal use. * @see Interpreter#source(String) * @see Interpreter#eval(String) */ public void setMethod(BshMethod method) { String name = method.getName(); if (!this.methods.containsKey(name)) this.methods.put(name, new ArrayList<BshMethod>(1)); this.methods.get(name).remove(method); this.methods.get(name).add(0, method); }
throws EvalError Class returnType = getReturnType(); Class [] paramTypes = getParameterTypes();
public Class[] getParameterTypes() { return _method.getParameterTypes(); }
/** 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; }
public Class getReturnType() { return _method.getReturnType(); }
return invokeImpl( argValues, interpreter, callstack, callerInfo, overrideNameSpace ); return invokeImpl( argValues, interpreter, callstack, callerInfo, overrideNameSpace );
public boolean equals(Object o) { if (o == null) { return false; } if (o == this) { return true; } if (o.getClass() == this.getClass()) { BshMethod m = (BshMethod)o; if( !name.equals(m.name) || numArgs!=m.numArgs ) return false; for( int i=0; i<numArgs; i++ ) { if( !equal(cparamTypes[i],m.cparamTypes[i]) ) return false; } return true; } return false; }
/** Invoke the declared method with the specified arguments and interpreter reference. This is the simplest form of invoke() for BshMethod intended to be used in reflective style access to bsh scripts. */ public Object invoke( Object[] argValues, Interpreter interpreter ) throws EvalError { return invoke( argValues, interpreter, null, null, false ); }
/** Note: this is primarily for internal use. @see Interpreter#source( String ) @see Interpreter#eval( String ) */ public void setMethod( BshMethod method ) throws UtilEvalError { //checkMethodModifiers( method ); if ( methods == null ) methods = new HashMap<String,List<BshMethod>>(); String name = method.getName(); List<BshMethod> list = methods.get(name); if ( list == null ) { methods.put(name, Collections.singletonList(method)); } else { if( !(list instanceof ArrayList) ) { list = new ArrayList<BshMethod>(list); methods.put( name, list ); } list.remove(method); list.add( method ); } }
public String toString() { return "Scripted Method: " + StringUtil.methodString( name, getParameterTypes() ); }
/** 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; }
return invokeImpl( argValues, visitor, callerInfo, overrideNameSpace ); return invokeImpl( argValues, visitor, callerInfo, overrideNameSpace );