1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
18 // import net.sourceforge.phpdt.internal.core.JavaModelStatus;
21 * A checked exception representing a failure in the Java model. Java model
22 * exceptions contain a Java-specific status object describing the cause of the
25 * This class is not intended to be subclassed by clients. Instances of this
26 * class are automatically created by the Java model when problems arise, so
27 * there is generally no need for clients to create instances.
30 * @see IJavaModelStatus
31 * @see IJavaModelStatusConstants
33 public class JavaModelException extends CoreException {
34 CoreException nestedCoreException;
37 * Creates a Java model exception that wrappers the given
38 * <code>Throwable</code>. The exception contains a Java-specific status
39 * object with severity <code>IStatus.ERROR</code> and the given status
43 * the <code>Throwable</code>
45 * one of the Java-specific status codes declared in
46 * <code>IJavaModelStatusConstants</code>
47 * @see IJavaModelStatusConstants
48 * @see org.eclipse.core.runtime.IStatus#ERROR
50 public JavaModelException(Throwable e, int code) {
51 this(new JavaModelStatus(code, e));
55 * Creates a Java model exception for the given <code>CoreException</code>.
57 * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
60 * the <code>CoreException</code>
62 public JavaModelException(CoreException exception) {
63 super(exception.getStatus());
64 this.nestedCoreException = exception;
68 * Creates a Java model exception for the given Java-specific status object.
71 * the Java-specific status object
73 public JavaModelException(IJavaModelStatus status) {
78 * Returns the underlying <code>Throwable</code> that caused the failure.
80 * @return the wrappered <code>Throwable</code>, or <code>null</code>
81 * if the direct case of the failure was at the Java model layer
83 public Throwable getException() {
84 if (this.nestedCoreException == null) {
85 return getStatus().getException();
87 return this.nestedCoreException;
92 * Returns the Java model status object for this exception. Equivalent to
93 * <code>(IJavaModelStatus) getStatus()</code>.
95 * @return a status object
97 public IJavaModelStatus getJavaModelStatus() {
98 IStatus status = this.getStatus();
99 if (status instanceof IJavaModelStatus) {
100 return (IJavaModelStatus) status;
102 // A regular IStatus is created only in the case of a CoreException.
103 // See bug 13492 Should handle JavaModelExceptions that contains
104 // CoreException more gracefully
105 return new JavaModelStatus(this.nestedCoreException);
110 * Returns whether this exception indicates that a Java model element does
111 * not exist. Such exceptions have a status with a code of
112 * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code>. This is
113 * a convenience method.
115 * @return <code>true</code> if this exception indicates that a Java model
116 * element does not exist
117 * @see IJavaModelStatus#isDoesNotExist
118 * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
120 public boolean isDoesNotExist() {
121 IJavaModelStatus javaModelStatus = getJavaModelStatus();
122 return javaModelStatus != null && javaModelStatus.isDoesNotExist();
126 * Returns a printable representation of this exception suitable for
127 * debugging purposes only.
129 public String toString() {
130 StringBuffer buffer = new StringBuffer();
131 buffer.append("Java Model Exception: "); //$NON-NLS-1$
132 if (getException() != null) {
133 if (getException() instanceof CoreException) {
134 CoreException c = (CoreException) getException();
135 buffer.append("Core Exception [code "); //$NON-NLS-1$
136 buffer.append(c.getStatus().getCode());
137 buffer.append("] "); //$NON-NLS-1$
138 buffer.append(c.getStatus().getMessage());
140 buffer.append(getException().toString());
143 buffer.append(getStatus().toString());
145 return buffer.toString();