/** * Caches a block of code, returning a handle that can be passed to runCachedBlock(). * * @param id An identifier. If null, a unique identifier is generated * @param code The code * @param namespace If true, the namespace will be set * @return Description of the Return Value * @exception Exception instances are thrown when various BeanShell errors occur * @since jEdit 4.1pre1 */ public BshMethod cacheBlock(String id, String code, boolean namespace) throws Exception { String name = "__internal_" + id; // evaluate a method declaration if (namespace) { _eval(global, name + "(ns) {\nthis.callstack.set(0,ns);\n" + code + "\n}"); return global.getMethod(name, new Class[] {NameSpace.class}); } else { _eval(global, name + "() {\n" + code + "\n}"); return global.getMethod(name, new Class[0]); } } // }}}
/** * A command is a scripted method or compiled command class implementing a specified method * signature. Commands are loaded from the classpath and may be imported using the * importCommands() method. * * <p>This method searches the imported commands packages for a script or command object * corresponding to the name of the method. If it is a script the script is sourced into this * namespace and the BshMethod for the requested signature is returned. If it is a compiled class * the class is returned. (Compiled command classes implement static invoke() methods). * * <p>The imported packages are searched in reverse order, so that later imports take priority. * Currently only the first object (script or class) with the appropriate name is checked. If * another, overloaded form, is located in another package it will not currently be found. This * could be fixed. * * <p> * * @return a BshMethod, Class, or null if no such command is found. * @param name is the name of the desired command method * @param argTypes is the signature of the desired command method. * @throws UtilEvalError if loadScriptedCommand throws UtilEvalError i.e. on errors loading a * script that was found */ public Object getCommand(String name, Class[] argTypes, Interpreter interpreter) throws UtilEvalError { if (Interpreter.DEBUG) Interpreter.debug("getCommand: " + name); BshClassManager bcm = interpreter.getClassManager(); if (importedCommands != null) { // loop backwards for precedence for (int i = importedCommands.size() - 1; i >= 0; i--) { String path = importedCommands.get(i); String scriptPath; if (path.equals("/")) scriptPath = path + name + ".bsh"; else scriptPath = path + "/" + name + ".bsh"; Interpreter.debug("searching for script: " + scriptPath); InputStream in = bcm.getResourceAsStream(scriptPath); if (in != null) return loadScriptedCommand(in, name, argTypes, scriptPath, interpreter); // Chop leading "/" and change "/" to "." String className; if (path.equals("/")) className = name; else className = path.substring(1).replace('/', '.') + "." + name; Interpreter.debug("searching for class: " + className); Class clas = bcm.classForName(className); if (clas != null) return clas; } } if (parent != null) return parent.getCommand(name, argTypes, interpreter); else return null; }
/** * Get the bsh method matching the specified signature declared in this name space or a parent. * * <p>Note: this method is primarily intended for use internally. If you use this method outside * of the bsh package you will have to be familiar with BeanShell's use of the Primitive wrapper * class. * * @see bsh.Primitive * @return the BshMethod or null if not found * @param declaredOnly if true then only methods declared directly in this namespace will be found * and no inherited or imported methods will be visible. */ public BshMethod getMethod(String name, Class[] sig, boolean declaredOnly) throws UtilEvalError { BshMethod method = null; // Change import precedence if we are a class body/instance // Get import first. if (method == null && isClass && !declaredOnly) method = getImportedMethod(name, sig); if (method == null && methods != null) { List<BshMethod> list = methods.get(name); if (list != null) { // Apply most specific signature matching Class[][] candidates = new Class[list.size()][]; for (int i = 0; i < candidates.length; i++) candidates[i] = list.get(i).getParameterTypes(); int match = Reflect.findMostSpecificSignature(sig, candidates); if (match != -1) method = list.get(match); } } if (method == null && !isClass && !declaredOnly) method = getImportedMethod(name, sig); // try parent if (!declaredOnly && (method == null) && (parent != null)) return parent.getMethod(name, sig); return method; }
String getPackage() { if (packageName != null) return packageName; if (parent != null) return parent.getPackage(); return null; }
/** * Load a class through this namespace taking into account imports. The class search will proceed * through the parent namespaces if necessary. * * @return null if not found. */ public Class getClass(String name) throws UtilEvalError { Class c = getClassImpl(name); if (c != null) return c; else // implement the recursion for getClassImpl() if (parent != null) return parent.getClass(name); else return null; }
NameSpace copy() { try { final NameSpace clone = (NameSpace) clone(); clone.thisReference = null; clone.variables = clone(variables); clone.methods = clone(methods); clone.importedClasses = clone(importedClasses); clone.importedPackages = clone(importedPackages); clone.importedCommands = clone(importedCommands); clone.importedObjects = clone(importedObjects); clone.importedStatic = clone(importedStatic); clone.names = clone(names); return clone; } catch (CloneNotSupportedException e) { throw new IllegalStateException(e); } }
/** * Description of the Method * * @param namespace Description of the Parameter * @exception UtilEvalError Description of the Exception */ private void resetDefaultVariables(NameSpace namespace) // throws UtilEvalError { { try { namespace.setVariable("reg", factory, false); } catch (UtilEvalError ex) { log.error(ex, ex); } } // }}}
public BshClassManager getClassManager() { if (classManager != null) return classManager; if (parent != null && parent != JAVACODE) return parent.getClassManager(); System.out.println("experiment: creating class manager"); classManager = BshClassManager.createClassManager(null /*interp*/); // Interpreter.debug("No class manager namespace:" +this); return classManager; }
/** Description of the Method */ public void init() { global = new NameSpace(new BshClassManager(), "Embedded BeanShell interpreter"); // global.importPackage("org.gjt.sp.util"); global.importPackage("org.twdata.TW1606U.gui"); global.importPackage("org.twdata.TW1606U.signal"); global.importPackage("org.twdata.TW1606U.action"); global.importPackage("org.twdata.TW1606U.tw"); global.importPackage("org.twdata.TW1606U.tw.data"); global.importPackage("org.twdata.TW1606U.data"); global.importPackage("org.twdata.TW1606U.tw.model"); global.importPackage("org.twdata.TW1606U.tw.signal"); global.importPackage("org.werx.framework.bus"); interpForMethods = createInterpreter(global); } // }}}
/** * Locate a variable and return the Variable object with optional recursion through parent name * spaces. * * <p>If this namespace is static, return only static variables. * * @return the Variable value or null if it is not defined */ protected Variable getVariableImpl(String name, boolean recurse) throws UtilEvalError { Variable var = null; // Change import precedence if we are a class body/instance // Get imported first. if (var == null && isClass) var = getImportedVar(name); if (var == null && variables != null) var = (Variable) variables.get(name); // Change import precedence if we are a class body/instance if (var == null && !isClass) var = getImportedVar(name); // try parent if (recurse && (var == null) && (parent != null)) var = parent.getVariableImpl(name, recurse); return var; }
/** Helper for implementing NameSource */ protected void getAllNamesAux(List<String> list) { list.addAll(variables.keySet()); list.addAll(methods.keySet()); if (parent != null) parent.getAllNamesAux(list); }
static { JAVACODE.isMethod = true; }
/** * Get the top level namespace or this namespace if we are the top. Note: this method should * probably return type bsh.This to be consistent with getThis(); */ public This getGlobal(Interpreter declaringInterpreter) { if (parent != null) return parent.getGlobal(declaringInterpreter); else return getThis(declaringInterpreter); }
SimpleNode getNode() { if (callerInfoNode != null) return callerInfoNode; if (parent != null) return parent.getNode(); else return null; }