Home | History | Annotate | Download | only in javassist
      1 /*
      2  * Javassist, a Java-bytecode translator toolkit.
      3  * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
      4  *
      5  * The contents of this file are subject to the Mozilla Public License Version
      6  * 1.1 (the "License"); you may not use this file except in compliance with
      7  * the License.  Alternatively, the contents of this file may be used under
      8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
      9  *
     10  * Software distributed under the License is distributed on an "AS IS" basis,
     11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     12  * for the specific language governing rights and limitations under the
     13  * License.
     14  */
     15 
     16 package javassist;
     17 
     18 import javassist.compiler.CompileError;
     19 
     20 /**
     21  * Thrown when bytecode transformation has failed.
     22  */
     23 public class CannotCompileException extends Exception {
     24     private Throwable myCause;
     25 
     26     /**
     27      * Gets the cause of this throwable.
     28      * It is for JDK 1.3 compatibility.
     29      */
     30     public Throwable getCause() {
     31         return (myCause == this ? null : myCause);
     32     }
     33 
     34     /**
     35      * Initializes the cause of this throwable.
     36      * It is for JDK 1.3 compatibility.
     37      */
     38     public synchronized Throwable initCause(Throwable cause) {
     39         myCause = cause;
     40         return this;
     41     }
     42 
     43     private String message;
     44 
     45     /**
     46      * Gets a long message if it is available.
     47      */
     48     public String getReason() {
     49         if (message != null)
     50             return message;
     51         else
     52             return this.toString();
     53     }
     54 
     55     /**
     56      * Constructs a CannotCompileException with a message.
     57      *
     58      * @param msg       the message.
     59      */
     60     public CannotCompileException(String msg) {
     61         super(msg);
     62         message = msg;
     63         initCause(null);
     64     }
     65 
     66     /**
     67      * Constructs a CannotCompileException with an <code>Exception</code>
     68      * representing the cause.
     69      *
     70      * @param e     the cause.
     71      */
     72     public CannotCompileException(Throwable e) {
     73         super("by " + e.toString());
     74         message = null;
     75         initCause(e);
     76     }
     77 
     78     /**
     79      * Constructs a CannotCompileException with a detailed message
     80      * and an <code>Exception</code> representing the cause.
     81      *
     82      * @param msg   the message.
     83      * @param e     the cause.
     84      */
     85     public CannotCompileException(String msg, Throwable e) {
     86         this(msg);
     87         initCause(e);
     88     }
     89 
     90     /**
     91      * Constructs a CannotCompileException with a
     92      * <code>NotFoundException</code>.
     93      */
     94     public CannotCompileException(NotFoundException e) {
     95         this("cannot find " + e.getMessage(), e);
     96     }
     97 
     98     /**
     99      * 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 }
    120