1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / JavaModelException.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core;
12
13 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17
18 // import net.sourceforge.phpdt.internal.core.JavaModelStatus;
19
20 /**
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
23  * exception.
24  * <p>
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.
28  * </p>
29  * 
30  * @see IJavaModelStatus
31  * @see IJavaModelStatusConstants
32  */
33 public class JavaModelException extends CoreException {
34         /**
35          * 
36          */
37         private static final long serialVersionUID = -1142217552048425936L;
38         CoreException nestedCoreException;
39
40         /**
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
44          * code.
45          * 
46          * @param exception
47          *            the <code>Throwable</code>
48          * @param 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
53          */
54         public JavaModelException(Throwable e, int code) {
55                 this(new JavaModelStatus(code, e));
56         }
57
58         /**
59          * Creates a Java model exception for the given <code>CoreException</code>.
60          * Equivalent to
61          * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
62          * 
63          * @param exception
64          *            the <code>CoreException</code>
65          */
66         public JavaModelException(CoreException exception) {
67                 super(exception.getStatus());
68                 this.nestedCoreException = exception;
69         }
70
71         /**
72          * Creates a Java model exception for the given Java-specific status object.
73          * 
74          * @param status
75          *            the Java-specific status object
76          */
77         public JavaModelException(IJavaModelStatus status) {
78                 super(status);
79         }
80
81         /**
82          * Returns the underlying <code>Throwable</code> that caused the failure.
83          * 
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
86          */
87         public Throwable getException() {
88                 if (this.nestedCoreException == null) {
89                         return getStatus().getException();
90                 } else {
91                         return this.nestedCoreException;
92                 }
93         }
94
95         /**
96          * Returns the Java model status object for this exception. Equivalent to
97          * <code>(IJavaModelStatus) getStatus()</code>.
98          * 
99          * @return a status object
100          */
101         public IJavaModelStatus getJavaModelStatus() {
102                 IStatus status = this.getStatus();
103                 if (status instanceof IJavaModelStatus) {
104                         return (IJavaModelStatus) status;
105                 } else {
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);
110                 }
111         }
112
113         /**
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.
118          * 
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
123          */
124         public boolean isDoesNotExist() {
125                 IJavaModelStatus javaModelStatus = getJavaModelStatus();
126                 return javaModelStatus != null && javaModelStatus.isDoesNotExist();
127         }
128
129         /**
130          * Returns a printable representation of this exception suitable for
131          * debugging purposes only.
132          */
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());
143                         } else {
144                                 buffer.append(getException().toString());
145                         }
146                 } else {
147                         buffer.append(getStatus().toString());
148                 }
149                 return buffer.toString();
150         }
151 }