📄 beanshell.java
字号:
} catch(EvalError e) { // do nothing } } } //}}} //{{{ eval() method /** * Evaluates the specified BeanShell expression. Errors are reported in * a dialog box. * @param view The view. Within the script, references to * <code>buffer</code>, <code>textArea</code> and <code>editPane</code> * are determined with reference to this parameter. * @param namespace The namespace * @param command The expression * @since jEdit 4.0pre8 */ public static Object eval(View view, NameSpace namespace, String command) { try { return _eval(view,namespace,command); } catch(Throwable e) { Log.log(Log.ERROR,BeanShell.class,e); handleException(view,null,e); } return null; } //}}} //{{{ _eval() method /** * Evaluates the specified BeanShell expression. Unlike * <code>eval()</code>, this method passes any exceptions to the caller. * * @param view The view. Within the script, references to * <code>buffer</code>, <code>textArea</code> and <code>editPane</code> * are determined with reference to this parameter. * @param namespace The namespace * @param command The expression * @exception Exception instances are thrown when various BeanShell * errors occur * @since jEdit 3.2pre7 */ public static Object _eval(View view, NameSpace namespace, String command) throws Exception { Interpreter interp = createInterpreter(namespace); try { if(view != null) { EditPane editPane = view.getEditPane(); interp.set("view",view); interp.set("editPane",editPane); interp.set("buffer",editPane.getBuffer()); interp.set("textArea",editPane.getTextArea()); } return interp.eval(command); } catch(Exception e) { unwrapException(e); // never called return null; } finally { try { if(view != null) { interp.unset("view"); interp.unset("editPane"); interp.unset("buffer"); interp.unset("textArea"); } } catch(EvalError e) { // do nothing } } } //}}} //{{{ cacheBlock() method /** * 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 * @exception Exception instances are thrown when various BeanShell errors * occur * @since jEdit 4.1pre1 */ public static BshMethod cacheBlock(String id, String code, boolean namespace) throws Exception { String name = "__internal_" + id; // evaluate a method declaration if(namespace) { _eval(null,global,name + "(ns) {\nthis.callstack.set(0,ns);\n" + code + "\n}"); return global.getMethod(name,new Class[] { NameSpace.class }); } else { _eval(null,global,name + "() {\n" + code + "\n}"); return global.getMethod(name,new Class[0]); } } //}}} //{{{ runCachedBlock() method /** * Runs a cached block of code in the specified namespace. Faster than * evaluating the block each time. * @param method The method instance returned by cacheBlock() * @param view The view * @param namespace The namespace to run the code in * @exception Exception instances are thrown when various BeanShell * errors occur * @since jEdit 4.1pre1 */ public static Object runCachedBlock(BshMethod method, View view, NameSpace namespace) throws Exception { boolean useNamespace; if(namespace == null) { useNamespace = false; namespace = global; } else useNamespace = true; try { if(view != null) { namespace.setVariable("view",view); EditPane editPane = view.getEditPane(); namespace.setVariable("editPane",editPane); namespace.setVariable("buffer",editPane.getBuffer()); namespace.setVariable("textArea",editPane.getTextArea()); } Object retVal = method.invoke(useNamespace ? new Object[] { namespace } : NO_ARGS, interpForMethods,new CallStack()); if(retVal instanceof Primitive) { if(retVal == Primitive.VOID) return null; else return ((Primitive)retVal).getValue(); } else return retVal; } catch(Exception e) { unwrapException(e); // never called return null; } finally { if(view != null) { try { namespace.setVariable("view",null); namespace.setVariable("editPane",null); namespace.setVariable("buffer",null); namespace.setVariable("textArea",null); } catch(EvalError e) { // can't do much } } } } //}}} //{{{ isScriptRunning() method /** * Returns if a BeanShell script or macro is currently running. * @since jEdit 2.7pre2 */ public static boolean isScriptRunning() { return running; } //}}} //{{{ getNameSpace() method /** * Returns the global namespace. * @since jEdit 3.2pre5 */ public static NameSpace getNameSpace() { return global; } //}}} //{{{ Deprecated functions //{{{ runScript() method /** * @deprecated The <code>rethrowBshErrors</code> parameter is now * obsolete; call <code>_runScript()</code> or <code>runScript()</code> * instead. */ public static void runScript(View view, String path, boolean ownNamespace, boolean rethrowBshErrors) { runScript(view,path,null,ownNamespace); } //}}} //{{{ runScript() method /** * @deprecated The <code>rethrowBshErrors</code> parameter is now * obsolete; call <code>_runScript()</code> or <code>runScript()</code> * instead. */ public static void runScript(View view, String path, Reader in, boolean ownNamespace, boolean rethrowBshErrors) { runScript(view,path,in,ownNamespace); } //}}} //{{{ eval() method /** * @deprecated The <code>rethrowBshErrors</code> parameter is now * obsolete; call <code>_eval()</code> or <code>eval()</code> instead. */ public static Object eval(View view, String command, boolean rethrowBshErrors) { return eval(view,global,command); } //}}} //{{{ eval() method /** * @deprecated The <code>rethrowBshErrors</code> parameter is now * obsolete; call <code>_eval()</code> or <code>eval()</code> instead. */ public static Object eval(View view, NameSpace namespace, String command, boolean rethrowBshErrors) { return eval(view,namespace,command); } //}}} //}}} //{{{ Package-private members //{{{ init() method static void init() { BshClassManager.setClassLoader(new JARClassLoader()); global = new NameSpace("jEdit embedded BeanShell interpreter"); global.importPackage("org.gjt.sp.jedit"); global.importPackage("org.gjt.sp.jedit.browser"); global.importPackage("org.gjt.sp.jedit.buffer"); global.importPackage("org.gjt.sp.jedit.gui"); global.importPackage("org.gjt.sp.jedit.help"); global.importPackage("org.gjt.sp.jedit.io"); global.importPackage("org.gjt.sp.jedit.msg"); global.importPackage("org.gjt.sp.jedit.options"); global.importPackage("org.gjt.sp.jedit.pluginmgr"); global.importPackage("org.gjt.sp.jedit.print"); global.importPackage("org.gjt.sp.jedit.search"); global.importPackage("org.gjt.sp.jedit.syntax"); global.importPackage("org.gjt.sp.jedit.textarea"); global.importPackage("org.gjt.sp.util"); interpForMethods = createInterpreter(global); Log.log(Log.DEBUG,BeanShell.class,"BeanShell interpreter version " + Interpreter.VERSION); } //}}} //}}} //{{{ Private members //{{{ Static variables private static final Object[] NO_ARGS = new Object[0]; private static Interpreter interpForMethods; private static NameSpace global; private static boolean running; //}}} //{{{ unwrapException() method /** * This extracts an exception from a 'wrapping' exception, as BeanShell * sometimes throws. This gives the user a more accurate error traceback */ private static void unwrapException(Exception e) throws Exception { if(e instanceof TargetError) { Throwable t = ((TargetError)e).getTarget(); if(t instanceof Exception) throw (Exception)t; else if(t instanceof Error) throw (Error)t; } if(e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException)e).getTargetException(); if(t instanceof Exception) throw (Exception)t; else if(t instanceof Error) throw (Error)t; } throw e; } //}}} //{{{ handleException() method private static void handleException(View view, String path, Throwable t) { if(t instanceof IOException) { VFSManager.error(view,path,"ioerror.read-error", new String[] { t.toString() }); } else new BeanShellErrorDialog(view,t); } //}}} //{{{ createInterpreter() method private static Interpreter createInterpreter(NameSpace nameSpace) { return new Interpreter(null,System.out,System.err,false,nameSpace); } //}}} //}}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -