MD5 校验值:195005882709ac21163d7a1b97aeec73
Interpreter.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.databySide.bsh; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.PrintStream; import java.io.Reader; import java.io.Serializable; import java.io.StringReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.lang3.CharUtils; public class Interpreter implements Runnable, ConsoleInterface, Serializable { public static boolean DEBUG = false; public static boolean LOCALSCOPING = false; public static boolean TRACE = false; public static final String VERSION = "2.0b4"; static Class array$Ljava$lang$String; static transient PrintStream debug; static This sharedObject; static String systemLineSeparator = "\n"; ConsoleInterface console; transient PrintStream err; protected boolean evalOnly; private boolean exitOnEOF; NameSpace globalNameSpace; transient Reader in; protected boolean interactive; transient PrintStream out; Interpreter parent; transient Parser parser; private boolean showResults; String sourceFileInfo; private boolean strictJava; static { staticInit(); } public Interpreter() { this(new StringReader(""), System.out, System.err, false, null); this.evalOnly = true; setu("com.databySide.bsh.evalOnly", new Primitive(true)); } public Interpreter(ConsoleInterface consoleInterface) { this(consoleInterface, null); } public Interpreter(ConsoleInterface consoleInterface, NameSpace nameSpace) { this(consoleInterface.getIn(), consoleInterface.getOut(), consoleInterface.getErr(), true, nameSpace); setConsole(consoleInterface); } public Interpreter(Reader reader, PrintStream printStream, PrintStream printStream2, boolean z) { this(reader, printStream, printStream2, z, null); } public Interpreter(Reader reader, PrintStream printStream, PrintStream printStream2, boolean z, NameSpace nameSpace) { this(reader, printStream, printStream2, z, nameSpace, null, null); } public Interpreter(Reader reader, PrintStream printStream, PrintStream printStream2, boolean z, NameSpace nameSpace, Interpreter interpreter, String str) { this.strictJava = false; this.exitOnEOF = true; this.parser = new Parser(reader); long currentTimeMillis = System.currentTimeMillis(); this.in = reader; this.out = printStream; this.err = printStream2; this.interactive = z; debug = printStream2; this.parent = interpreter; if (interpreter != null) { setStrictJava(interpreter.getStrictJava()); } this.sourceFileInfo = str; BshClassManager createClassManager = BshClassManager.createClassManager(this); if (nameSpace == null) { this.globalNameSpace = new NameSpace(createClassManager, "global"); } else { this.globalNameSpace = nameSpace; } if (!(getu("bsh") instanceof This)) { initRootSystemObject(); } if (z) { loadRCFiles(); } long currentTimeMillis2 = System.currentTimeMillis(); if (DEBUG) { debug(new StringBuffer().append("Time to initialize interpreter: ").append(currentTimeMillis2 - currentTimeMillis).toString()); } } private boolean Line() throws ParseException { return this.parser.Line(); } static Class class$(String str) { try { return Class.forName(str); } catch (ClassNotFoundException e) { throw new NoClassDefFoundError(e.getMessage()); } } public static final void debug(String str) { if (DEBUG) { debug.println(new StringBuffer().append("// Debug: ").append(str).toString()); } } private String getBshPrompt() { try { return (String) eval("getBshPrompt()"); } catch (Exception e) { return "bsh % "; } } private JavaCharStream get_jj_input_stream() { return this.parser.jj_input_stream; } private JJTParserState get_jjtree() { return this.parser.jjtree; } private void initRootSystemObject() { BshClassManager classManager = getClassManager(); setu("bsh", new NameSpace(classManager, "Bsh Object").getThis(this)); if (sharedObject == null) { sharedObject = new NameSpace(classManager, "Bsh Shared System Object").getThis(this); } setu("com.databySide.bsh.system", sharedObject); setu("com.databySide.bsh.shared", sharedObject); setu("com.databySide.bsh.help", new NameSpace(classManager, "Bsh Command Help Text").getThis(this)); try { setu("com.databySide.bsh.cwd", System.getProperty("user.dir")); } catch (SecurityException e) { setu("com.databySide.bsh.cwd", "."); } setu("com.databySide.bsh.interactive", new Primitive(this.interactive)); setu("com.databySide.bsh.evalOnly", new Primitive(this.evalOnly)); } public static void invokeMain(Class cls, String[] strArr) throws Exception { Class cls2; Class[] clsArr = new Class[1]; if (array$Ljava$lang$String == null) { Class class$ = class$("[Ljava.lang.String;"); cls2 = class$; array$Ljava$lang$String = class$; } else { cls2 = array$Ljava$lang$String; } clsArr[0] = cls2; Method resolveJavaMethod = Reflect.resolveJavaMethod(null, cls, "main", clsArr, true); if (resolveJavaMethod != null) { resolveJavaMethod.invoke(null, strArr); } } public static void main(String[] strArr) { String[] strArr2; if (strArr.length <= 0) { new Interpreter(new CommandLineReader(new InputStreamReader((System.getProperty("os.name").startsWith("Windows") && System.getProperty("java.version").startsWith("1.1.")) ? new FilterInputStream(System.in) { @Override public int available() throws IOException { return 0; } } : System.in)), System.out, System.err, true).run(); return; } String str = strArr[0]; if (strArr.length > 1) { strArr2 = new String[strArr.length - 1]; System.arraycopy(strArr, 1, strArr2, 0, strArr.length - 1); } else { strArr2 = new String[0]; } Interpreter interpreter = new Interpreter(); interpreter.setu("com.databySide.bsh.args", strArr2); try { Object source = interpreter.source(str, interpreter.globalNameSpace); if (source instanceof Class) { try { invokeMain((Class) source, strArr2); } catch (Exception e) { Object obj = e; if (e instanceof InvocationTargetException) { obj = ((InvocationTargetException) e).getTargetException(); } System.err.println(new StringBuffer().append("Class: ").append(source).append(" main method threw exception:").append(obj).toString()); } } } catch (TargetError e2) { System.out.println(new StringBuffer().append("Script threw exception: ").append(e2).toString()); if (e2.inNativeCode()) { e2.printStackTrace(DEBUG, System.err); } } catch (EvalError e3) { System.out.println(new StringBuffer().append("Evaluation Error: ").append(e3).toString()); } catch (FileNotFoundException e4) { System.out.println(new StringBuffer().append("File not found: ").append(e4).toString()); } catch (IOException e5) { System.out.println(new StringBuffer().append("I/O Error: ").append(e5).toString()); } } private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); if (this.console != null) { setOut(this.console.getOut()); setErr(this.console.getErr()); } else { setOut(System.out); setErr(System.err); } } public static void redirectOutputToFile(String str) { try { PrintStream printStream = new PrintStream(new FileOutputStream(str)); System.setOut(printStream); System.setErr(printStream); } catch (IOException e) { System.err.println(new StringBuffer().append("Can't redirect output to file: ").append(str).toString()); } } private String showEvalString(String str) { String replace = str.replace('\n', ' ').replace(CharUtils.CR, ' '); if (replace.length() > 80) { replace = new StringBuffer().append(replace.substring(0, 80)).append(" . . . ").toString(); } return replace; } static void staticInit() { try { systemLineSeparator = System.getProperty("line.separator"); debug = System.err; DEBUG = Boolean.getBoolean("debug"); TRACE = Boolean.getBoolean("trace"); LOCALSCOPING = Boolean.getBoolean("localscoping"); String property = System.getProperty("outfile"); if (property != null) { redirectOutputToFile(property); } } catch (SecurityException e) { System.err.println(new StringBuffer().append("Could not init static:").append(e).toString()); } catch (Exception e2) { System.err.println(new StringBuffer().append("Could not init static(2):").append(e2).toString()); } catch (Throwable th) { System.err.println(new StringBuffer().append("Could not init static(3):").append(th).toString()); } } @Override public final void error(Object obj) { if (this.console != null) { this.console.error(new StringBuffer().append("// Error: ").append(obj).append("\n").toString()); } else { this.err.println(new StringBuffer().append("// Error: ").append(obj).toString()); this.err.flush(); } } public Object eval(Reader reader) throws EvalError { return eval(reader, this.globalNameSpace, "eval stream"); } public Object eval(Reader reader, NameSpace nameSpace, String str) throws EvalError { Object obj = null; if (DEBUG) { debug(new StringBuffer().append("eval: nameSpace = ").append(nameSpace).toString()); } Interpreter interpreter = new Interpreter(reader, this.out, this.err, false, nameSpace, this, str); CallStack callStack = new CallStack(nameSpace); boolean z = false; while (true) { if (z) { break; } try { try { try { try { z = interpreter.Line(); if (interpreter.get_jjtree().nodeArity() > 0) { SimpleNode simpleNode = (SimpleNode) interpreter.get_jjtree().rootNode(); simpleNode.setSourceFile(str); if (TRACE) { println(new StringBuffer().append("// ").append(simpleNode.getText()).toString()); } obj = simpleNode.eval(callStack, interpreter); if (callStack.depth() > 1) { throw new InterpreterError(new StringBuffer().append("Callstack growing: ").append(callStack).toString()); } if (obj instanceof ReturnControl) { obj = ((ReturnControl) obj).value; interpreter.get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(nameSpace); } } else if (interpreter.showResults && obj != Primitive.VOID) { println(new StringBuffer().append("<").append(obj).append(">").toString()); } } interpreter.get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(nameSpace); } } catch (TargetError e) { if (e.getNode() == null) { e.setNode(null); } e.reThrow(new StringBuffer().append("Sourced file: ").append(str).toString()); interpreter.get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(nameSpace); } } catch (Exception e2) { if (DEBUG) { e2.printStackTrace(); } throw new EvalError(new StringBuffer().append("Sourced file: ").append(str).append(" unknown error: ").append(e2.getMessage()).toString(), null, callStack); } } catch (ParseException e3) { if (DEBUG) { error(e3.getMessage(DEBUG)); } e3.setErrorSourceFile(str); throw e3; } catch (EvalError e4) { if (DEBUG) { e4.printStackTrace(); } if (e4.getNode() == null) { e4.setNode(null); } e4.reThrow(new StringBuffer().append("Sourced file: ").append(str).toString()); interpreter.get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(nameSpace); } } } catch (InterpreterError e5) { e5.printStackTrace(); throw new EvalError(new StringBuffer().append("Sourced file: ").append(str).append(" internal Error: ").append(e5.getMessage()).toString(), null, callStack); } catch (TokenMgrError e6) { throw new EvalError(new StringBuffer().append("Sourced file: ").append(str).append(" Token Parsing Error: ").append(e6.getMessage()).toString(), null, callStack); } } catch (Throwable th) { interpreter.get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(nameSpace); } throw th; } } return Primitive.unwrap(obj); } public Object eval(String str) throws EvalError { if (DEBUG) { debug(new StringBuffer().append("eval(String): ").append(str).toString()); } return eval(str, this.globalNameSpace); } public Object eval(String str, NameSpace nameSpace) throws EvalError { String stringBuffer = str.endsWith(";") ? str : new StringBuffer().append(str).append(";").toString(); return eval(new StringReader(stringBuffer), nameSpace, new StringBuffer().append("inline evaluation of: ``").append(showEvalString(stringBuffer)).append("''").toString()); } public Object get(String str) throws EvalError { try { return Primitive.unwrap(this.globalNameSpace.get(str, this)); } catch (UtilEvalError e) { throw e.toEvalError(SimpleNode.JAVACODE, new CallStack()); } } public BshClassManager getClassManager() { return getNameSpace().getClassManager(); } @Override public PrintStream getErr() { return this.err; } @Override public Reader getIn() { return this.in; } public Object getInterface(Class cls) throws EvalError { try { return this.globalNameSpace.getThis(this).getInterface(cls); } catch (UtilEvalError e) { throw e.toEvalError(SimpleNode.JAVACODE, new CallStack()); } } public NameSpace getNameSpace() { return this.globalNameSpace; } @Override public PrintStream getOut() { return this.out; } public Interpreter getParent() { return this.parent; } public boolean getShowResults() { return this.showResults; } public String getSourceFileInfo() { return this.sourceFileInfo != null ? this.sourceFileInfo : "<unknown source>"; } public boolean getStrictJava() { return this.strictJava; } Object getu(String str) { try { return get(str); } catch (EvalError e) { throw new InterpreterError(new StringBuffer().append("set: ").append(e).toString()); } } void loadRCFiles() { try { source(new StringBuffer().append(System.getProperty("user.home")).append(File.separator).append(".bshrc").toString(), this.globalNameSpace); } catch (Exception e) { if (DEBUG) { debug(new StringBuffer().append("Could not find rc file: ").append(e).toString()); } } } public File pathToFile(String str) throws IOException { File file = new File(str); if (!file.isAbsolute()) { file = new File(new StringBuffer().append((String) getu("com.databySide.bsh.cwd")).append(File.separator).append(str).toString()); } return new File(file.getCanonicalPath()); } @Override public final void print(Object obj) { if (this.console != null) { this.console.print(obj); } else { this.out.print(obj); this.out.flush(); } } @Override public final void println(Object obj) { print(new StringBuffer().append(String.valueOf(obj)).append(systemLineSeparator).toString()); } @Override public void run() { if (this.evalOnly) { throw new RuntimeException("bsh Interpreter: No stream"); } if (this.interactive) { try { eval("printBanner();"); } catch (EvalError e) { println("BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)"); } } CallStack callStack = new CallStack(this.globalNameSpace); boolean z = false; while (!z) { try { try { try { try { System.out.flush(); System.err.flush(); Thread.yield(); if (this.interactive) { print(getBshPrompt()); } z = Line(); if (get_jjtree().nodeArity() > 0) { SimpleNode simpleNode = (SimpleNode) get_jjtree().rootNode(); if (DEBUG) { simpleNode.dump(">"); } Object eval = simpleNode.eval(callStack, this); if (callStack.depth() > 1) { throw new InterpreterError(new StringBuffer().append("Callstack growing: ").append(callStack).toString()); break; } if (eval instanceof ReturnControl) { eval = ((ReturnControl) eval).value; } if (eval != Primitive.VOID) { setu("$_", eval); if (this.showResults) { println(new StringBuffer().append("<").append(eval).append(">").toString()); } } } get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } catch (EvalError e2) { if (this.interactive) { error(new StringBuffer().append("EvalError: ").append(e2.toString()).toString()); } else { error(new StringBuffer().append("EvalError: ").append(e2.getMessage()).toString()); } if (DEBUG) { e2.printStackTrace(); } if (!this.interactive) { z = true; } get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } catch (Exception e3) { error(new StringBuffer().append("Unknown error: ").append(e3).toString()); if (DEBUG) { e3.printStackTrace(); } if (!this.interactive) { z = true; } get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } } catch (ParseException e4) { error(new StringBuffer().append("Parser Error: ").append(e4.getMessage(DEBUG)).toString()); if (DEBUG) { e4.printStackTrace(); } if (!this.interactive) { z = true; } this.parser.reInitInput(this.in); get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } catch (TargetError e5) { error(new StringBuffer().append("// Uncaught Exception: ").append(e5).toString()); if (e5.inNativeCode()) { e5.printStackTrace(DEBUG, this.err); } if (!this.interactive) { z = true; } setu("$_e", e5.getTarget()); get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } } catch (InterpreterError e6) { error(new StringBuffer().append("Internal Error: ").append(e6.getMessage()).toString()); e6.printStackTrace(); if (!this.interactive) { z = true; } get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } catch (TokenMgrError e7) { error(new StringBuffer().append("Error parsing input: ").append(e7).toString()); this.parser.reInitTokenInput(this.in); if (!this.interactive) { z = true; } get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } } } catch (Throwable th) { get_jjtree().reset(); if (callStack.depth() > 1) { callStack.clear(); callStack.push(this.globalNameSpace); } throw th; } } if (this.interactive && this.exitOnEOF) { System.exit(0); } } public void set(String str, double d) throws EvalError { set(str, new Primitive(d)); } public void set(String str, float f) throws EvalError { set(str, new Primitive(f)); } public void set(String str, int i) throws EvalError { set(str, new Primitive(i)); } public void set(String str, long j) throws EvalError { set(str, new Primitive(j)); } public void set(String str, Object obj) throws EvalError { Object obj2 = obj; if (obj2 == null) { obj2 = Primitive.NULL; } CallStack callStack = new CallStack(); try { if (Name.isCompound(str)) { this.globalNameSpace.getNameResolver(str).toLHS(callStack, this).assign(obj2, false); } else { this.globalNameSpace.setVariable(str, obj2, false); } } catch (UtilEvalError e) { throw e.toEvalError(SimpleNode.JAVACODE, callStack); } } public void set(String str, boolean z) throws EvalError { set(str, new Primitive(z)); } public void setClassLoader(ClassLoader classLoader) { getClassManager().setClassLoader(classLoader); } public void setConsole(ConsoleInterface consoleInterface) { this.console = consoleInterface; setu("com.databySide.bsh.console", consoleInterface); setOut(consoleInterface.getOut()); setErr(consoleInterface.getErr()); } public void setErr(PrintStream printStream) { this.err = printStream; } public void setExitOnEOF(boolean z) { this.exitOnEOF = z; } public void setNameSpace(NameSpace nameSpace) { this.globalNameSpace = nameSpace; } public void setOut(PrintStream printStream) { this.out = printStream; } public void setShowResults(boolean z) { this.showResults = z; } public void setStrictJava(boolean z) { this.strictJava = z; } void setu(String str, Object obj) { try { set(str, obj); } catch (EvalError e) { throw new InterpreterError(new StringBuffer().append("set: ").append(e).toString()); } } public Object source(String str) throws FileNotFoundException, IOException, EvalError { return source(str, this.globalNameSpace); } public Object source(String str, NameSpace nameSpace) throws FileNotFoundException, IOException, EvalError { File pathToFile = pathToFile(str); if (DEBUG) { debug(new StringBuffer().append("Sourcing file: ").append(pathToFile).toString()); } BufferedReader bufferedReader = new BufferedReader(new FileReader(pathToFile)); try { return eval(bufferedReader, nameSpace, str); } finally { bufferedReader.close(); } } public void unset(String str) throws EvalError { try { LHS lhs = this.globalNameSpace.getNameResolver(str).toLHS(new CallStack(), this); if (lhs.type != 0) { throw new EvalError(new StringBuffer().append("Can't unset, not a variable: ").append(str).toString(), SimpleNode.JAVACODE, new CallStack()); } lhs.nameSpace.unsetVariable(str); } catch (UtilEvalError e) { throw new EvalError(e.getMessage(), SimpleNode.JAVACODE, new CallStack()); } } }