1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50:
51:
56: public class ValueFactory
57: {
58:
67: public static Value createFromTagged(ByteBuffer bb)
68: throws JdwpInternalErrorException, InvalidObjectException, InvalidTagException
69: {
70: return create(bb, bb.get());
71: }
72:
73:
83: public static Value createFromUntagged(ByteBuffer bb, Class type)
84: throws JdwpInternalErrorException, InvalidObjectException, InvalidClassException
85: {
86: byte tag = getTagForClass(type);
87:
88: try
89: {
90: return create(bb, tag);
91: }
92: catch (InvalidTagException ite)
93: {
94: throw new InvalidClassException(ite);
95: }
96: }
97:
98:
107: private static Value create(ByteBuffer bb, byte tag)
108: throws JdwpInternalErrorException, InvalidObjectException, InvalidTagException
109: {
110: Value val = null;
111: switch(tag)
112: {
113: case JdwpConstants.Tag.BYTE:
114: val = new ByteValue(bb.get());
115: break;
116: case JdwpConstants.Tag.BOOLEAN:
117: val = new BooleanValue((bb.get() != 0));
118: break;
119: case JdwpConstants.Tag.CHAR:
120: val = new CharValue(bb.getChar());
121: break;
122: case JdwpConstants.Tag.SHORT:
123: val = new ShortValue(bb.getShort());
124: break;
125: case JdwpConstants.Tag.INT:
126: val = new IntValue(bb.getInt());
127: break;
128: case JdwpConstants.Tag.FLOAT:
129: val = new FloatValue(bb.getFloat());
130: break;
131: case JdwpConstants.Tag.LONG:
132: val = new LongValue(bb.getLong());
133: break;
134: case JdwpConstants.Tag.DOUBLE:
135: val = new DoubleValue(bb.getDouble());
136: break;
137: case JdwpConstants.Tag.VOID:
138: val = new VoidValue();
139: break;
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());
148: break;
149: case JdwpConstants.Tag.STRING:
150: val = new StringValue(JdwpString.readString(bb));
151: break;
152: default:
153: throw new InvalidTagException(tag);
154: }
155:
156: return val;
157: }
158:
159:
167: private static byte getTagForClass(Class klass)
168: throws JdwpInternalErrorException
169: {
170: byte tag;
171:
172: if (klass.isPrimitive())
173: {
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;
192: else
193: throw new JdwpInternalErrorException("Invalid primitive class");
194: }
195: else
196: {
197: tag = JdwpConstants.Tag.OBJECT;
198: }
199:
200: return tag;
201: }
202:
203:
212: public static Value createFromObject(Object value, Class type)
213: {
214: Value val = null;
215:
216: if (type.isPrimitive())
217: {
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();
236: }
237: else
238: {
239: if (type.isAssignableFrom(String.class))
240: val = new StringValue ((String) value);
241: else
242: val = new ObjectValue(value);
243: }
244:
245: return val;
246: }
247: }