01: /*
02: * ReflectException.java --
03: *
04: * The file implements the handling of the Exception's caught
05: * while invoking the Reflection API.
06: *
07: * Copyright (c) 1997 Sun Microsystems, Inc.
08: *
09: * See the file "license.terms" for information on usage and
10: * redistribution of this file, and for a DISCLAIMER OF ALL
11: * WARRANTIES.
12: *
13: * RCS: @(#) $Id: ReflectException.java 3308 2006-06-15 18:54:14Z gbevin $
14: *
15: */
16: package tcl.lang;
17:
18: import java.lang.reflect.*;
19:
20: /**
21: * This class handles Exception's caught while invoking the Reflection
22: * API. It records the string form of the Exception into the result
23: * of the interpreter and stores the actual Exception object in the
24: * errorCode of the interpreter.
25: */
26:
27: public class ReflectException extends TclException {
28:
29: /*
30: *----------------------------------------------------------------------
31: *
32: * ReflectException --
33: *
34: * Records the string form of the Exception into the result
35: * of the interpreter and stores the actual Exception object in the
36: * errorCode of the interpreter.
37: *
38: * Results:
39: * None.
40: *
41: * Side effects:
42: * If interp is non-null, the interpreter result and errorCode
43: * are modified
44: *
45: *----------------------------------------------------------------------
46: */
47:
48: private static final long serialVersionUID = 4651756357452262833L;
49:
50: public ReflectException(Interp interp, // Current interpreter. May be null.
51: // If non-null, its result object and
52: // errorCode variable will be changed.
53: Throwable e) // The exception to record in the interp.
54: {
55: super (TCL.ERROR);
56: initCause(e);
57:
58: if (interp != null) {
59: Throwable t = e;
60: if (t instanceof InvocationTargetException) {
61: // The original exception is wrapped in InvocationTargetException
62: // for us by the Java Reflection API. This fact doesn't provide
63: // any interesting information to script writers, so we'll
64: // unwrap it so that is more convenient for scripts to
65: // figure out the exception.
66:
67: t = ((InvocationTargetException) t)
68: .getTargetException();
69: }
70:
71: TclObject errCode = TclList.newInstance();
72: errCode.preserve();
73:
74: try {
75: TclList.append(interp, errCode, TclString
76: .newInstance("JAVA"));
77: TclList.append(interp, errCode, ReflectObject
78: .newInstance(interp, Throwable.class, t));
79: } catch (TclException tclex) {
80: throw new TclRuntimeError("unexpected TclException: "
81: + tclex);
82: }
83:
84: // interp.setErrorCode() may fail silently if there is an bad
85: // trace on the "errorCode" variable. If that happens, the
86: // errCode list we created above may hang around
87: // forever. Hence, we added the pair of preserve() + release()
88: // calls to ensure that errCode will get cleaned up if
89: // interp.setErrorCode() fails.
90:
91: interp.setErrorCode(errCode);
92: errCode.release();
93:
94: interp.setResult(t.toString());
95: }
96: }
97:
98: } // end ReflectException
|