cc886275a21b9c1e0fc91a133f1e841046debd08
[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 org.eclipse.jdt.internal.core.JavaModelStatus;
19
20 /**
21  * A checked exception representing a failure in the Java model.
22  * Java model exceptions contain a Java-specific status object describing the
23  * cause of the 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  * Creates a Java model exception that wrappers the given <code>Throwable</code>.
37  * The exception contains a Java-specific status object with severity
38  * <code>IStatus.ERROR</code> and the given status code.
39  *
40  * @param exception the <code>Throwable</code>
41  * @param code one of the Java-specific status codes declared in
42  *   <code>IJavaModelStatusConstants</code>
43  * @see IJavaModelStatusConstants
44  * @see org.eclipse.core.runtime.IStatus#ERROR
45  */
46 public JavaModelException(Throwable e, int code) {
47         this(new JavaModelStatus(code, e)); 
48 }
49 /**
50  * Creates a Java model exception for the given <code>CoreException</code>.
51  * Equivalent to 
52  * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
53  *
54  * @param exception the <code>CoreException</code>
55  */
56 public JavaModelException(CoreException exception) {
57         super(exception.getStatus());
58         this.nestedCoreException = exception;
59 }
60 /**
61  * Creates a Java model exception for the given Java-specific status object.
62  *
63  * @param status the Java-specific status object
64  */
65 public JavaModelException(IJavaModelStatus status) {
66         super(status);
67 }
68 /**
69  * Returns the underlying <code>Throwable</code> that caused the failure.
70  *
71  * @return the wrappered <code>Throwable</code>, or <code>null</code> if the
72  *   direct case of the failure was at the Java model layer
73  */
74 public Throwable getException() {
75         if (this.nestedCoreException == null) {
76                 return getStatus().getException();
77         } else {
78                 return this.nestedCoreException;
79         }
80 }
81 /**
82  * Returns the Java model status object for this exception.
83  * Equivalent to <code>(IJavaModelStatus) getStatus()</code>.
84  *
85  * @return a status object
86  */
87 public IJavaModelStatus getJavaModelStatus() {
88         IStatus status = this.getStatus();
89         if (status instanceof IJavaModelStatus) {
90                 return (IJavaModelStatus)status;
91         } else {
92                 // A regular IStatus is created only in the case of a CoreException.
93                 // See bug 13492 Should handle JavaModelExceptions that contains CoreException more gracefully  
94                 return new JavaModelStatus(this.nestedCoreException);
95         }
96 }
97 /**
98  * Returns whether this exception indicates that a Java model element does not
99  * exist. Such exceptions have a status with a code of
100  * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code>.
101  * This is a convenience method.
102  *
103  * @return <code>true</code> if this exception indicates that a Java model
104  *   element does not exist
105  * @see IJavaModelStatus#isDoesNotExist
106  * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
107  */
108 public boolean isDoesNotExist() {
109         IJavaModelStatus javaModelStatus = getJavaModelStatus();
110         return javaModelStatus != null && javaModelStatus.isDoesNotExist();
111 }
112 /**
113  * Returns a printable representation of this exception suitable for debugging
114  * purposes only.
115  */
116 public String toString() {
117         StringBuffer buffer= new StringBuffer();
118         buffer.append("Java Model Exception: "); //$NON-NLS-1$
119         if (getException() != null) {
120                 if (getException() instanceof CoreException) {
121                         CoreException c= (CoreException)getException();
122                         buffer.append("Core Exception [code "); //$NON-NLS-1$
123                         buffer.append(c.getStatus().getCode());
124                         buffer.append("] "); //$NON-NLS-1$
125                         buffer.append(c.getStatus().getMessage());
126                 } else {
127                         buffer.append(getException().toString());
128                 }
129         } else {
130                 buffer.append(getStatus().toString());
131         }
132         return buffer.toString();
133 }
134 }