/** 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); }
/** 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 ); } }
/** 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(); }
for (BshMethod mtd : _mtds) { try { _bshns.setMethod(mtd.getName(), mtd); } catch (Throwable ex) { log.warn("Ignored failure of set " + mtd, ex);
Fields.setAccessible(f, acs); ns.setMethod(mtd.getName(), mtd); } catch (IOException ex) { throw ex;
final BshMethod[] mtds = ns.getMethods(); for (int j = mtds != null ? mtds.length : 0; --j >= 0;) { final String nm = mtds[j].getName(); if (isMethodSerializable(nm) && (filter == null || filter.accept(nm, mtds[j]))) {
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; }