1 /* Value.java -- class to read/write JDWP tagged and untagged values
2 Copyright (C) 2005, 2006, Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.jdwp.util;
41 import gnu.classpath.jdwp.JdwpConstants;
42 import gnu.classpath.jdwp.VMIdManager;
43 import gnu.classpath.jdwp.exception.InvalidFieldException;
44 import gnu.classpath.jdwp.exception.JdwpException;
45 import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
46 import gnu.classpath.jdwp.exception.NotImplementedException;
47 import gnu.classpath.jdwp.id.ObjectId;
49 import java.io.DataOutputStream;
50 import java.io.IOException;
51 import java.nio.ByteBuffer;
54 * A class to read/write JDWP tagged and untagged values.
56 * @author Aaron Luchko <aluchko@redhat.com>
61 * Will write the given object as an untagged value to the DataOutputStream.
63 * @param os write the value here
64 * @param obj the Object to write
66 * @throws InvalidFieldException
68 public static void writeUntaggedValue(DataOutputStream os, Object obj)
69 throws JdwpException, IOException
71 writeValue(os, obj, false);
75 * Will write the given object as a tagged value to the DataOutputStream.
77 * @param os write the value here
78 * @param obj the Object to write
80 * @throws InvalidFieldException
82 public static void writeTaggedValue(DataOutputStream os, Object obj)
83 throws JdwpException, IOException
85 writeValue(os, obj, true);
89 * Will write the given object as either a value or an untagged value to the
92 * @param os write the value here
93 * @param obj the Object to write
94 * @param tagged true if the value is tagged, false otherwise
96 * @throws InvalidFieldException
98 private static void writeValue(DataOutputStream os, Object obj,
100 throws IOException, JdwpException
102 Class clazz = obj.getClass();
103 if (clazz.isPrimitive())
105 if (clazz == byte.class)
108 os.writeByte(JdwpConstants.Tag.BYTE);
109 os.writeByte(((Byte) obj).byteValue());
111 else if (clazz == char.class)
114 os.writeByte(JdwpConstants.Tag.CHAR);
115 os.writeChar(((Character) obj).charValue());
117 else if (clazz == float.class)
120 os.writeByte(JdwpConstants.Tag.FLOAT);
121 os.writeFloat(((Float) obj).floatValue());
123 else if (clazz == double.class)
126 os.writeByte(JdwpConstants.Tag.DOUBLE);
127 os.writeDouble(((Double) obj).doubleValue());
129 else if (clazz == int.class)
132 os.writeByte(JdwpConstants.Tag.BYTE);
133 os.writeInt(((Integer) obj).intValue());
135 else if (clazz == long.class)
138 os.writeByte(JdwpConstants.Tag.LONG);
139 os.writeLong(((Long) obj).longValue());
141 else if (clazz == short.class)
144 os.writeByte(JdwpConstants.Tag.SHORT);
145 os.writeInt(((Short) obj).shortValue());
147 else if (clazz == void.class)
148 { // A 'void' has no data
150 os.writeByte(JdwpConstants.Tag.VOID);
152 else if (clazz == boolean.class)
155 os.writeByte(JdwpConstants.Tag.BOOLEAN);
156 os.writeBoolean(((Boolean) obj).booleanValue());
159 { // This shouldn't be possible
160 throw new JdwpInternalErrorException(
161 "Field has invalid primitive!");
166 // Object is an Object, not a primitive type wrapped in an object
167 // Write the appropriate tag
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);
183 os.writeByte(JdwpConstants.Tag.OBJECT);
185 ObjectId oid = VMIdManager.getDefault().getObjectId(obj);
191 * Reads the appropriate object for the tagged value contained in the
194 * @param bb contains the Object
195 * @return The Object referenced by the value
196 * @throws JdwpException
197 * @throws IOException
199 public static Object getObj(ByteBuffer bb)
200 throws JdwpException, IOException
202 return getUntaggedObj(bb, bb.get());
206 * Reads an object of the given Class from the untagged value contained
209 * @param bb contains the Object
210 * @param type corresponds to the TAG of value to be read
211 * @return the resultant object
212 * @throws JdwpException
213 * @throws IOException
215 public static Object getUntaggedObj(ByteBuffer bb, Class type)
216 throws JdwpException, IOException
218 if (type.isPrimitive())
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 Boolean.valueOf(bb.get() != 0);
236 else if (type == void.class)
239 { // This shouldn't be possible
240 throw new JdwpInternalErrorException(
241 "Field has invalid primitive!");
246 // Field is an object
247 ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
248 return oid.getObject();
253 * Reads the an object of the given Class from the untagged value contained
256 * @param bb contains the Object
257 * @param tag TAG of the Value to be read
259 * @throws JdwpException
260 * @throws IOException
262 public static Object getUntaggedObj(ByteBuffer bb, byte tag)
263 throws JdwpException, IOException
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:
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 // All these cases are ObjectIds
294 ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
295 return oid.getObject();
297 throw new NotImplementedException("Tag " + tag
298 + " is not implemented.");