001: /*****************************************************************************
002: * *
003: * This file is part of the BeanShell Java Scripting distribution. *
004: * Documentation and updates may be found at http://www.beanshell.org/ *
005: * *
006: * Sun Public License Notice: *
007: * *
008: * The contents of this file are subject to the Sun Public License Version *
009: * 1.0 (the "License"); you may not use this file except in compliance with *
010: * the License. A copy of the License is available at http://www.sun.com *
011: * *
012: * The Original Code is BeanShell. The Initial Developer of the Original *
013: * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014: * (C) 2000. All Rights Reserved. *
015: * *
016: * GNU Public License Notice: *
017: * *
018: * Alternatively, the contents of this file may be used under the terms of *
019: * the GNU Lesser General Public License (the "LGPL"), in which case the *
020: * provisions of LGPL are applicable instead of those above. If you wish to *
021: * allow use of your version of this file only under the terms of the LGPL *
022: * and not to allow others to use your version of this file under the SPL, *
023: * indicate your decision by deleting the provisions above and replace *
024: * them with the notice and other provisions required by the LGPL. If you *
025: * do not delete the provisions above, a recipient may use your version of *
026: * this file under either the SPL or the LGPL. *
027: * *
028: * Patrick Niemeyer (pat@pat.net) *
029: * Author of Learning Java, O'Reilly & Associates *
030: * http://www.pat.net/~pat/ *
031: * *
032: *****************************************************************************/package bsh;
033:
034: import java.lang.reflect.InvocationTargetException;
035:
036: class BSHMethodInvocation extends SimpleNode {
037: BSHMethodInvocation(int id) {
038: super (id);
039: }
040:
041: BSHAmbiguousName getNameNode() {
042: return (BSHAmbiguousName) jjtGetChild(0);
043: }
044:
045: BSHArguments getArgsNode() {
046: return (BSHArguments) jjtGetChild(1);
047: }
048:
049: /**
050: Evaluate the method invocation with the specified callstack and
051: interpreter
052: */
053: public Object eval(CallStack callstack, Interpreter interpreter)
054: throws EvalError {
055: NameSpace namespace = callstack.top();
056: BSHAmbiguousName nameNode = getNameNode();
057:
058: // Do not evaluate methods this() or super() in class instance space
059: // (i.e. inside a constructor)
060: if (namespace.getParent() != null
061: && namespace.getParent().isClass
062: && (nameNode.text.equals("super") || nameNode.text
063: .equals("this")))
064: return Primitive.VOID;
065:
066: Name name = nameNode.getName(namespace);
067: Object[] args = getArgsNode().getArguments(callstack,
068: interpreter);
069:
070: // This try/catch block is replicated is BSHPrimarySuffix... need to
071: // factor out common functionality...
072: // Move to Reflect?
073: try {
074: return name
075: .invokeMethod(interpreter, args, callstack, this );
076: } catch (ReflectError e) {
077: throw new EvalError("Error in method invocation: "
078: + e.getMessage(), this , callstack);
079: } catch (InvocationTargetException e) {
080: String msg = "Method Invocation " + name;
081: Throwable te = e.getTargetException();
082:
083: /*
084: Try to squeltch the native code stack trace if the exception
085: was caused by a reflective call back into the bsh interpreter
086: (e.g. eval() or source()
087: */
088: boolean isNative = true;
089: if (te instanceof EvalError)
090: if (te instanceof TargetError)
091: isNative = ((TargetError) te).inNativeCode();
092: else
093: isNative = false;
094:
095: throw new TargetError(msg, te, this , callstack, isNative);
096: } catch (UtilEvalError e) {
097: throw e.toEvalError(this, callstack);
098: }
099: }
100: }
|