1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52:
53:
58: public class Value
59: {
60:
68: public static void writeUntaggedValue(DataOutputStream os, Object obj)
69: throws JdwpException, IOException
70: {
71: writeValue(os, obj, false);
72: }
73:
74:
82: public static void writeTaggedValue(DataOutputStream os, Object obj)
83: throws JdwpException, IOException
84: {
85: writeValue(os, obj, true);
86: }
87:
88:
98: private static void writeValue(DataOutputStream os, Object obj,
99: boolean tagged)
100: throws IOException, JdwpException
101: {
102: Class clazz = obj.getClass();
103: if (clazz.isPrimitive())
104: {
105: if (clazz == byte.class)
106: {
107: if (tagged)
108: os.writeByte(JdwpConstants.Tag.BYTE);
109: os.writeByte(((Byte) obj).byteValue());
110: }
111: else if (clazz == char.class)
112: {
113: if (tagged)
114: os.writeByte(JdwpConstants.Tag.CHAR);
115: os.writeChar(((Character) obj).charValue());
116: }
117: else if (clazz == float.class)
118: {
119: if (tagged)
120: os.writeByte(JdwpConstants.Tag.FLOAT);
121: os.writeFloat(((Float) obj).floatValue());
122: }
123: else if (clazz == double.class)
124: {
125: if (tagged)
126: os.writeByte(JdwpConstants.Tag.DOUBLE);
127: os.writeDouble(((Double) obj).doubleValue());
128: }
129: else if (clazz == int.class)
130: {
131: if (tagged)
132: os.writeByte(JdwpConstants.Tag.BYTE);
133: os.writeInt(((Integer) obj).intValue());
134: }
135: else if (clazz == long.class)
136: {
137: if (tagged)
138: os.writeByte(JdwpConstants.Tag.LONG);
139: os.writeLong(((Long) obj).longValue());
140: }
141: else if (clazz == short.class)
142: {
143: if (tagged)
144: os.writeByte(JdwpConstants.Tag.SHORT);
145: os.writeInt(((Short) obj).shortValue());
146: }
147: else if (clazz == void.class)
148: {
149: if (tagged)
150: os.writeByte(JdwpConstants.Tag.VOID);
151: }
152: else if (clazz == boolean.class)
153: {
154: if (tagged)
155: os.writeByte(JdwpConstants.Tag.BOOLEAN);
156: os.writeBoolean(((Boolean) obj).booleanValue());
157: }
158: else
159: {
160: throw new JdwpInternalErrorException(
161: "Field has invalid primitive!");
162: }
163: }
164: else
165: {
166:
167:
168: if (tagged)
169: {
170: if (clazz.isArray())
171: os.writeByte(JdwpConstants.Tag.ARRAY);
172: else if (obj instanceof String)
173: os.writeByte(JdwpConstants.Tag.STRING);
174: else if (obj instanceof Thread)
175: os.writeByte(JdwpConstants.Tag.THREAD);
176: else if (obj instanceof ThreadGroup)
177: os.writeByte(JdwpConstants.Tag.THREAD_GROUP);
178: else if (obj instanceof ClassLoader)
179: os.writeByte(JdwpConstants.Tag.CLASS_LOADER);
180: else if (obj instanceof Class)
181: os.writeByte(JdwpConstants.Tag.CLASS_OBJECT);
182: else
183: os.writeByte(JdwpConstants.Tag.OBJECT);
184: }
185: ObjectId oid = VMIdManager.getDefault().getObjectId(obj);
186: oid.write(os);
187: }
188: }
189:
190:
199: public static Object getObj(ByteBuffer bb)
200: throws JdwpException, IOException
201: {
202: return getUntaggedObj(bb, bb.get());
203: }
204:
205:
215: public static Object getUntaggedObj(ByteBuffer bb, Class type)
216: throws JdwpException, IOException
217: {
218: if (type.isPrimitive())
219: {
220: if (type == byte.class)
221: return new Byte(bb.get());
222: else if (type == char.class)
223: return new Character(bb.getChar());
224: else if (type == float.class)
225: return new Float(bb.getFloat());
226: else if (type == double.class)
227: return new Double(bb.getDouble());
228: else if (type == int.class)
229: return new Integer(bb.getInt());
230: else if (type == long.class)
231: return new Long(bb.getLong());
232: else if (type == short.class)
233: return new Short(bb.getShort());
234: else if (type == boolean.class)
235: return (bb.get() == 0) ? new Boolean(false) : new Boolean(true);
236: else if (type == void.class)
237: return new byte[0];
238: else
239: {
240: throw new JdwpInternalErrorException(
241: "Field has invalid primitive!");
242: }
243: }
244: else
245: {
246:
247: ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
248: return oid.getObject();
249: }
250: }
251:
252:
262: public static Object getUntaggedObj(ByteBuffer bb, byte tag)
263: throws JdwpException, IOException
264: {
265: switch (tag)
266: {
267: case JdwpConstants.Tag.BYTE:
268: return new Byte(bb.get());
269: case JdwpConstants.Tag.CHAR:
270: return new Character(bb.getChar());
271: case JdwpConstants.Tag.FLOAT:
272: return new Float(bb.getFloat());
273: case JdwpConstants.Tag.DOUBLE:
274: return new Double(bb.getDouble());
275: case JdwpConstants.Tag.INT:
276: return new Integer(bb.getInt());
277: case JdwpConstants.Tag.LONG:
278: return new Long(bb.getLong());
279: case JdwpConstants.Tag.SHORT:
280: return new Short(bb.getShort());
281: case JdwpConstants.Tag.VOID:
282: return new byte[0];
283: case JdwpConstants.Tag.BOOLEAN:
284: return (bb.get() == 0) ? new Boolean(false) : new Boolean(true);
285: case JdwpConstants.Tag.STRING:
286: return JdwpString.readString(bb);
287: case JdwpConstants.Tag.ARRAY:
288: case JdwpConstants.Tag.THREAD:
289: case JdwpConstants.Tag.OBJECT:
290: case JdwpConstants.Tag.THREAD_GROUP:
291: case JdwpConstants.Tag.CLASS_LOADER:
292: case JdwpConstants.Tag.CLASS_OBJECT:
293:
294: ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
295: return oid.getObject();
296: default:
297: throw new NotImplementedException("Tag " + tag
298: + " is not implemented.");
299: }
300: }
301: }