001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino.util;
035:
036: import java.io.*;
037: import java.lang.reflect.*;
038:
039: /**
040: * For compatibility with pre-1.4 JDKs, this class mimics
041: */
042: public class CausedException extends Exception {
043: private Throwable optionalCause = null;
044: private static Method INIT_CAUSE; // Null for pre-1.4 JDKs.
045: static {
046: try {
047: CausedException.INIT_CAUSE = Exception.class
048: .getDeclaredMethod("initCause",
049: new Class[] { Throwable.class });
050: } catch (NoSuchMethodException e) {
051: CausedException.INIT_CAUSE = null;
052: }
053: }
054:
055: public CausedException() {
056: }
057:
058: public CausedException(String message) {
059: super (message);
060: }
061:
062: public CausedException(String message, Throwable optionalCause) {
063: super (message);
064: this .initCause(optionalCause);
065: }
066:
067: public CausedException(Throwable optionalCause) {
068: super (optionalCause == null ? null : optionalCause.getMessage());
069: this .initCause(optionalCause);
070: }
071:
072: public Throwable initCause(Throwable optionalCause) {
073: if (CausedException.INIT_CAUSE == null) {
074: this .optionalCause = optionalCause;
075: } else {
076: try {
077: CausedException.INIT_CAUSE.invoke(this ,
078: new Object[] { optionalCause });
079: } catch (IllegalArgumentException e) {
080: throw new RuntimeException("Calling \"initCause()\"");
081: } catch (IllegalAccessException e) {
082: throw new RuntimeException("Calling \"initCause()\"");
083: } catch (InvocationTargetException e) {
084: throw new RuntimeException("Calling \"initCause()\"");
085: }
086: }
087: return this ;
088: }
089:
090: public Throwable getCause() {
091: return this .optionalCause;
092: }
093:
094: public void printStackTrace(PrintStream ps) {
095: super .printStackTrace(ps);
096: if (this .optionalCause == null)
097: return;
098:
099: ps.print("Caused by: ");
100: this .optionalCause.printStackTrace(ps);
101: }
102:
103: public void printStackTrace(PrintWriter pw) {
104: super .printStackTrace(pw);
105: if (this .optionalCause == null)
106: return;
107:
108: pw.print("Caused by: ");
109: this.optionalCause.printStackTrace(pw);
110: }
111: }
|