1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
16 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
19 * A checked exception representing a failure in the Java model.
20 * Java model exceptions contain a Java-specific status object describing the
21 * cause of the exception.
23 * This class is not intended to be subclassed by clients. Instances of this
24 * class are automatically created by the Java model when problems arise, so
25 * there is generally no need for clients to create instances.
28 * @see IJavaModelStatus
29 * @see IJavaModelStatusConstants
31 public class JavaModelException extends CoreException {
32 CoreException nestedCoreException;
34 * Creates a Java model exception that wrappers the given <code>Throwable</code>.
35 * The exception contains a Java-specific status object with severity
36 * <code>IStatus.ERROR</code> and the given status code.
38 * @param exception the <code>Throwable</code>
39 * @param code one of the Java-specific status codes declared in
40 * <code>IJavaModelStatusConstants</code>
41 * @see IJavaModelStatusConstants
42 * @see org.eclipse.core.runtime.IStatus#ERROR
44 public JavaModelException(Throwable e, int code) {
45 this(new JavaModelStatus(code, e));
48 * Creates a Java model exception for the given <code>CoreException</code>.
50 * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
52 * @param exception the <code>CoreException</code>
54 public JavaModelException(CoreException exception) {
55 super(exception.getStatus());
56 this.nestedCoreException = exception;
59 * Creates a Java model exception for the given Java-specific status object.
61 * @param status the Java-specific status object
63 public JavaModelException(IJavaModelStatus status) {
67 * Returns the underlying <code>Throwable</code> that caused the failure.
69 * @return the wrappered <code>Throwable</code>, or <code>null</code> if the
70 * direct case of the failure was at the Java model layer
72 public Throwable getException() {
73 if (this.nestedCoreException == null) {
74 return getStatus().getException();
76 return this.nestedCoreException;
80 * Returns the Java model status object for this exception.
81 * Equivalent to <code>(IJavaModelStatus) getStatus()</code>.
83 * @return a status object
85 public IJavaModelStatus getJavaModelStatus() {
86 IStatus status = this.getStatus();
87 if (status instanceof IJavaModelStatus) {
88 return (IJavaModelStatus)status;
90 // A regular IStatus is created only in the case of a CoreException.
91 // See bug 13492 Should handle JavaModelExceptions that contains CoreException more gracefully
92 return new JavaModelStatus(this.nestedCoreException);
96 * Returns whether this exception indicates that a Java model element does not
97 * exist. Such exceptions have a status with a code of
98 * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code>.
99 * This is a convenience method.
101 * @return <code>true</code> if this exception indicates that a Java model
102 * element does not exist
103 * @see IJavaModelStatus#isDoesNotExist
104 * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
106 public boolean isDoesNotExist() {
107 IJavaModelStatus javaModelStatus = getJavaModelStatus();
108 return javaModelStatus != null && javaModelStatus.isDoesNotExist();
111 * Returns a printable representation of this exception suitable for debugging
114 public String toString() {
115 StringBuffer buffer= new StringBuffer();
116 buffer.append("Java Model Exception: "); //$NON-NLS-1$
117 if (getException() != null) {
118 if (getException() instanceof CoreException) {
119 CoreException c= (CoreException)getException();
120 buffer.append("Core Exception [code "); //$NON-NLS-1$
121 buffer.append(c.getStatus().getCode());
122 buffer.append("] "); //$NON-NLS-1$
123 buffer.append(c.getStatus().getMessage());
125 buffer.append(getException().toString());
128 buffer.append(getStatus().toString());
130 return buffer.toString();