KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.debug.core.DebugException;
17 import org.eclipse.jdt.core.Signature;
18 import org.eclipse.jdt.debug.core.IJavaClassObject;
19 import org.eclipse.jdt.debug.core.IJavaFieldVariable;
20 import org.eclipse.jdt.debug.core.IJavaObject;
21 import org.eclipse.jdt.debug.core.IJavaReferenceType;
22
23 import com.ibm.icu.text.MessageFormat;
24 import com.sun.jdi.AbsentInformationException;
25 import com.sun.jdi.ArrayType;
26 import com.sun.jdi.ClassLoaderReference;
27 import com.sun.jdi.ClassNotLoadedException;
28 import com.sun.jdi.Field;
29 import com.sun.jdi.ReferenceType;
30 import com.sun.jdi.Type;
31 import com.sun.jdi.Value;
32
33 /**
34  * References a class, interface, or array type.
35  */

36 public abstract class JDIReferenceType extends JDIType implements IJavaReferenceType {
37     
38     // field names declared in this type
39
private String JavaDoc[] fDeclaredFields = null;
40     // field names declared in this type, super types, implemented interaces and superinterfaces
41
private String JavaDoc[] fAllFields = null;
42
43     /**
44      * Constructs a new reference type in the given target.
45      *
46      * @param target associated vm
47      * @param type reference type
48      */

49     public JDIReferenceType(JDIDebugTarget target, Type type) {
50         super(target, type);
51     }
52
53     /* (non-Javadoc)
54      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getAvailableStrata()
55      */

56     public String JavaDoc[] getAvailableStrata() {
57         List JavaDoc strata = getReferenceType().availableStrata();
58         return (String JavaDoc[])strata.toArray(new String JavaDoc[strata.size()]);
59     }
60
61     /**
62      * Returns the underlying reference type.
63      *
64      * @return the underlying reference type
65      */

66     protected ReferenceType getReferenceType() {
67         return (ReferenceType)getUnderlyingType();
68     }
69
70
71     /* (non-Javadoc)
72      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getDefaultStratum()
73      */

74     public String JavaDoc getDefaultStratum() throws DebugException {
75         try {
76             return getReferenceType().defaultStratum();
77         } catch (RuntimeException JavaDoc e) {
78             targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_1, e);
79         }
80         // exectution will not reach here
81
return null;
82     }
83     
84     /* (non-Javadoc)
85      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getField(java.lang.String)
86      */

87     public IJavaFieldVariable getField(String JavaDoc name) throws DebugException {
88         try {
89             ReferenceType type = (ReferenceType)getUnderlyingType();
90             Field field = type.fieldByName(name);
91             if (field != null && field.isStatic()) {
92                 return new JDIFieldVariable(getJavaDebugTarget(), field, type);
93             }
94         } catch (RuntimeException JavaDoc e) {
95             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_field, new String JavaDoc[] {e.toString(), name}), e);
96         }
97         // it is possible to return null
98
return null;
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getClassObject()
103      */

104     public IJavaClassObject getClassObject() throws DebugException {
105         try {
106             ReferenceType type= (ReferenceType)getUnderlyingType();
107             return (IJavaClassObject)JDIValue.createValue(getJavaDebugTarget(), type.classObject());
108         } catch (RuntimeException JavaDoc e) {
109             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_class_object, new String JavaDoc[] {e.toString()}), e);
110         }
111         // execution will not fall through to here,
112
// as #requestFailed will throw an exception
113
return null;
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getAllFieldNames()
118      */

119     public String JavaDoc[] getAllFieldNames() throws DebugException {
120         if (fAllFields == null) {
121             try {
122                 List JavaDoc fields = ((ReferenceType)getUnderlyingType()).allFields();
123                 fAllFields = new String JavaDoc[fields.size()];
124                 Iterator JavaDoc iterator = fields.iterator();
125                 int i = 0;
126                 while (iterator.hasNext()) {
127                     Field field = (Field)iterator.next();
128                     fAllFields[i] = field.name();
129                     i++;
130                 }
131             } catch (RuntimeException JavaDoc e) {
132                 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_2, e);
133             }
134         }
135         return fAllFields;
136     }
137
138     /* (non-Javadoc)
139      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getDeclaredFieldNames()
140      */

141     public String JavaDoc[] getDeclaredFieldNames() throws DebugException {
142         if (fDeclaredFields == null) {
143             try {
144                 List JavaDoc fields = ((ReferenceType)getUnderlyingType()).fields();
145                 fDeclaredFields = new String JavaDoc[fields.size()];
146                 Iterator JavaDoc iterator = fields.iterator();
147                 int i = 0;
148                 while (iterator.hasNext()) {
149                     Field field = (Field)iterator.next();
150                     fDeclaredFields[i] = field.name();
151                     i++;
152                 }
153             } catch (RuntimeException JavaDoc e) {
154                 targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_3, e);
155             }
156         }
157         return fDeclaredFields;
158     }
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getSourcePaths(java.lang.String)
162      */

