1:
39:
40:
41: package ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
67: public class ClassTypeCommandSet
68: extends CommandSet
69: {
70: public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
71: throws JdwpException
72: {
73: try
74: {
75: switch (command)
76: {
77: case JdwpConstants.CommandSet.ClassType.SUPERCLASS:
78: executeSuperclass(bb, os);
79: break;
80: case JdwpConstants.CommandSet.ClassType.SET_VALUES:
81: executeSetValues(bb, os);
82: break;
83: case JdwpConstants.CommandSet.ClassType.INVOKE_METHOD:
84: executeInvokeMethod(bb, os);
85: break;
86: case JdwpConstants.CommandSet.ClassType.NEW_INSTANCE:
87: executeNewInstance(bb, os);
88: break;
89: default:
90: throw new NotImplementedException("Command " + command +
91: " not found in ClassType Command Set.");
92: }
93: }
94: catch (IOException ex)
95: {
96:
97:
98: throw new JdwpInternalErrorException(ex);
99: }
100:
101: return false;
102: }
103:
104: private void executeSuperclass(ByteBuffer bb, DataOutputStream os)
105: throws JdwpException, IOException
106: {
107: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
108: Class clazz = refId.getType();
109: Class superClazz = clazz.getSuperclass();
110:
111: if (superClazz == null) {
112: os.writeLong(0L);
113: } else {
114: ReferenceTypeId clazzId = idMan.getReferenceTypeId(superClazz);
115: clazzId.write(os);
116: }
117: }
118:
119: private void executeSetValues(ByteBuffer bb, DataOutputStream os)
120: throws JdwpException, IOException
121: {
122: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
123:
124:
125: Class clazz = refId.getType();
126:
127: int numValues = bb.getInt();
128:
129: for (int i = 0; i < numValues; i++)
130: {
131: ObjectId fieldId = idMan.readObjectId(bb);
132: Field field = (Field) (fieldId.getObject());
133: Object value = Value.getUntaggedObject(bb, field.getType());
134: try
135: {
136: field.setAccessible(true);
137: field.set(null, value);
138: }
139: catch (IllegalArgumentException ex)
140: {
141: throw new InvalidFieldException(ex);
142: }
143: catch (IllegalAccessException ex)
144: {
145: throw new JdwpInternalErrorException(ex);
146: }
147: }
148: }
149:
150: private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
151: throws JdwpException, IOException
152: {
153: MethodResult mr = invokeMethod(bb);
154:
155: Throwable exception = mr.getThrownException();
156: ObjectId eId = idMan.getObjectId(exception);
157: mr.getReturnedValue().writeTagged(os);
158: eId.writeTagged(os);
159: }
160:
161: private void executeNewInstance(ByteBuffer bb, DataOutputStream os)
162: throws JdwpException, IOException
163: {
164: MethodResult mr = invokeMethod(bb);
165: Throwable exception = mr.getThrownException();
166:
167: if (exception == null && ! (mr.getReturnedValue() instanceof ObjectValue))
168: throw new JdwpInternalErrorException("new instance returned non-object");
169:
170: ObjectValue ov = (ObjectValue) mr.getReturnedValue();
171: ObjectId oId = idMan.getObjectId(ov.getValue());
172:
173: ObjectId eId = idMan.getObjectId(exception);
174:
175: oId.writeTagged(os);
176: eId.writeTagged(os);
177: }
178:
179:
182: private MethodResult invokeMethod(ByteBuffer bb)
183: throws JdwpException, IOException
184: {
185: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
186: Class clazz = refId.getType();
187:
188: ObjectId tId = idMan.readObjectId(bb);
189: Thread thread = (Thread) tId.getObject();
190:
191: VMMethod method = VMMethod.readId(clazz, bb);
192:
193: int args = bb.getInt();
194: Value[] values = new Value[args];
195:
196: for (int i = 0; i < args; i++)
197: values[i] = ValueFactory.createFromTagged(bb);
198:
199: int invokeOpts = bb.getInt();
200: MethodResult mr = VMVirtualMachine.executeMethod(null, thread,
201: clazz, method,
202: values, invokeOpts);
203: return mr;
204: }
205: }