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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
18 //import org.eclipse.jdt.internal.core.JavaModelStatus;
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.
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.
30 * @see IJavaModelStatus
31 * @see IJavaModelStatusConstants
33 public class JavaModelException extends CoreException {
34 CoreException nestedCoreException;
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.
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
46 public JavaModelException(Throwable e, int code) {
47 this(new JavaModelStatus(code, e));
50 * Creates a Java model exception for the given <code>CoreException</code>.
52 * <code>JavaModelException(exception,IJavaModelStatusConstants.CORE_EXCEPTION</code>.
54 * @param exception the <code>CoreException</code>
56 public JavaModelException(CoreException exception) {
57 super(exception.getStatus());
58 this.nestedCoreException = exception;
61 * Creates a Java model exception for the given Java-specific status object.
63 * @param status the Java-specific status object
65 public JavaModelException(IJavaModelStatus status) {
69 * Returns the underlying <code>Throwable</code> that caused the failure.
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
74 public Throwable getException() {
75 if (this.nestedCoreException == null) {
76 return getStatus().getException();
78 return this.nestedCoreException;
82 * Returns the Java model status object for this exception.
83 * Equivalent to <code>(IJavaModelStatus) getStatus()</code>.
85 * @return a status object
87 public IJavaModelStatus getJavaModelStatus() {
88 IStatus status = this.getStatus();
89 if (status instanceof IJavaModelStatus) {
90 return (IJavaModelStatus)status;
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);
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.
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
108 //public boolean isDoesNotExist() {
109 // IJavaModelStatus javaModelStatus = getJavaModelStatus();
110 // return javaModelStatus != null && javaModelStatus.isDoesNotExist();
113 * Returns a printable representation of this exception suitable for debugging
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());
127 buffer.append(getException().toString());
130 buffer.append(getStatus().toString());
132 return buffer.toString();