001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist;
017:
018: import javassist.compiler.CompileError;
019:
020: /**
021: * Thrown when bytecode transformation has failed.
022: */
023: public class CannotCompileException extends Exception {
024: private Throwable myCause;
025:
026: /**
027: * Gets the cause of this throwable.
028: * It is for JDK 1.3 compatibility.
029: */
030: public Throwable getCause() {
031: return (myCause == this ? null : myCause);
032: }
033:
034: /**
035: * Initializes the cause of this throwable.
036: * It is for JDK 1.3 compatibility.
037: */
038: public synchronized Throwable initCause(Throwable cause) {
039: myCause = cause;
040: return this ;
041: }
042:
043: private String message;
044:
045: /**
046: * Gets a long message if it is available.
047: */
048: public String getReason() {
049: if (message != null)
050: return message;
051: else
052: return this .toString();
053: }
054:
055: /**
056: * Constructs a CannotCompileException with a message.
057: *
058: * @param msg the message.
059: */
060: public CannotCompileException(String msg) {
061: super (msg);
062: message = msg;
063: initCause(null);
064: }
065:
066: /**
067: * Constructs a CannotCompileException with an <code>Exception</code>
068: * representing the cause.
069: *
070: * @param e the cause.
071: */
072: public CannotCompileException(Throwable e) {
073: super ("by " + e.toString());
074: message = null;
075: initCause(e);
076: }
077:
078: /**
079: * Constructs a CannotCompileException with a detailed message
080: * and an <code>Exception</code> representing the cause.
081: *
082: * @param msg the message.
083: * @param e the cause.
084: */
085: public CannotCompileException(String msg, Throwable e) {
086: this (msg);
087: initCause(e);
088: }
089:
090: /**
091: * Constructs a CannotCompileException with a
092: * <code>NotFoundException</code>.
093: */
094: public CannotCompileException(NotFoundException e) {
095: this ("cannot find " + e.getMessage(), e);
096: }
097:
098: /**
099: * Constructs a CannotCompileException with an <code>CompileError</code>.
100: */
101: public CannotCompileException(CompileError e) {
102: this ("[source error] " + e.getMessage(), e);
103: }
104:
105: /**
106: * Constructs a CannotCompileException
107: * with a <code>ClassNotFoundException</code>.
108: */
109: public CannotCompileException(ClassNotFoundException e, String name) {
110: this ("cannot find " + name, e);
111: }
112:
113: /**
114: * Constructs a CannotCompileException with a ClassFormatError.
115: */
116: public CannotCompileException(ClassFormatError e, String name) {
117: this ("invalid class format: " + name, e);
118: }
119: }
|