1 /* ValueFactory.java -- factory to create JDWP Values
2 Copyright (C) 2007 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. */
38 package gnu.classpath.jdwp.value;
40 import gnu.classpath.jdwp.JdwpConstants;
41 import gnu.classpath.jdwp.VMIdManager;
42 import gnu.classpath.jdwp.exception.InvalidClassException;
43 import gnu.classpath.jdwp.exception.InvalidObjectException;
44 import gnu.classpath.jdwp.exception.InvalidTagException;
45 import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
46 import gnu.classpath.jdwp.id.ObjectId;
47 import gnu.classpath.jdwp.util.JdwpString;
49 import java.nio.ByteBuffer;
52 * A factory to create JDWP Values.
54 * @author Kyle Galloway <kgallowa@redhat.com>
56 public class ValueFactory
59 * Creates a new Value of appropriate type for the value in the ByteBuffer
60 * by reading the tag byte from the front of the buffer.
62 * @param bb contains the Object
63 * @return A new Value of appropriate type
64 * @throws JdwpInternalErrorException
65 * @throws InvalidObjectException
67 public static Value createFromTagged(ByteBuffer bb)
68 throws JdwpInternalErrorException, InvalidObjectException, InvalidTagException
70 return create(bb, bb.get());
74 * Creates a new Value of appropriate type for the value in the ByteBuffer
75 * by checking the type of the Class passed in.
77 * @param bb contains the Object
78 * @param type a Class representing the type of the value in the ByteBuffer
79 * @return A new Value of appropriate type
80 * @throws JdwpInternalErrorException
81 * @throws InvalidObjectException
83 public static Value createFromUntagged(ByteBuffer bb, Class type)
84 throws JdwpInternalErrorException, InvalidObjectException, InvalidClassException
86 byte tag = getTagForClass(type);
90 return create(bb, tag);
92 catch (InvalidTagException ite)
94 throw new InvalidClassException(ite);
99 * Creates a new Value of appropriate type for the value in the ByteBuffer.
101 * @param bb contains the Object
102 * @param tag a byte representing the type of the object
103 * @return A new Value of appropriate type
104 * @throws JdwpInternalErrorException
105 * @throws InvalidObjectException
107 private static Value create(ByteBuffer bb, byte tag)
108 throws JdwpInternalErrorException, InvalidObjectException, InvalidTagException
113 case JdwpConstants.Tag.BYTE:
114 val = new ByteValue(bb.get());
116 case JdwpConstants.Tag.BOOLEAN:
117 val = new BooleanValue((bb.get() != 0));
119 case JdwpConstants.Tag.CHAR:
120 val = new CharValue(bb.getChar());
122 case JdwpConstants.Tag.SHORT:
123 val = new ShortValue(bb.getShort());
125 case JdwpConstants.Tag.INT:
126 val = new IntValue(bb.getInt());
128 case JdwpConstants.Tag.FLOAT:
129 val = new FloatValue(bb.getFloat());
131 case JdwpConstants.Tag.LONG:
132 val = new LongValue(bb.getLong());
134 case JdwpConstants.Tag.DOUBLE:
135 val = new DoubleValue(bb.getDouble());
137 case JdwpConstants.Tag.VOID:
138 val = new VoidValue();
140 case JdwpConstants.Tag.ARRAY:
141 case JdwpConstants.Tag.THREAD:
142 case JdwpConstants.Tag.OBJECT:
143 case JdwpConstants.Tag.THREAD_GROUP:
144 case JdwpConstants.Tag.CLASS_LOADER:
145 case JdwpConstants.Tag.CLASS_OBJECT:
146 ObjectId oid = VMIdManager.getDefault().readObjectId(bb);
147 val = new ObjectValue(oid.getObject());
149 case JdwpConstants.Tag.STRING:
150 val = new StringValue(JdwpString.readString(bb));
153 throw new InvalidTagException(tag);
160 * Creates a tag for the type of the class.
162 * @param klass the type to get a tag for
163 * @return a byte tag representing the class
164 * @throws JdwpInternalErrorException
165 * @throws InvalidObjectException
167 private static byte getTagForClass(Class klass)
168 throws JdwpInternalErrorException
172 if (klass.isPrimitive())
174 if (klass == byte.class)
175 tag = JdwpConstants.Tag.BYTE;
176 else if (klass == boolean.class)
177 tag = JdwpConstants.Tag.BOOLEAN;
178 else if (klass == char.class)
179 tag = JdwpConstants.Tag.CHAR;
180 else if (klass == short.class)
181 tag = JdwpConstants.Tag.SHORT;
182 else if (klass == int.class)
183 tag = JdwpConstants.Tag.INT;
184 else if (klass == float.class)
185 tag = JdwpConstants.Tag.FLOAT;
186 else if (klass == long.class)
187 tag = JdwpConstants.Tag.LONG;
188 else if (klass == double.class)
189 tag = JdwpConstants.Tag.DOUBLE;
190 else if (klass == void.class)
191 tag = JdwpConstants.Tag.VOID;
193 throw new JdwpInternalErrorException("Invalid primitive class");
197 tag = JdwpConstants.Tag.OBJECT;
204 * Create a value type for an Object of type determined by a Class. This is
205 * a special case where a value needs to be created, but the value to create
206 * it for is already in an object, not in a buffer.
208 * @param value the Object to convert to a Value
209 * @param type the Class type of the object
210 * @return a new Value representing this object
212 public static Value createFromObject(Object value, Class type)
216 if (type.isPrimitive())
218 if (type == byte.class)
219 val = new ByteValue(((Byte) value).byteValue());
220 else if (type == boolean.class)
221 val = new BooleanValue(((Boolean) value).booleanValue());
222 else if (type == char.class)
223 val = new CharValue(((Character) value).charValue());
224 else if (type == short.class)
225 val = new ShortValue(((Short) value).shortValue());
226 else if (type == int.class)
227 val = new IntValue(((Integer) value).intValue());
228 else if (type == float.class)
229 val = new FloatValue(((Float) value).floatValue());
230 else if (type == long.class)
231 val = new LongValue(((Long) value).longValue());
232 else if (type == double.class)
233 val = new DoubleValue(((Double) value).doubleValue());
234 else if (type == void.class)
235 val = new VoidValue();
239 if (type.isAssignableFrom(String.class))
240 val = new StringValue ((String) value);
242 val = new ObjectValue(value);