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: if (superClazz == null) {
110: os.writeLong(0L);
111: } else {
112: ReferenceTypeId clazzId = idMan.getReferenceTypeId(superClazz);
113: clazzId.write(os);
114: }
115: }
116:
117: private void executeSetValues(ByteBuffer bb, DataOutputStream os)
118: throws JdwpException, IOException
119: {
120: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
121:
122:
123: Class clazz = refId.getType();
124:
125: int numValues = bb.getInt();
126:
127: for (int i = 0; i < numValues; i++)
128: {
129: ObjectId fieldId = idMan.readObjectId(bb);
130: Field field = (Field) (fieldId.getObject());
131: Object value = Value.getUntaggedObj(bb, field.getType());
132: try
133: {
134: field.setAccessible(true);
135: field.set(null, value);
136: }
137: catch (IllegalArgumentException ex)
138: {
139: throw new InvalidFieldException(ex);
140: }
141: catch (IllegalAccessException ex)
142: {
143: throw new JdwpInternalErrorException(ex);
144: }
145: }
146: }
147:
148: private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
149: throws JdwpException, IOException
150: {
151: MethodResult mr = invokeMethod(bb);
152:
153: Object value = mr.getReturnedValue();
154: Exception exception = mr.getThrownException();
155: ObjectId eId = idMan.getObjectId(exception);
156:
157: Value.writeTaggedValue(os, value);
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:
166: Object obj = mr.getReturnedValue();
167: ObjectId oId = idMan.getObjectId(obj);
168: Exception exception = mr.getThrownException();
169: ObjectId eId = idMan.getObjectId(exception);
170:
171: oId.writeTagged(os);
172: eId.writeTagged(os);
173: }
174:
175:
178: private MethodResult invokeMethod(ByteBuffer bb) throws JdwpException,
179: IOException
180: {
181: ReferenceTypeId refId = idMan.readReferenceTypeId(bb);
182: Class clazz = refId.getType();
183:
184: ObjectId tId = idMan.readObjectId(bb);
185: Thread thread = (Thread) tId.getObject();
186:
187: ObjectId mId = idMan.readObjectId(bb);
188: Method method = (Method) mId.getObject();
189:
190: int args = bb.getInt();
191: Object[] values = new Object[args];
192:
193: for (int i = 0; i < args; i++)
194: {
195: values[i] = Value.getObj(bb);
196: }
197:
198: int invokeOpts = bb.getInt();
199: boolean suspend = ((invokeOpts
200: & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
201: != 0);
202: try
203: {
204: if (suspend)
205: VMVirtualMachine.suspendAllThreads ();
206:
207: MethodResult mr = VMVirtualMachine.executeMethod(null, thread,
208: clazz, method,
209: values, false);
210: if (suspend)
211: VMVirtualMachine.resumeAllThreads ();
212:
213: return mr;
214: }
215: catch (Exception ex)
216: {
217: if (suspend)
218: VMVirtualMachine.resumeAllThreads ();
219:
220: throw new JdwpInternalErrorException(ex);
221: }
222: }
223: }