KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > JavaModelException


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core;
12
13 import java.io.PrintStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.jdt.internal.core.JavaModelStatus;
20
21 /**
22  * A checked exception representing a failure in the Java model.
23  * Java model exceptions contain a Java-specific status object describing the
24  * cause of the exception.
25  * <p>
26  * This class is not intended to be subclassed by clients. Instances of this
27  * class are automatically created by the Java model when problems arise, so
28  * there is generally no need for clients to create instances.
29  * </p>
30  *
31  * @see IJavaModelStatus
32  * @see IJavaModelStatusConstants
33  */

34 public class JavaModelException extends CoreException {
35
36     private static final long serialVersionUID = -760398656505871287L; // backward compatible
37

38     CoreException nestedCoreException;
39 /**
40  * Creates a Java model exception that wrappers the given <code>Throwable</code>.
41  * The exception contains a Java-specific status object with severity
42  * <code>IStatus.ERROR</code> and the given status code.
43  *
44  * @param e the <code>Throwable</code>
45  * @param code 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 JavaDoc e, int code) {
51     this(new JavaModelStatus(code, e));
52 }
53 /**
54  * Creates a Java model exception for the given <code>CoreException</code>.
55  * Equivalent to
56  * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
57  *
58  * @param exception the <code>CoreException</code>
59  */

60 public JavaModelException(CoreException exception) {
61     super(exception.getStatus());
62     this.nestedCoreException = exception;
63 }
64 /**
65  * Creates a Java model exception for the given Java-specific status object.
66  *
67  * @param status the Java-specific status object
68  */

69 public JavaModelException(IJavaModelStatus status) {
70     super(status);
71 }
72 /**
73  * Returns the underlying <code>Throwable</code> that caused the failure.
74  *
75  * @return the wrappered <code>Throwable</code>, or <code>null</code> if the
76  * direct case of the failure was at the Java model layer
77  */

78 public Throwable JavaDoc getException() {
79     if (this.nestedCoreException == null) {
80         return getStatus().getException();
81     } else {
82         return this.nestedCoreException;
83     }
84 }
85 /**
86  * Returns the Java model status object for this exception.
87  * Equivalent to <code>(IJavaModelStatus) getStatus()</code>.
88  *
89  * @return a status object
90  */

91 public IJavaModelStatus getJavaModelStatus() {
92     IStatus status = this.getStatus();
93     if (status instanceof IJavaModelStatus) {
94         return (IJavaModelStatus)status;
95     } else {
96         // A regular IStatus is created only in the case of a CoreException.
97
// See bug 13492 Should handle JavaModelExceptions that contains CoreException more gracefully
98
return new JavaModelStatus(this.nestedCoreException);
99     }
100 }
101 /**
102  * Returns whether this exception indicates that a Java model element does not
103  * exist. Such exceptions have a status with a code of
104  * <code>IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST</code> or
105  * <code>IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH</code>.
106  * This is a convenience method.
107  *
108  * @return <code>true</code> if this exception indicates that a Java model
109  * element does not exist
110  * @see IJavaModelStatus#isDoesNotExist()
111  * @see IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST
112  * @see IJavaModelStatusConstants#ELEMENT_NOT_ON_CLASSPATH
113  */

114 public boolean isDoesNotExist() {
115     IJavaModelStatus javaModelStatus = getJavaModelStatus();
116     return javaModelStatus != null && javaModelStatus.isDoesNotExist();
117 }
118
119 /**
120  * Prints this exception's stack trace to the given print stream.
121  *
122  * @param output the print stream
123  * @since 3.0
124  */

125 public void printStackTrace(PrintStream JavaDoc output) {
126     synchronized(output) {
127         super.printStackTrace(output);
128         Throwable JavaDoc throwable = getException();
129         if (throwable != null) {
130             output.print("Caused by: "); //$NON-NLS-1$
131
throwable.printStackTrace(output);
132         }
133     }
134 }
135
136 /**
137  * Prints this exception's stack trace to the given print writer.
138  *
139  * @param output the print writer
140  * @since 3.0
141  */

142 public void printStackTrace(PrintWriter JavaDoc output) {
143     synchronized(output) {
144         super.printStackTrace(output);
145         Throwable JavaDoc throwable = getException();
146         if (throwable != null) {
147             output.print("Caused by: "); //$NON-NLS-1$
148
throwable.printStackTrace(output);
149         }
150     }
151 }
152 /*
153  * Returns a printable representation of this exception suitable for debugging
154  * purposes only.
155  */

156 public String JavaDoc toString() {
157     StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
158     buffer.append("Java Model Exception: "); //$NON-NLS-1$
159
if (getException() != null) {
160         if (getException() instanceof CoreException) {
161             CoreException c= (CoreException)getException();
162             buffer.append("Core Exception [code "); //$NON-NLS-1$
163
buffer.append(c.getStatus().getCode());
164             buffer.append("] "); //$NON-NLS-1$
165
buffer.append(c.getStatus().getMessage());
166         } else {
167             buffer.append(getException().toString());
168         }
169     } else {
170         buffer.append(getStatus().toString());
171     }
172     return buffer.toString();
173 }
174 }
175
Popular Tags