163     public String JavaDoc[] getSourcePaths(String JavaDoc stratum) throws DebugException {
164         try {
165             List JavaDoc sourcePaths= getReferenceType().sourcePaths(stratum);
166             return (String JavaDoc[]) sourcePaths.toArray(new String JavaDoc[sourcePaths.size()]);
167         } catch (AbsentInformationException e) {
168         } catch (RuntimeException JavaDoc e) {
169             requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED);
170         }
171         return null;
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getSourceName()
176      */

177     public String JavaDoc getSourceName() throws DebugException {
178         try {
179             return getReferenceType().sourceName();
180         } catch (AbsentInformationException e) {
181         } catch (RuntimeException JavaDoc e) {
182             requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED);
183         }
184         return null;
185     }
186
187     /* (non-Javadoc)
188      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getSourceNames(java.lang.String)
189      */

190     public String JavaDoc[] getSourceNames(String JavaDoc stratum) throws DebugException {
191         try {
192             List JavaDoc sourceNames= getReferenceType().sourceNames(stratum);
193             return (String JavaDoc[]) sourceNames.toArray(new String JavaDoc[sourceNames.size()]);
194         } catch (AbsentInformationException e) {
195         } catch (RuntimeException JavaDoc e) {
196             requestFailed(JDIDebugModelMessages.JDIReferenceType_4, e, DebugException.TARGET_REQUEST_FAILED);
197         }
198         return null;
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getClassLoaderObject()
203      */

204     public IJavaObject getClassLoaderObject() throws DebugException {
205         try {
206             ReferenceType type= (ReferenceType)getUnderlyingType();
207             ClassLoaderReference classLoader = type.classLoader();
208             if (classLoader != null) {
209                 return (IJavaObject)JDIValue.createValue(getJavaDebugTarget(), classLoader);
210             }
211         } catch (RuntimeException JavaDoc e) {
212             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIReferenceType_0, new String JavaDoc[] {e.toString()}), e);
213         }
214         return null;
215     }
216     
217
218     static public String JavaDoc getGenericName(ReferenceType type) throws DebugException {
219         if (type instanceof ArrayType) {
220             try {
221                 Type componentType;
222                     componentType= ((ArrayType)type).componentType();
223                 if (componentType instanceof ReferenceType) {
224                     return getGenericName((ReferenceType)componentType) + "[]"; //$NON-NLS-1$
225
}
226                 return type.name();
227             } catch (ClassNotLoadedException e) {
228                 // we cannot create the generic name using the component type,
229
// just try to create one with the infos
230
}
231         }
232         String JavaDoc signature= type.signature();
233         StringBuffer JavaDoc res= new StringBuffer JavaDoc(getTypeName(signature));
234         String JavaDoc genericSignature= type.genericSignature();
235         if (genericSignature != null) {
236             String JavaDoc[] typeParameters= Signature.getTypeParameters(genericSignature);
237             if (typeParameters.length > 0) {
238                 res.append('<').append(Signature.getTypeVariable(typeParameters[0]));
239                 for (int i= 1; i < typeParameters.length; i++) {
240                     res.append(',').append(Signature.getTypeVariable(typeParameters[i]));
241                 }
242                 res.append('>');
243             }
244         }
245         return res.toString();
246     }
247     
248     /**
249      * Return the name from the given signature.
250      * Keep the '$' characters.
251      */

252     static public String JavaDoc getTypeName(String JavaDoc genericTypeSignature) {
253         int arrayDimension= 0;
254         while (genericTypeSignature.charAt(arrayDimension) == '[') {
255             arrayDimension++;
256         }
257         int parameterStart= genericTypeSignature.indexOf('<');
258         StringBuffer JavaDoc name= new StringBuffer JavaDoc();
259         if (parameterStart < 0) {
260             name.append(genericTypeSignature.substring(arrayDimension + 1, genericTypeSignature.length() - 1).replace('/', '.'));
261         } else {
262             name.append(genericTypeSignature.substring(arrayDimension + 1, parameterStart).replace('/', '.'));
263             name.append(Signature.toString(genericTypeSignature).substring(parameterStart - 1 - arrayDimension).replace('/', '.'));
264         }
265         for (int i= 0; i < arrayDimension; i++) {
266             name.append("[]"); //$NON-NLS-1$
267
}
268         return name.toString();
269     }
270
271     /* (non-Javadoc)
272      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getGenericSignature()
273      */

274     public String JavaDoc getGenericSignature() throws DebugException {
275         return getReferenceType().genericSignature();
276     }
277
278     /* (non-Javadoc)
279      * @see org.eclipse.jdt.debug.core.IJavaReferenceType#getInstances(long)
280      */

281     public IJavaObject[] getInstances(long max) throws DebugException {
282         try {
283             List JavaDoc list = getReferenceType().instances(max);
284             IJavaObject[] instances = new IJavaObject[list.size()];
285             for (int i = 0; i < instances.length; i++) {
286                 instances[i] = (IJavaObject) JDIValue.createValue(getJavaDebugTarget(), (Value) list.get(i));
287             }
288             return instances;
289         } catch (RuntimeException JavaDoc e) {
290             targetRequestFailed(JDIDebugModelMessages.JDIReferenceType_5, e);
291         }
292         return null;
293     }
294
295 }
296
Popular Tags