KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > model > JDIType


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.internal.debug.core.model;
12
13  
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.jdi.TimeoutException;
20 import org.eclipse.jdt.debug.core.IJavaType;
21 import org.eclipse.jdt.debug.core.JDIDebugModel;
22
23 import com.sun.jdi.ArrayType;
24 import com.sun.jdi.ClassType;
25 import com.sun.jdi.InterfaceType;
26 import com.sun.jdi.Type;
27
28 /**
29  * A type of an object or primitive data type in a debug target.
30  */

31 public class JDIType extends JDIDebugElement implements IJavaType {
32     
33     /**
34      * Underlying type on target VM
35      */

36     private Type fType;
37     
38     /**
39      * Constructs a new type based on the specified underlying
40      * type, in the given debug target
41      *
42      * @param target the debug target this type originated from
43      * @param type underlying type on the target VM
44      */

45     protected JDIType(JDIDebugTarget target, Type type) {
46         super(target);
47         setUnderlyingType(type);
48     }
49     
50     /**
51      * Throws a new debug exception with the given status code.
52      *
53      * @param message Failure message
54      * @param e Exception that has occurred (<code>can be null</code>)
55      * @param code status code
56      * @throws DebugException a new exception with given status code
57      */

58     public void requestFailed(String JavaDoc message, Throwable JavaDoc e, int code) throws DebugException {
59         throwDebugException(message, code, e);
60     }
61     
62     /**
63      * Throws a debug exception with the given message, error code, and underlying
64      * exception.
65      */

66     protected void throwDebugException(String JavaDoc message, int code, Throwable JavaDoc exception) throws DebugException {
67         throw new DebugException(new Status(IStatus.ERROR, JDIDebugModel.getPluginIdentifier(),
68             code, message, exception));
69     }
70     
71     /**
72      * Throws a new debug exception with a status code of <code>TARGET_REQUEST_FAILED</code>
73      * with the given underlying exception. If the underlying exception is not a JDI
74      * exception, the original exception is thrown.
75      *
76      * @param message Failure message
77      * @param e underlying exception that has occurred
78      * @throws DebugException The exception with a status code of <code>TARGET_REQUEST_FAILED</code>
79      */

80     public void targetRequestFailed(String JavaDoc message, RuntimeException JavaDoc e) throws DebugException {
81         if (e == null || e.getClass().getName().startsWith("com.sun.jdi") || e instanceof TimeoutException) { //$NON-NLS-1$
82
requestFailed(message, e, DebugException.TARGET_REQUEST_FAILED);
83         } else {
84             throw e;
85         }
86     }
87
88     /**
89      * Creates the appropriate kind of type, based on the specialized
90      * type.
91      */

92     public static JDIType createType(JDIDebugTarget target, Type type) {
93         if (type instanceof ArrayType) {
94             return new JDIArrayType(target, (ArrayType)type);
95         }
96         if (type instanceof ClassType) {
97             return new JDIClassType(target, (ClassType)type);
98         }
99         if (type instanceof InterfaceType) {
100             return new JDIInterfaceType(target, (InterfaceType)type);
101         }
102         return new JDIType(target, type);
103     }
104     
105     /**
106      * @see IJavaType#getSignature()
107      */

108     public String JavaDoc getSignature() throws DebugException {
109         try {
110             return getUnderlyingType().signature();
111         } catch (RuntimeException JavaDoc e) {
112             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIType_exception_while_retrieving_signature, new String JavaDoc[] {e.toString()}), e);
113             // execution will not reach this line as
114
// #targetRequestFailed will throw an exception
115
return null;
116         }
117     }
118
119     /**
120      * Returns the underlying type on the VM.
121      *
122      * @return the underlying type on the VM
123      */

124     public Type getUnderlyingType() {
125         return fType;
126     }
127
128     /**
129      * Sets the underlying type on the VM.
130      *
131      * @param type the underlying type on the VM
132      */

133     protected void setUnderlyingType(Type type) {
134         fType = type;
135     }
136     
137     /**
138      * @see java.lang.Object#toString()
139      */

140     public String JavaDoc toString() {
141         return getUnderlyingType().toString();
142     }
143     
144     /**
145      * @see IJavaType#getName()
146      */

147     public String JavaDoc getName() throws DebugException {
148         try {
149             return getUnderlyingType().name();
150         } catch (RuntimeException JavaDoc e) {
151             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIType_exception_while_retrieving_type_name, new String JavaDoc[]{e.toString()}), e);
152         }
153         // execution will not fall through as an exception
154
// will be thrown by the catch block
155
return null;
156     }
157     
158     /**
159      * @see java.lang.Object#equals(Object)
160      */

161     public boolean equals(Object JavaDoc object) {
162         return object instanceof JDIType && fType.equals(((JDIType)object).fType);
163     }
164
165     /**
166      * @see java.lang.Object#hashCode()
167      */

168     public int hashCode() {
169         return fType.hashCode();
170     }
171
172 }
173
Popular Tags