7417c886c632d7208801656a4d3db214b3bc6158
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / JavaModelException.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.core;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15
16 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
17
18 /**
19  * A checked exception representing a failure in the Java model.
20  * Java model exceptions contain a Java-specific status object describing the
21  * cause of the exception.
22  * <p>
23  * This class is not intended to be subclassed by clients. Instances of this
24  * class are automatically created by the Java model when problems arise, so
25  * there is generally no need for clients to create instances.
26  * </p>
27  *
28  * @see IJavaModelStatus
29  * @see IJavaModelStatusConstants
30  */
31 public class JavaModelException extends CoreException {
32         CoreException nestedCoreException;
33 /**
34  * Creates a Java model exception that wrappers the given <code>Throwable</code>.
35  * The exception contains a Java-specific status object with severity
36  * <code>IStatus.ERROR</code> and the given status code.
37  *
38  * @param exception the <code>Throwable</code>
39  * @param code one of the Java-specific status codes declared in
40  *   <code>IJavaModelStatusConstants</code>
41  * @see IJavaModelStatusConstants
42  * @see org.eclipse.core.runtime.IStatus#ERROR
43  */
44 public JavaModelException(Throwable e, int code) {
45         this(new JavaModelStatus(code, e)); 
46 }
47 /**
48  * Creates a Java model exception for the given <code>CoreException</code>.
49  * Equivalent to 
50  * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
51  *
52  * @param exception the <code>CoreException</code>
53  */
54 public JavaModelException(CoreException exception) {
55         super(exception.getStatus());
56         this.nestedCoreException = exception;
57 }
58 /**
59  * Creates a Java model exception for the given Java-specific status object.
60  *
61  * @param status the Java-specific status object
62  */
63 public JavaModelException(IJavaModelStatus status) {
64         super(status);
65 }
66 /**
67  * Returns the underlying <code>Throwable</code> that caused the failure.
68  *
69  * @return the wrappered <code>Throwable</code>, or <code>null</code> if the
70  *   direct case of the failure was at the Java model layer
71  */
72 public Throwable getException() {
73         if (this.nestedCoreException == null) {
74                 return getStatus().getException();
75         } else {
76                 return this.nestedCoreException;
77         }
78 }
79 /**
80  * Returns the Java model status object for this exception.
81  * Equivalent to <code>(IJavaModelStatus) getStatus()</code>.
82  *
83  * @return a status object
84  */
85 public IJavaModelStatus getJavaModelStatus() {
86         IStatus status = this.getStatus();
87         if (status instanceof IJavaModelStatus) {
88                 return (IJavaModelStatus)status;
89         } else {
90                 // A regular IStatus is created only in the case of a CoreException.
91                 // See bug 13492 Should handle JavaModelExceptions that contains CoreException more gracefully  
92                 return new JavaModelStatus(this.nestedCoreException);
93         }
94 }
95 /**
96  * Returns whether this exception indicates that a Java model element does not
97  * exist. Such exceptions have a status with a code of
98  * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code>.
99  * This is a convenience method.
100  *
101  * @return <code>true</code> if this exception indicates that a Java model
102  *   element does not exist
103  * @see IJavaModelStatus#isDoesNotExist
104  * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
105  */
106 public boolean isDoesNotExist() {
107         IJavaModelStatus javaModelStatus = getJavaModelStatus();
108         return javaModelStatus != null && javaModelStatus.isDoesNotExist();
109 }
110 /**
111  * Returns a printable representation of this exception suitable for debugging
112  * purposes only.
113  */
114 public String toString() {
115         StringBuffer buffer= new StringBuffer();
116         buffer.append("Java Model Exception: "); //$NON-NLS-1$
117         if (getException() != null) {
118                 if (getException() instanceof CoreException) {
119                         CoreException c= (CoreException)getException();
120                         buffer.append("Core Exception [code "); //$NON-NLS-1$
121                         buffer.append(c.getStatus().getCode());
122                         buffer.append("] "); //$NON-NLS-1$
123                         buffer.append(c.getStatus().getMessage());
124                 } else {
125                         buffer.append(getException().toString());
126                 }
127         } else {
128                 buffer.append(getStatus().toString());
129         }
130         return buffer.toString();
131 }
132 }