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 {
37 private static final long serialVersionUID = -1142217552048425936L;
38 CoreException nestedCoreException;
41 * Creates a Java model exception that wrappers the given
42 * <code>Throwable</code>. The exception contains a Java-specific status
43 * object with severity <code>IStatus.ERROR</code> and the given status
47 * the <code>Throwable</code>
49 * one of the Java-specific status codes declared in
50 * <code>IJavaModelStatusConstants</code>
51 * @see IJavaModelStatusConstants
52 * @see org.eclipse.core.runtime.IStatus#ERROR
54 public JavaModelException(Throwable e, int code) {
55 this(new JavaModelStatus(code, e));
59 * Creates a Java model exception for the given <code>CoreException</code>.
61 * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
64 * the <code>CoreException</code>
66 public JavaModelException(CoreException exception) {
67 super(exception.getStatus());
68 this.nestedCoreException = exception;
72 * Creates a Java model exception for the given Java-specific status object.
75 * the Java-specific status object
77 public JavaModelException(IJavaModelStatus status) {
82 * Returns the underlying <code>Throwable</code> that caused the failure.
84 * @return the wrappered <code>Throwable</code>, or <code>null</code>
85 * if the direct case of the failure was at the Java model layer
87 public Throwable getException() {
88 if (this.nestedCoreException == null) {
89 return getStatus().getException();
91 return this.nestedCoreException;
96 * Returns the Java model status object for this exception. Equivalent to
97 * <code>(IJavaModelStatus) getStatus()</code>.
99 * @return a status object
101 public IJavaModelStatus getJavaModelStatus() {
102 IStatus status = this.getStatus();
103 if (status instanceof IJavaModelStatus) {
104 return (IJavaModelStatus) status;
106 // A regular IStatus is created only in the case of a CoreException.
107 // See bug 13492 Should handle JavaModelExceptions that contains
108 // CoreException more gracefully
109 return new JavaModelStatus(this.nestedCoreException);
114 * Returns whether this exception indicates that a Java model element does
115 * not exist. Such exceptions have a status with a code of
116 * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code>. This is
117 * a convenience method.
119 * @return <code>true</code> if this exception indicates that a Java model
120 * element does not exist
121 * @see IJavaModelStatus#isDoesNotExist
122 * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
124 public boolean isDoesNotExist() {
125 IJavaModelStatus javaModelStatus = getJavaModelStatus();
126 return javaModelStatus != null && javaModelStatus.isDoesNotExist();
130 * Returns a printable representation of this exception suitable for
131 * debugging purposes only.
133 public String toString() {
134 StringBuffer buffer = new StringBuffer();
135 buffer.append("Java Model Exception: "); //$NON-NLS-1$
136 if (getException() != null) {
137 if (getException() instanceof CoreException) {
138 CoreException c = (CoreException) getException();
139 buffer.append("Core Exception [code "); //$NON-NLS-1$
140 buffer.append(c.getStatus().getCode());
141 buffer.append("] "); //$NON-NLS-1$
142 buffer.append(c.getStatus().getMessage());
144 buffer.append(getException().toString());
147 buffer.append(getStatus().toString());
149 return buffer.toString();