KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.debug.core.DebugException;
21 import org.eclipse.jdt.debug.core.IJavaClassType;
22 import org.eclipse.jdt.debug.core.IJavaInterfaceType;
23 import org.eclipse.jdt.debug.core.IJavaObject;
24 import org.eclipse.jdt.debug.core.IJavaThread;
25 import org.eclipse.jdt.debug.core.IJavaValue;
26
27 import com.sun.jdi.ClassType;
28 import com.sun.jdi.InterfaceType;
29 import com.sun.jdi.Method;
30 import com.sun.jdi.ObjectReference;
31 import com.sun.jdi.Value;
32
33 /**
34  * The class of an object in a debug target.
35  */

36 public class JDIClassType extends JDIReferenceType implements IJavaClassType {
37     
38     /**
39      * Cosntructs a new class type on the given target referencing
40      * the specified class type.
41      */

42     public JDIClassType(JDIDebugTarget target, ClassType type) {
43         super(target, type);
44     }
45
46     /* (non-Javadoc)
47      * @see org.eclipse.jdt.debug.core.IJavaClassType#newInstance(java.lang.String, org.eclipse.jdt.debug.core.IJavaValue[], org.eclipse.jdt.debug.core.IJavaThread)
48      */

49     public IJavaObject newInstance(String JavaDoc signature, IJavaValue[] args, IJavaThread thread) throws DebugException {
50         if (getUnderlyingType() instanceof ClassType) {
51             ClassType clazz = (ClassType)getUnderlyingType();
52             JDIThread javaThread = (JDIThread)thread;
53             List JavaDoc arguments = convertArguments(args);
54             Method method = null;
55             try {
56                 List JavaDoc methods = clazz.methodsByName("<init>", signature); //$NON-NLS-1$
57
if (methods.isEmpty()) {
58                     requestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_Type_does_not_implement_cosntructor, new String JavaDoc[]{signature}), null);
59                 } else {
60                     method = (Method)methods.get(0);
61                 }
62             } catch (RuntimeException JavaDoc e) {
63                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_performing_method_lookup_for_constructor, new String JavaDoc[] {e.toString(), signature}), e);
64             }
65             ObjectReference result = javaThread.newInstance(clazz, method, arguments);
66             return (IJavaObject)JDIValue.createValue(getJavaDebugTarget(), result);
67         }
68         requestFailed(JDIDebugModelMessages.JDIClassType_Type_is_not_a_class_type, null);
69         // execution will not fall through to here,
70
// as #requestFailed will throw an exception
71
return null;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.jdt.debug.core.IJavaClassType#sendMessage(java.lang.String, java.lang.String, org.eclipse.jdt.debug.core.IJavaValue[], org.eclipse.jdt.debug.core.IJavaThread)
76      */

77     public IJavaValue sendMessage(String JavaDoc selector, String JavaDoc signature, IJavaValue[] args, IJavaThread thread) throws DebugException {
78         if (getUnderlyingType() instanceof ClassType) {
79             ClassType clazz = (ClassType)getUnderlyingType();
80             JDIThread javaThread = (JDIThread)thread;
81             List JavaDoc arguments = convertArguments(args);
82             Method method = null;
83             try {
84                 List JavaDoc methods = clazz.methodsByName(selector, signature);
85                 if (methods.isEmpty()) {
86                     requestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_Type_does_not_implement_selector, new String JavaDoc[] {selector, signature}), null);
87                 } else {
88                     method = (Method)methods.get(0);
89                 }
90             } catch (RuntimeException JavaDoc e) {
91                 targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_performing_method_lookup_for_selector, new String JavaDoc[] {e.toString(), selector, signature}), e);
92             }
93             Value result = javaThread.invokeMethod(clazz, null, method, arguments, false);
94             return JDIValue.createValue(getJavaDebugTarget(), result);
95         }
96         requestFailed(JDIDebugModelMessages.JDIClassType_Type_is_not_a_class_type, null);
97         // execution will not fall through to here,
98
// as #requestFailed will throw an exception
99
return null;
100     }
101     
102     /**
103      * Utility method to convert argument array to an
104      * argument list.
105      *
106      * @param args array of arguments, as <code>IJavaValue</code>s,
107      * possibly <code>null</code> or empty
108      * @return a list of underlying <code>Value</code>s
109      */

