⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 evaljavabsh.java

📁 The ElectricTM VLSI Design System is an open-source Electronic Design Automation (EDA) system that c
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    private static class runScriptJob extends Job	{        String script;        protected runScriptJob(String script) {            super("JavaBsh script: "+script, User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER);            this.script = script;        }        public boolean doIt() throws JobException {            EvalJavaBsh evaluator = new EvalJavaBsh();            evaluator.doSource(script);			return true;       }    }    // ****************************** REFLECTION FOR ACCESSING THE BEAN SHELL ******************************    private void initBSH()    {        // if already initialized, return        if (interpreterClass != null) return;        // find the BSH classes        try        {            interpreterClass = Class.forName("bsh.Interpreter");            targetErrorClass = Class.forName("bsh.TargetError");        } catch (ClassNotFoundException e)        {            if (Job.getDebug())            	System.out.println("GNU Release can't find the Bean Shell: " + e.getMessage());            interpreterClass = null;            return;        }        // find the necessary methods on the BSH class        try        {            evalMethod = interpreterClass.getMethod("eval", new Class[] {String.class});            sourceMethod = interpreterClass.getMethod("source", new Class[] {String.class});            setMethod = interpreterClass.getMethod("set", new Class[] {String.class, Object.class});            getMethod = interpreterClass.getMethod("get", new Class[] {String.class});            getTargetMethod = targetErrorClass.getMethod("getTarget", (Class[])null);        } catch (NoSuchMethodException e)        {            System.out.println("Can't find methods in the Bean Shell: " + e.getMessage());            interpreterClass = null;            return;        }    }    /**     * Set a variable in the Java Bean Shell     * @param name the name of the variable     * @param value the value to set the variable to     */    public void setVariable(String name, Object value)    {        try {            if (envObject != null) {                setMethod.invoke(envObject, new Object[] {name, value});            }        } catch (Exception e) {            handleInvokeException(e, "Bean shell error setting " + name + " to "+ value + ": ");        }    }    public Object getVariable(String name)    {        try {            if (envObject != null) {                return getMethod.invoke(envObject, new Object [] {name});            }        } catch (Exception e) {            handleInvokeException(e, "Bean shell error getting variable " + name);        }        return null;    }    // -------------------------- Private Methods -----------------------------    /**     * Evaluate a string containing Java Bean Shell code.     * @param line the string to evaluate     * @return an object representing the evaluated string, or null on error.     * @throws VarContext.EvalException exception     */    private Object doEval(String line) throws VarContext.EvalException    {        Object returnVal = null;        try {            if (envObject != null) {                returnVal = evalMethod.invoke(envObject, new Object[] {line});            }        } catch (Exception e) {            if (e instanceof InvocationTargetException) {                // rethrow original EvalException, if any                VarContext.EvalException ee = getEvalException((InvocationTargetException)e);                if (ee != null) throw ee;            }            if (!handleInvokeException(e, "Bean shell error evaluating "+line)) {                throw new VarContext.EvalException(e.getMessage(), e);            }        }        return returnVal;    }    public Object doEvalLine(String line)    {        Object obj = null;        try        {            obj = doEval(line);        }        catch (Exception e)        {            e.printStackTrace();        }        return obj;    }    /**     * Method to determine whether a string is valid Java code.     * @param line the string to test.     * @return true if the string is valid, evaluatable Java code.     */    public boolean isValidJava(String line)    {        try        {            if (envObject != null)            {                evalMethod.invoke(envObject, new Object[] {line});                return true;            }        } catch (Exception e) {}        return false;    }    // source a Java Bean Shell script file    private void doSource(String file)    {        try {            if (envObject != null) {                sourceMethod.invoke(envObject, new Object[] {file});            }        } catch (Exception e) {            handleInvokeException(e, "Java Bean shell error sourcing '" + file +"'");        }    }    private static Throwable doGetTarget(Object ex)    {        Throwable returnVal = null;        if (interpreterClass != null && targetErrorClass.isInstance(ex)) {            try {                returnVal = (Throwable)getTargetMethod.invoke(ex, (Object[])null);            } catch (Exception e) {                handleInvokeException(e, "Java Bean shell error getting exception target");            }        }        return returnVal;    }    /**     * If the InvocationTargetException was generated because of an EvalException,     * get the EvalException that is wrapped by the target exception.     * @param e the invocation target exception     * @return the initial eval exception, or null if none.     */    private VarContext.EvalException getEvalException(InvocationTargetException e) {        Throwable t = e.getCause();        if (t == null) return null;        Throwable tt = doGetTarget(t);        if (tt == null) return null;        if (tt instanceof VarContext.EvalException)            return (VarContext.EvalException)tt;        return null;    }    /**     * Handle exceptions thrown by attempting to invoke a reflected method or constructor.     * @param e The exception thrown by the invoked method or constructor.     * @param description a description of the event to be printed with the error message.     * @return true if exception handled (error message printed), false if not     */    private static boolean handleInvokeException(Exception e, String description) {        boolean handled = false;        if (e instanceof InvocationTargetException) {            // This wraps an exception thrown by the method invoked.            Throwable t = e.getCause();            if (t != null)                handled = handleBshError((Exception)t, description);        }        else if (e instanceof IllegalArgumentException) {            System.out.println(description+": "+e.getMessage());            if (DEBUG) e.printStackTrace(System.out);        }        else if (e instanceof IllegalAccessException) {            System.out.println(description+": "+e.getMessage());            if (DEBUG) e.printStackTrace(System.out);        }        else {            System.out.println("Unhandled Exception: ");            System.out.println(description+": "+e.getMessage());            e.printStackTrace(System.out);            handled = false;        }	    // Finishing session		if (Job.BATCHMODE)			System.exit(1);        return handled;    }    /**     * Handle Bean Shell evaluation errors.  Sends it to system.out.     * @param e the TargetError exception thrown.     * @param description a description of the event that caused the error to be thrown.     */    private static boolean handleBshError(Exception e, String description)    {        if (targetErrorClass.isInstance(e)) {            // The Bean Shell had an error            Throwable t = doGetTarget(e);            if (t != null) {                if (t instanceof VarContext.EvalException) {                    if (DEBUG) {                        System.out.println("Java EvalException: "+description+": "+t.getMessage());                        if (DEBUGSTACKTRACE) e.printStackTrace(System.out);                    }                } else {                    if (t.getMessage() != null)                        System.out.println(description+": "+t.getMessage());                    else if (t.getStackTrace() != null) {                        System.out.println(description+": ");                        t.printStackTrace(System.out);                    } else                        System.out.println(description+": "+t);                    if (DEBUGSTACKTRACE) e.printStackTrace(System.out);                }            }        } else {            System.out.println("Unhandled Java Bsh Exception: "+description+": "+e.getMessage());            if (DEBUGSTACKTRACE) e.printStackTrace(System.out);            return false;        }        return true;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -