A massive organize imports and formatting of the sources using default Eclipse code...
[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         CoreException nestedCoreException;
35
36         /**
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
40          * code.
41          * 
42          * @param exception
43          *            the <code>Throwable</code>
44          * @param 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
49          */
50         public JavaModelException(Throwable e, int code) {
51                 this(new JavaModelStatus(code, e));
52         }
53
54         /**
55          * Creates a Java model exception for the given <code>CoreException</code>.
56          * Equivalent to
57          * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
58          * 
59          * @param exception
60          *            the <code>CoreException</code>
61          */
62         public JavaModelException(CoreException exception) {
63                 super(exception.getStatus());
64                 this.nestedCoreException = exception;
65         }
66
67         /**
68          * Creates a Java model exception for the given Java-specific status object.
69          * 
70          * @param status
71          *            the Java-specific status object
72          */
73         public JavaModelException(IJavaModelStatus status) {
74                 super(status);
75         }
76
77         /**
78          * Returns the underlying <code>Throwable</code> that caused the failure.
79          * 
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
82          */
83         public Throwable getException() {
84                 if (this.nestedCoreException == null) {
85                         return getStatus().getException();
86                 } else {
87                         return this.nestedCoreException;
88                 }
89         }
90
91         /**
92          * Returns the Java model status object for this exception. Equivalent to
93          * <code>(IJavaModelStatus) getStatus()</code>.
94          * 
95          * @return a status object
96          */
97         public IJavaModelStatus getJavaModelStatus() {
98                 IStatus status = this.getStatus();
99                 if (status instanceof IJavaModelStatus) {
100                         return (IJavaModelStatus) status;
101                 } else {
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);
106                 }
107         }
108
109         /**
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.
114          * 
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
119          */
120         public boolean isDoesNotExist() {
121                 IJavaModelStatus javaModelStatus = getJavaModelStatus();
122                 return javaModelStatus != null && javaModelStatus.isDoesNotExist();
123         }
124
125         /**
126          * Returns a printable representation of this exception suitable for
127          * debugging purposes only.
128          */
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());
139                         } else {
140                                 buffer.append(getException().toString());
141                         }
142                 } else {
143                         buffer.append(getStatus().toString());
144                 }
145                 return buffer.toString();
146         }
147 }