110     protected List JavaDoc convertArguments(IJavaValue[] args) {
111         List JavaDoc arguments = null;
112         if (args == null) {
113             arguments = Collections.EMPTY_LIST;
114         } else {
115             arguments= new ArrayList JavaDoc(args.length);
116             for (int i = 0; i < args.length; i++) {
117                 arguments.add(((JDIValue)args[i]).getUnderlyingValue());
118             }
119         }
120         return arguments;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jdt.debug.core.IJavaClassType#getSuperclass()
125      */

126     public IJavaClassType getSuperclass() throws DebugException {
127         try {
128             ClassType superclazz = ((ClassType)getUnderlyingType()).superclass();
129             if (superclazz != null) {
130                 return (IJavaClassType)JDIType.createType(getJavaDebugTarget(), superclazz);
131             }
132         } catch (RuntimeException JavaDoc e) {
133             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_superclass, new String JavaDoc[] {e.toString()}), e);
134         }
135         // it is possible to return null
136
return null;
137     }
138     
139     /* (non-Javadoc)
140      * @see org.eclipse.jdt.debug.core.IJavaClassType#getAllInterfaces()
141      */

142     public IJavaInterfaceType[] getAllInterfaces() throws DebugException {
143         try {
144             List JavaDoc interfaceList = ((ClassType)getUnderlyingType()).allInterfaces();
145             List JavaDoc javaInterfaceTypeList = new ArrayList JavaDoc(interfaceList.size());
146             Iterator JavaDoc iterator = interfaceList.iterator();
147             while (iterator.hasNext()) {
148                 InterfaceType interfaceType = (InterfaceType) iterator.next();
149                 if (interfaceType != null) {
150                     javaInterfaceTypeList.add(JDIType.createType(getJavaDebugTarget(), interfaceType));
151                 }
152             }
153             IJavaInterfaceType[] javaInterfaceTypeArray = new IJavaInterfaceType[javaInterfaceTypeList.size()];
154             javaInterfaceTypeArray = (IJavaInterfaceType[]) javaInterfaceTypeList.toArray(javaInterfaceTypeArray);
155             return javaInterfaceTypeArray;
156         } catch (RuntimeException JavaDoc re) {
157             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_superclass, new String JavaDoc[] {re.toString()}), re);
158         }
159         return new IJavaInterfaceType[0];
160     }
161
162     /* (non-Javadoc)
163      * @see org.eclipse.jdt.debug.core.IJavaClassType#getInterfaces()
164      */

165     public IJavaInterfaceType[] getInterfaces() throws DebugException {
166         try {
167             List JavaDoc interfaceList = ((ClassType)getUnderlyingType()).interfaces();
168             List JavaDoc javaInterfaceTypeList = new ArrayList JavaDoc(interfaceList.size());
169             Iterator JavaDoc iterator = interfaceList.iterator();
170             while (iterator.hasNext()) {
171                 InterfaceType interfaceType = (InterfaceType) iterator.next();
172                 if (interfaceType != null) {
173                     javaInterfaceTypeList.add(JDIType.createType(getJavaDebugTarget(), interfaceType));
174                 }
175             }
176             IJavaInterfaceType[] javaInterfaceTypeArray = new IJavaInterfaceType[javaInterfaceTypeList.size()];
177             javaInterfaceTypeArray = (IJavaInterfaceType[]) javaInterfaceTypeList.toArray(javaInterfaceTypeArray);
178             return javaInterfaceTypeArray;
179         } catch (RuntimeException JavaDoc re) {
180             targetRequestFailed(MessageFormat.format(JDIDebugModelMessages.JDIClassType_exception_while_retrieving_superclass, new String JavaDoc[] {re.toString()}), re);
181         }
182         return new IJavaInterfaceType[0];
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.jdt.debug.core.IJavaClassType#isEnum()
187      */

188     public boolean isEnum() {
189         return ((ClassType)getReferenceType()).isEnum();
190     }
191
192 }
193
194
Popular Tags