1 /* ArrayReferenceCommandSet.java -- class to implement the Array
3 Copyright (C) 2005, 2007 Free Software Foundation
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 terms of your choice, provided that you also meet, for each linked
33 independent module, the terms and conditions of the license of that
34 module. An independent module is a module which is not derived from
35 or based on this library. If you modify this library, you may extend
36 this exception to your version of the library, but you are not
37 obligated to do so. If you do not wish to do so, delete this
38 exception statement from your version. */
41 package gnu.classpath.jdwp.processor;
43 import gnu.classpath.jdwp.JdwpConstants;
44 import gnu.classpath.jdwp.exception.InvalidObjectException;
45 import gnu.classpath.jdwp.exception.JdwpException;
46 import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
47 import gnu.classpath.jdwp.exception.NotImplementedException;
48 import gnu.classpath.jdwp.id.ObjectId;
49 import gnu.classpath.jdwp.value.Value;
50 import gnu.classpath.jdwp.value.ValueFactory;
52 import java.io.DataOutputStream;
53 import java.io.IOException;
54 import java.lang.reflect.Array;
55 import java.nio.ByteBuffer;
58 * A class representing the ArrayReference Command Set.
60 * @author Aaron Luchko <aluchko@redhat.com>
62 public class ArrayReferenceCommandSet
65 public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
72 case JdwpConstants.CommandSet.ArrayReference.LENGTH:
73 executeLength(bb, os);
75 case JdwpConstants.CommandSet.ArrayReference.GET_VALUES:
76 executeGetValues(bb, os);
78 case JdwpConstants.CommandSet.ArrayReference.SET_VALUES:
79 executeSetValues(bb, os);
82 throw new NotImplementedException("Command " + command +
83 " not found in Array Reference Command Set.");
86 catch (IOException ex)
88 // The DataOutputStream we're using isn't talking to a socket at all
89 // So if we throw an IOException we're in serious trouble
90 throw new JdwpInternalErrorException(ex);
96 private void executeLength(ByteBuffer bb, DataOutputStream os)
97 throws InvalidObjectException, IOException
99 ObjectId oid = idMan.readObjectId(bb);
100 Object array = oid.getObject();
101 os.writeInt(Array.getLength(array));
104 private void executeGetValues(ByteBuffer bb, DataOutputStream os)
105 throws JdwpException, IOException
107 ObjectId oid = idMan.readObjectId(bb);
108 Object array = oid.getObject();
109 int first = bb.getInt();
110 int length = bb.getInt();
112 // We need to write out the byte signifying the type of array first
113 Class clazz = array.getClass().getComponentType();
115 // Uugh, this is a little ugly but it's the only time we deal with
117 if (clazz == byte.class)
118 os.writeByte(JdwpConstants.Tag.BYTE);
119 else if (clazz == char.class)
120 os.writeByte(JdwpConstants.Tag.CHAR);
121 else if (clazz == float.class)
122 os.writeByte(JdwpConstants.Tag.FLOAT);
123 else if (clazz == double.class)
124 os.writeByte(JdwpConstants.Tag.DOUBLE);
125 else if (clazz == int.class)
126 os.writeByte(JdwpConstants.Tag.BYTE);
127 else if (clazz == long.class)
128 os.writeByte(JdwpConstants.Tag.LONG);
129 else if (clazz == short.class)
130 os.writeByte(JdwpConstants.Tag.SHORT);
131 else if (clazz == void.class)
132 os.writeByte(JdwpConstants.Tag.VOID);
133 else if (clazz == boolean.class)
134 os.writeByte(JdwpConstants.Tag.BOOLEAN);
135 else if (clazz.isArray())
136 os.writeByte(JdwpConstants.Tag.ARRAY);
137 else if (String.class.isAssignableFrom(clazz))
138 os.writeByte(JdwpConstants.Tag.STRING);
139 else if (Thread.class.isAssignableFrom(clazz))
140 os.writeByte(JdwpConstants.Tag.THREAD);
141 else if (ThreadGroup.class.isAssignableFrom(clazz))
142 os.writeByte(JdwpConstants.Tag.THREAD_GROUP);
143 else if (ClassLoader.class.isAssignableFrom(clazz))
144 os.writeByte(JdwpConstants.Tag.CLASS_LOADER);
145 else if (Class.class.isAssignableFrom(clazz))
146 os.writeByte(JdwpConstants.Tag.CLASS_OBJECT);
148 os.writeByte(JdwpConstants.Tag.OBJECT);
150 // Write all the values, primitives should be untagged and Objects must be
152 for (int i = first; i < first + length; i++)
154 Value val = ValueFactory.createFromObject(Array.get(array, i), clazz);
155 if (clazz.isPrimitive())
156 val.writeUntagged(os);
162 private void executeSetValues(ByteBuffer bb, DataOutputStream os)
163 throws IOException, JdwpException
165 ObjectId oid = idMan.readObjectId(bb);
166 Object array = oid.getObject();
167 int first = bb.getInt();
168 int length = bb.getInt();
169 Class type = array.getClass().getComponentType();
170 for (int i = first; i < first + length; i++)
172 Object value = Value.getUntaggedObject(bb, type);
173 Array.set(array, i, value);