001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2001,2002 Marcus Stursberg
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.compiler;
023
024/**
025 * Indicates a Failure to compile.
026 * 
027 * @author Marcus Stursberg
028 */
029public class CompilerException extends java.io.IOException
030{
031
032    static final long serialVersionUID = 6247426753392546734L;
033
034    /**
035     * The throwable that caused this throwable to get thrown, or null if this throwable was not
036     * caused by another throwable, or if the causative throwable is unknown. If this field is equal
037     * to this throwable itself, it indicates that the cause of this throwable has not yet been
038     * initialized.
039     */
040    private Throwable _cause = this;
041
042    /**
043     * Construct a new exception with the specified message.
044     * 
045     * @param message Description of the error
046     */
047    public CompilerException(String message)
048    {
049        super(message);
050    }
051
052    /**
053     * Construct a new exception with the specified message and wraps another cause.
054     * 
055     * @param message Description of the error
056     * @param cause Throwable
057     */
058    public CompilerException(String message, Throwable cause)
059    {
060        super(message);
061        this._cause = cause;
062    }
063
064    /**
065     * Initializes the <i>cause</i> of this throwable to the specified value. (The cause is the
066     * throwable that caused this throwable to get thrown.)
067     * 
068     * <p>
069     * This method can be called at most once. It is generally called from within the constructor,
070     * or immediately after creating the throwable. If this throwable was created with {@link
071     * #CompilerException(String,Throwable)}, this method cannot be called even once.
072     * 
073     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()}
074     * method). (A <code>null</code> value is permitted, and indicates that the cause is
075     * nonexistent or unknown.)
076     * @return a reference to this <code>Throwable</code> instance.
077     * @throws IllegalArgumentException if <code>cause</code> is this throwable. (A throwable
078     * cannot be its own cause.)
079     * @throws IllegalStateException if this throwable was created with {@link
080     * #CompilerException(String,Throwable)}, or this method has already been called on this
081     * throwable.
082     */
083    public synchronized Throwable initCause(Throwable cause)
084    {
085        if (this._cause != this) throw new IllegalStateException("Can't overwrite cause");
086        if (cause == this) throw new IllegalArgumentException("Self-causation not permitted");
087        this._cause = cause;
088        return this;
089    }
090
091    /**
092     * Returns the cause of this throwable or <code>null</code> if the cause is nonexistent or
093     * unknown. (The cause is the throwable that caused this throwable to get thrown.)
094     * 
095     * <p>
096     * This implementation returns the cause that was supplied via one of the constructors requiring
097     * a <code>Throwable</code>, or that was set after creation with the
098     * {@link #initCause(Throwable)} method. While it is typically unnecessary to override this
099     * method, a subclass can override it to return a cause set by some other means. This is
100     * appropriate for a "legacy chained throwable" that predates the addition of chained exceptions
101     * to <code>Throwable</code>. Note that it is <i>not</i> necessary to override any of the
102     * <code>PrintStackTrace</code> methods, all of which invoke the <code>getCause</code>
103     * method to determine the cause of a throwable.
104     * 
105     * @return the cause of this throwable or <code>null</code> if the cause is nonexistent or
106     * unknown.
107     */
108    public Throwable getCause()
109    {
110        return (_cause == this ? null : _cause);
111    }
112}