KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > jdwp > JdwpID


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.jdwp;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.DataOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.jdi.internal.VirtualMachineImpl;
21
22
23 /**
24  * From this class all Java Debug Wire Protocol (JDWP) IDs
25  * declared by the JDWP specification are derived.
26  *
27  */

28 public abstract class JdwpID {
29     /** Tag Constants. */
30     public static final byte NULL_TAG = 91; // Used for tagged null values.
31
public static final byte ARRAY_TAG = 91; // '[' - an array object (objectID size).
32
public static final byte BYTE_TAG = 66; // 'B' - a byte value (1 byte).
33
public static final byte CHAR_TAG = 67; // 'C' - a character value (2 bytes).
34
public static final byte OBJECT_TAG = 76; // 'L' - an object (objectID size).
35
public static final byte FLOAT_TAG = 70; // 'F' - a float value (4 bytes).
36
public static final byte DOUBLE_TAG = 68; // 'D' - a double value (8 bytes).
37
public static final byte INT_TAG = 73; // 'I' - an int value (4 bytes).
38
public static final byte LONG_TAG = 74; // 'J' - a long value (8 bytes).
39
public static final byte SHORT_TAG = 83; // 'S' - a short value (2 bytes).
40
public static final byte VOID_TAG = 86; // 'V' - a void value (no bytes).
41
public static final byte BOOLEAN_TAG = 90; // 'Z' - a boolean value (1 byte).
42
public static final byte STRING_TAG = 115; // 's' - a String object (objectID size).
43
public static final byte THREAD_TAG = 116; // 't' - a Thread object (objectID size).
44
public static final byte THREAD_GROUP_TAG = 103; // 'g' - a ThreadGroup object (objectID size).
45
public static final byte CLASS_LOADER_TAG = 108; // 'l' - a ClassLoader object (objectID size).
46
public static final byte CLASS_OBJECT_TAG = 99; // 'c' - a class object object (objectID size).
47

48     /** TypeTag Constants. */
49     public static final byte TYPE_TAG_CLASS = 1; // ReferenceType is a class.
50
public static final byte TYPE_TAG_INTERFACE = 2; // ReferenceType is an interface.
51
public static final byte TYPE_TAG_ARRAY = 3; // ReferenceType is an array.
52

53     /** Mapping of command codes to strings. */
54     private static HashMap JavaDoc fTagMap = null;
55     private static HashMap JavaDoc fTypeTagMap = null;
56
57     /** Jdwp representation of null ID. */
58     protected static final int VALUE_NULL = 0;
59
60     /** The value of the ID */
61     protected long fValue = VALUE_NULL;
62     /** The virtual machine of the mirror object that uses this ID (needed for ID sizes. */
63     protected VirtualMachineImpl fVirtualMachine;
64     
65     /**
66      * Creates new JdwpID.
67      */

68     public JdwpID(VirtualMachineImpl vmImpl) {
69         fVirtualMachine = vmImpl;
70     }
71     
72     /**
73      * @return Returns true if two IDs refer to the same entity in the target VM.
74      * @see java.lang.Object#equals(Object)
75      */

76     public boolean equals(Object JavaDoc object) {
77         return object instanceof JdwpID && fValue == ((JdwpID)object).fValue;
78     }
79     
80     /**
81      * @return Returns a has code for this object.
82      * @see java.lang.Object#hashCode
83      */

84     public int hashCode() {
85         return (int)fValue;
86     }
87     
88     /**
89      * @return Returns value of ID.
90      */

91     public final long value() {
92         return fValue;
93     }
94     
95     /**
96      * @return Returns string representation.
97      */

98     public String JavaDoc toString() {
99         return new Long JavaDoc(fValue).toString();
100     }
101
102     /**
103      * @return Returns VM specific size of ID.
104      */

105     protected abstract int getSize();
106
107     /**
108      * @return Returns true if ID is null.
109      */

110     public abstract boolean isNull();
111     
112     /**
113      * Reads ID.
114      */

115     public void read(DataInputStream JavaDoc inStream) throws IOException JavaDoc {
116         fValue = 0;
117         int size = getSize();
118         for (int i = 0; i < size; i++) {
119             int b = inStream.readUnsignedByte(); // Note that the byte must be treated as unsigned.
120
fValue = fValue << 8 | b;
121         }
122     }
123
124     /**
125      * Writes ID.
126      */

127     public void write(DataOutputStream JavaDoc outStream) throws IOException JavaDoc {
128         int size = getSize();
129         for (int i = size - 1; i >= 0; i--) {
130             byte b = (byte)(fValue >>> 8 * i); // Note that >>> must be used because fValue must be treated as unsigned.
131
outStream.write(b);
132         }
133     }
134     
135     /**
136      * Retrieves constant mappings.
137      */

138     public static void getConstantMaps() {
139         if (fTagMap != null)
140             return;
141         
142         java.lang.reflect.Field JavaDoc[] fields = JdwpID.class.getDeclaredFields();
143         fTagMap = new HashMap JavaDoc();
144         fTypeTagMap = new HashMap JavaDoc();
145         for (int i = 0; i < fields.length; i++) {
146             java.lang.reflect.Field JavaDoc field = fields[i];
147             if ((field.getModifiers() & java.lang.reflect.Modifier.PUBLIC) == 0 || (field.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || (field.getModifiers() & java.lang.reflect.Modifier.FINAL) == 0)
148                 continue;
149                 
150             try {
151                 String JavaDoc name = field.getName();
152                 Integer JavaDoc intValue = new Integer JavaDoc(field.getInt(null));
153                 if (name.startsWith("TYPE_TAG_")) { //$NON-NLS-1$
154
name = name.substring(9);
155                     fTypeTagMap.put(intValue, name);
156                 } else if (name.endsWith("_TAG")) { //$NON-NLS-1$
157
fTagMap.put(intValue, name);
158                 }
159             } catch (IllegalAccessException JavaDoc e) {
160                 // Will not occur for own class.
161
} catch (IllegalArgumentException JavaDoc e) {
162                 // Should not occur.
163
// We should take care that all public static final constants
164
// in this class are numbers that are convertible to int.
165
}
166         }
167     }
168     
169     /**
170      * @return Returns a map with string representations of tags.
171      */

172      public static Map JavaDoc tagMap() {
173         getConstantMaps();
174         return fTagMap;
175      }
176
177     /**
178      * @return Returns a map with string representations of type tags.
179      */

180      public static Map JavaDoc typeTagMap() {
181         getConstantMaps();
182         return fTypeTagMap;
183      }
184 }
185
Popular Tags