KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > spy > JdwpCommandPacket


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdi.internal.spy;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.DataOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.lang.reflect.Field JavaDoc;
18 import java.lang.reflect.Modifier JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * This class implements the corresponding Java Debug Wire Protocol (JDWP) packet
24  * declared by the JDWP specification.
25  *
26  */

27 public class JdwpCommandPacket extends JdwpPacket {
28     /** Command Sets. */
29     public static final int CSET_VIRTUAL_MACHINE = 1;
30     public static final int CSET_REFERENCE_TYPE = 2;
31     public static final int CSET_CLASS_TYPE = 3;
32     public static final int CSET_ARRAY_TYPE = 4;
33     public static final int CSET_INTERFACE_TYPE = 5;
34     public static final int CSET_METHOD = 6;
35     public static final int CSET_FIELD = 8;
36     public static final int CSET_OBJECT_REFERENCE = 9;
37     public static final int CSET_STRING_REFERENCE = 10;
38     public static final int CSET_THREAD_REFERENCE = 11;
39     public static final int CSET_THREAD_GROUP_REFERENCE = 12;
40     public static final int CSET_ARRAY_REFERENCE = 13;
41     public static final int CSET_CLASS_LOADER_REFERENCE = 14;
42     public static final int CSET_EVENT_REQUEST = 15;
43     public static final int CSET_STACK_FRAME = 16;
44     public static final int CSET_CLASS_OBJECT_REFERENCE = 17;
45     public static final int CSET_EVENT = 64;
46     public static final int CSET_HOT_CODE_REPLACEMENT = 128;
47
48     /** Commands VirtualMachine. */
49     public static final int VM_VERSION = 1 + (CSET_VIRTUAL_MACHINE << 8);
50     public static final int VM_CLASSES_BY_SIGNATURE = 2 + (CSET_VIRTUAL_MACHINE << 8);
51     public static final int VM_ALL_CLASSES = 3 + (CSET_VIRTUAL_MACHINE << 8);
52     public static final int VM_ALL_THREADS = 4 + (CSET_VIRTUAL_MACHINE << 8);
53     public static final int VM_TOP_LEVEL_THREAD_GROUPS = 5 + (CSET_VIRTUAL_MACHINE << 8);
54     public static final int VM_DISPOSE = 6 + (CSET_VIRTUAL_MACHINE << 8);
55     public static final int VM_ID_SIZES = 7 + (CSET_VIRTUAL_MACHINE << 8);
56     public static final int VM_SUSPEND = 8 + (CSET_VIRTUAL_MACHINE << 8);
57     public static final int VM_RESUME = 9 + (CSET_VIRTUAL_MACHINE << 8);
58     public static final int VM_EXIT = 10 + (CSET_VIRTUAL_MACHINE << 8);
59     public static final int VM_CREATE_STRING = 11 + (CSET_VIRTUAL_MACHINE << 8);
60     public static final int VM_CAPABILITIES = 12 + (CSET_VIRTUAL_MACHINE << 8);
61     public static final int VM_CLASS_PATHS = 13 + (CSET_VIRTUAL_MACHINE << 8);
62     public static final int VM_DISPOSE_OBJECTS = 14 + (CSET_VIRTUAL_MACHINE << 8);
63     public static final int VM_HOLD_EVENTS = 15 + (CSET_VIRTUAL_MACHINE << 8);
64     public static final int VM_RELEASE_EVENTS = 16 + (CSET_VIRTUAL_MACHINE << 8);
65     public static final int VM_CAPABILITIES_NEW = 17 + (CSET_VIRTUAL_MACHINE << 8);
66     public static final int VM_REDEFINE_CLASSES = 18 + (CSET_VIRTUAL_MACHINE << 8);
67     public static final int VM_SET_DEFAULT_STRATUM = 19 + (CSET_VIRTUAL_MACHINE << 8);
68     public static final int VM_ALL_CLASSES_WITH_GENERIC= 20 + (CSET_VIRTUAL_MACHINE << 8);
69
70     /** Commands ReferenceType. */
71     public static final int RT_SIGNATURE = 1 + (CSET_REFERENCE_TYPE << 8);
72     public static final int RT_CLASS_LOADER = 2 + (CSET_REFERENCE_TYPE << 8);
73     public static final int RT_MODIFIERS = 3 + (CSET_REFERENCE_TYPE << 8);
74     public static final int RT_FIELDS = 4 + (CSET_REFERENCE_TYPE << 8);
75     public static final int RT_METHODS = 5 + (CSET_REFERENCE_TYPE << 8);
76     public static final int RT_GET_VALUES = 6 + (CSET_REFERENCE_TYPE << 8);
77     public static final int RT_SOURCE_FILE = 7 + (CSET_REFERENCE_TYPE << 8);
78     public static final int RT_NESTED_TYPES = 8 + (CSET_REFERENCE_TYPE << 8);
79     public static final int RT_STATUS = 9 + (CSET_REFERENCE_TYPE << 8);
80     public static final int RT_INTERFACES = 10 + (CSET_REFERENCE_TYPE << 8);
81     public static final int RT_CLASS_OBJECT = 11 + (CSET_REFERENCE_TYPE << 8);
82     public static final int RT_SOURCE_DEBUG_EXTENSION = 12 + (CSET_REFERENCE_TYPE << 8);
83     public static final int RT_SIGNATURE_WITH_GENERIC = 13 + (CSET_REFERENCE_TYPE << 8);
84     public static final int RT_FIELDS_WITH_GENERIC = 14 + (CSET_REFERENCE_TYPE << 8);
85     public static final int RT_METHODS_WITH_GENERIC = 15 + (CSET_REFERENCE_TYPE << 8);
86
87     /** Commands ClassType. */
88     public static final int CT_SUPERCLASS = 1 + (CSET_CLASS_TYPE << 8);
89     public static final int CT_SET_VALUES = 2 + (CSET_CLASS_TYPE << 8);
90     public static final int CT_INVOKE_METHOD = 3 + (CSET_CLASS_TYPE << 8);
91     public static final int CT_NEW_INSTANCE = 4 + (CSET_CLASS_TYPE << 8);
92
93     /** Commands ArrayType. */
94     public static final int AT_NEW_INSTANCE = 1 + (CSET_ARRAY_TYPE << 8);
95
96     /** Commands Method. */
97     public static final int M_LINE_TABLE = 1 + (CSET_METHOD << 8);
98     public static final int M_VARIABLE_TABLE = 2 + (CSET_METHOD << 8);
99     public static final int M_BYTECODES = 3 + (CSET_METHOD << 8);
100     public static final int M_IS_OBSOLETE = 4 + (CSET_METHOD << 8);
101     public static final int M_VARIABLE_TABLE_WITH_GENERIC = 5 + (CSET_METHOD << 8);
102
103     /** Commands ObjectReference. */
104     public static final int OR_REFERENCE_TYPE = 1 + (CSET_OBJECT_REFERENCE << 8);
105     public static final int OR_GET_VALUES = 2 + (CSET_OBJECT_REFERENCE << 8);
106     public static final int OR_SET_VALUES = 3 + (CSET_OBJECT_REFERENCE << 8);
107     public static final int OR_MONITOR_INFO = 5 + (CSET_OBJECT_REFERENCE << 8);
108     public static final int OR_INVOKE_METHOD = 6 + (CSET_OBJECT_REFERENCE << 8);
109     public static final int OR_DISABLE_COLLECTION = 7 + (CSET_OBJECT_REFERENCE << 8);
110     public static final int OR_ENABLE_COLLECTION = 8 + (CSET_OBJECT_REFERENCE << 8);
111     public static final int OR_IS_COLLECTED = 9 + (CSET_OBJECT_REFERENCE << 8);
112
113     /** Commands StringReference. */
114     public static final int SR_VALUE = 1 + (CSET_STRING_REFERENCE << 8);
115
116     /** Commands ThreadReference. */
117     public static final int TR_NAME = 1 + (CSET_THREAD_REFERENCE << 8);
118     public static final int TR_SUSPEND = 2 + (CSET_THREAD_REFERENCE << 8);
119     public static final int TR_RESUME = 3 + (CSET_THREAD_REFERENCE << 8);
120     public static final int TR_STATUS = 4 + (CSET_THREAD_REFERENCE << 8);
121     public static final int TR_THREAD_GROUP = 5 + (CSET_THREAD_REFERENCE << 8);
122     public static final int TR_FRAMES = 6 + (CSET_THREAD_REFERENCE << 8);
123     public static final int TR_FRAME_COUNT = 7 + (CSET_THREAD_REFERENCE << 8);
124     public static final int TR_OWNED_MONITORS = 8 + (CSET_THREAD_REFERENCE << 8);
125     public static final int TR_CURRENT_CONTENDED_MONITOR = 9 + (CSET_THREAD_REFERENCE << 8);
126     public static final int TR_STOP = 10 + (CSET_THREAD_REFERENCE << 8);
127     public static final int TR_INTERRUPT = 11 + (CSET_THREAD_REFERENCE << 8);
128     public static final int TR_SUSPEND_COUNT = 12 + (CSET_THREAD_REFERENCE << 8);
129     public static final int TR_POP_TOP_FRAME = 13 + (CSET_THREAD_REFERENCE << 8);
130
131     /** Commands ThreadGroupReference. */
132     public static final int TGR_NAME = 1 + (CSET_THREAD_GROUP_REFERENCE << 8);
133     public static final int TGR_PARENT = 2 + (CSET_THREAD_GROUP_REFERENCE << 8);
134     public static final int TGR_CHILDREN = 3 + (CSET_THREAD_GROUP_REFERENCE << 8);
135
136     /** Commands ArrayReference. */
137     public static final int AR_LENGTH = 1 + (CSET_ARRAY_REFERENCE << 8);
138     public static final int AR_GET_VALUES = 2 + (CSET_ARRAY_REFERENCE << 8);
139     public static final int AR_SET_VALUES = 3 + (CSET_ARRAY_REFERENCE << 8);
140
141     /** Commands ClassLoaderReference. */
142     public static final int CLR_VISIBLE_CLASSES = 1 + (CSET_CLASS_LOADER_REFERENCE << 8);
143
144     /** Commands EventRequest. */
145     public static final int ER_SET = 1 + (CSET_EVENT_REQUEST << 8);
146     public static final int ER_CLEAR = 2 + (CSET_EVENT_REQUEST << 8);
147     public static final int ER_CLEAR_ALL_BREAKPOINTS = 3 + (CSET_EVENT_REQUEST << 8);
148
149     /** Commands StackFrame. */
150     public static final int SF_GET_VALUES = 1 + (CSET_STACK_FRAME << 8);
151     public static final int SF_SET_VALUES = 2 + (CSET_STACK_FRAME << 8);
152     public static final int SF_THIS_OBJECT = 3 + (CSET_STACK_FRAME << 8);
153     public static final int SF_POP_FRAME = 4 + (CSET_STACK_FRAME << 8);
154
155     /** Commands ClassObjectReference. */
156     public static final int COR_REFLECTED_TYPE = 1 + (CSET_CLASS_OBJECT_REFERENCE << 8);
157
158     /** Commands Event. */
159     public static final int E_COMPOSITE = 100 + (CSET_EVENT << 8);
160
161     /** Commands Hot Code Replacement (OTI specific). */
162     public static final int HCR_CLASSES_HAVE_CHANGED = 1 + (CSET_HOT_CODE_REPLACEMENT << 8);
163     public static final int HCR_GET_CLASS_VERSION = 2 + (CSET_HOT_CODE_REPLACEMENT << 8);
164     public static final int HCR_DO_RETURN = 3 + (CSET_HOT_CODE_REPLACEMENT << 8);
165     public static final int HCR_REENTER_ON_EXIT = 4 + (CSET_HOT_CODE_REPLACEMENT << 8);
166     public static final int HCR_CAPABILITIES = 5 + (CSET_HOT_CODE_REPLACEMENT << 8);
167
168     /** Mapping of command codes to strings. */
169     private static Map JavaDoc fgCommandMap = null;
170
171     /** Next id to be assigned. */
172     private static int fgNextId = 1;
173     /** Command, note that this field is 256 * JDWP CommandSet (unsigned) + JDWP Command. */
174     private int fCommand;
175
176     /**
177      * Creates new JdwpCommandPacket.
178      */

179     protected JdwpCommandPacket() {
180     }
181
182     /**
183      * Creates new JdwpCommandPacket.
184      */

185     public JdwpCommandPacket(int command) {
186         setCommand(command);
187         setId(getNewId());
188     }
189     
190     /**
191      * @return Returns unique id for command packet.
192      */

193     public static synchronized int getNewId() {
194         return fgNextId++;
195     }
196
197     /**
198      * @return Returns JDWP command set of packet.
199      */

200     public byte getCommandSet() {
201         return (byte)(fCommand >>> 8);
202     }
203
204     /**
205      * @return Returns 256 * JDWP CommandSet (unsigned) + JDWP Command.
206      */

207     public int getCommand() {
208         return fCommand;
209     }
210     
211     /**
212      * Assigns command (256 * JDWP CommandSet (unsigned) + JDWP Command)
213      */

214     public void setCommand(int command) {
215         fCommand = command;
216     }
217     
218     /**
219      * Reads header fields that are specific for this type of packet.
220      */

221     protected void readSpecificHeaderFields(DataInputStream JavaDoc dataInStream) throws IOException JavaDoc {
222         byte commandSet = dataInStream.readByte();
223         fCommand = dataInStream.readByte() + (commandSet << 8);
224     }
225
226     /**
227      * Writes header fields that are specific for this type of packet.
228      */

229     protected void writeSpecificHeaderFields(DataOutputStream JavaDoc dataOutStream) throws IOException JavaDoc {
230         dataOutStream.writeByte(getCommandSet());
231         dataOutStream.writeByte((byte)fCommand);
232     }
233     
234     /**
235      * Retrieves constant mappings.
236      */

237     public static void getConstantMaps() {
238         if (fgCommandMap != null) {
239             return;
240         }
241         
242         Field JavaDoc[] fields = JdwpCommandPacket.class.getDeclaredFields();
243         
244         // First get the set names.
245
Map JavaDoc setNames = new HashMap JavaDoc(fields.length);
246         for (int i = 0; i < fields.length; i++) {
247             Field JavaDoc field = fields[i];
248             if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0)
249                 continue;
250
251             try {
252                 String JavaDoc name = field.getName();
253                 // If it is not a set, continue.
254
if (!name.startsWith("CSET_")) {//$NON-NLS-1$
255
continue;
256                 }
257                 int value = field.getInt(null);
258                 setNames.put(new Integer JavaDoc(value), removePrefix(name));
259             } catch (IllegalAccessException JavaDoc e) {
260                 // Will not occur for own class.
261
} catch (IllegalArgumentException JavaDoc e) {
262                 // Should not occur.
263
// We should take care that all public static final constants
264
// in this class are numbers that are convertible to int.
265
}
266         }
267         
268         // Get the commands.
269
fgCommandMap = new HashMap JavaDoc();
270         for (int i = 0; i < fields.length; i++) {
271             Field JavaDoc field = fields[i];
272             if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0) {
273                 continue;
274             }
275                 
276             try {
277                 String JavaDoc name = field.getName();
278                 
279                 // If it is a set, continue.
280
if (name.startsWith("CSET_")) { //$NON-NLS-1$
281
continue;
282                 }
283                 Integer JavaDoc val = (Integer JavaDoc)field.get(null);
284                 int value = val.intValue();
285                 int set = value >>> 8;
286                 String JavaDoc setName = (String JavaDoc)setNames.get(new Integer JavaDoc(set));
287                 String JavaDoc entryName = setName + " - " + removePrefix(name); //$NON-NLS-1$
288

289                 fgCommandMap.put(val, entryName);
290                 
291             } catch (IllegalAccessException JavaDoc e) {
292                 // Will not occur for own class.
293
}
294         }
295     }
296
297     /**
298      * @return Returns a map with string representations of error codes.
299      */

300     public static Map JavaDoc commandMap() {
301         getConstantMaps();
302         return fgCommandMap;
303     }
304     
305     /**
306      * @return Returns string without XXX_ prefix.
307      */

308     public static String JavaDoc removePrefix(String JavaDoc str) {
309         int i = str.indexOf('_');
310         if (i < 0) {
311             return str;
312         }
313         return str.substring(i + 1);
314     }
315 }
316
Popular Tags