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:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
65: public class ClassTypeCommandSet
66: extends CommandSet
67: {
68: public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
69: throws JdwpException
70: {
71: try
72: {
73: switch (command)
74: {
75: case JdwpConstants.CommandSet.ClassType.SUPERCLASS:
76: executeSuperclass(bb, os);
77: break;
78: case JdwpConstants.CommandSet.ClassType.SET_VALUES:
79: executeSetValues(bb, os);
80: break;
81: case JdwpConstants.CommandSet.ClassType.INVOKE_METHOD:
82: executeInvokeMethod(bb, os);
83: break;
84: case JdwpConstants.CommandSet.ClassType.NEW_INSTANCE:
85: executeNewInstance(bb, os);
86: break;
87: default:
88: throw new NotImplementedException("Command " + command +
89: " not found in ClassType Command Set.");
90: }
91: }
92: catch (IOException ex)
93: {
94:
95:
96: throw new JdwpInternalErrorException(ex);
97: }
98:
99: return false;
100: }
101:
102: private void executeSuperclass(ByteBuffer bb, DataOutputStream os)
103: throws JdwpException, IOException
104: {
105: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
106: Class clazz = refId.getType();
107: Class superClazz = clazz.getSuperclass();
108:
109: ReferenceTypeId clazzId = idMan.getReferenceTypeId(superClazz);
110: clazzId.write(os);
111: }
112:
113: private void executeSetValues(ByteBuffer bb, DataOutputStream os)
114: throws JdwpException, IOException
115: {
116: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
117:
118:
119: Class clazz = refId.getType();
120:
121: int numValues = bb.getInt();
122:
123: for (int i = 0; i < numValues; i++)
124: {
125: ObjectId fieldId = idMan.readObjectId(bb);
126: Field field = (Field) (fieldId.getObject());
127: Object value = Value.getUntaggedObj(bb, field.getType());
128: try
129: {
130: field.setAccessible(true);
131: field.set(null, value);
132: }
133: catch (IllegalArgumentException ex)
134: {
135: throw new InvalidFieldException(ex);
136: }
137: catch (IllegalAccessException ex)
138: {
139: throw new JdwpInternalErrorException(ex);
140: }
141: }
142: }
143:
144: private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
145: throws JdwpException, IOException
146: {
147: MethodResult mr = invokeMethod(bb);
148:
149: Object value = mr.getReturnedValue();
150: Exception exception = mr.getThrownException();
151: ObjectId eId = idMan.getObjectId(exception);
152:
153: Value.writeTaggedValue(os, value);
154: eId.writeTagged(os);
155: }
156:
157: private void executeNewInstance(ByteBuffer bb, DataOutputStream os)
158: throws JdwpException, IOException
159: {
160: MethodResult mr = invokeMethod(bb);
161:
162: Object obj = mr.getReturnedValue();
163: ObjectId oId = idMan.getObjectId(obj);
164: Exception exception = mr.getThrownException();
165: ObjectId eId = idMan.getObjectId(exception);
166:
167: oId.writeTagged(os);
168: eId.writeTagged(os);
169: }
170:
171:
174: private MethodResult invokeMethod(ByteBuffer bb) throws JdwpException,
175: IOException
176: {
177: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
178: Class clazz = refId.getType();
179:
180: ObjectId tId = idMan.readObjectId(bb);
181: Thread thread = (Thread) tId.getObject();
182:
183: ObjectId mId = idMan.readObjectId(bb);
184: Method method = (Method) mId.getObject();
185:
186: int args = bb.getInt();
187: Object[] values = new Object[args];
188:
189: for (int i = 0; i < args; i++)
190: {
191: values[i] = Value.getObj(bb);
192: }
193:
194: int invokeOpts = bb.getInt();
195: boolean suspend = ((invokeOpts
196: & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
197: != 0);
198: try
199: {
200: if (suspend)
201: VMVirtualMachine.suspendAllThreads ();
202:
203: MethodResult mr = VMVirtualMachine.executeMethod(null, thread,
204: clazz, method,
205: values, false);
206: if (suspend)
207: VMVirtualMachine.resumeAllThreads ();
208:
209: return mr;
210: }
211: catch (Exception ex)
212: {
213: if (suspend)
214: VMVirtualMachine.resumeAllThreads ();
215:
216: throw new JdwpInternalErrorException(ex);
217: }
218: }
219: }