1:
38:
39:
40: package ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
64: public class ObjectReferenceCommandSet
65: extends CommandSet
66: {
67: public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
68: throws JdwpException
69: {
70: try
71: {
72: switch (command)
73: {
74: case JdwpConstants.CommandSet.ObjectReference.REFERENCE_TYPE:
75: executeReferenceType(bb, os);
76: break;
77: case JdwpConstants.CommandSet.ObjectReference.GET_VALUES:
78: executeGetValues(bb, os);
79: break;
80: case JdwpConstants.CommandSet.ObjectReference.SET_VALUES:
81: executeSetValues(bb, os);
82: break;
83: case JdwpConstants.CommandSet.ObjectReference.MONITOR_INFO:
84: executeMonitorInfo(bb, os);
85: break;
86: case JdwpConstants.CommandSet.ObjectReference.INVOKE_METHOD:
87: executeInvokeMethod(bb, os);
88: break;
89: case JdwpConstants.CommandSet.ObjectReference.DISABLE_COLLECTION:
90: executeDisableCollection(bb, os);
91: break;
92: case JdwpConstants.CommandSet.ObjectReference.ENABLE_COLLECTION:
93: executeEnableCollection(bb, os);
94: break;
95: case JdwpConstants.CommandSet.ObjectReference.IS_COLLECTED:
96: executeIsCollected(bb, os);
97: break;
98: default:
99: throw new NotImplementedException("Command " + command +
100: " not found in ObjectReference Command Set.");
101: }
102: }
103: catch (IOException ex)
104: {
105:
106:
107: throw new JdwpInternalErrorException(ex);
108: }
109:
110: return false;
111: }
112:
113: private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
114: throws JdwpException, IOException
115: {
116: ObjectId oid = idMan.readObjectId(bb);
117: Object obj = oid.getObject();
118: Class clazz = obj.getClass();
119: ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
120: refId.writeTagged(os);
121: }
122:
123: private void executeGetValues(ByteBuffer bb, DataOutputStream os)
124: throws JdwpException, IOException
125: {
126: ObjectId oid = idMan.readObjectId(bb);
127: Object obj = oid.getObject();
128:
129: int numFields = bb.getInt();
130:
131: os.writeInt(numFields);
132:
133: for (int i = 0; i < numFields; i++)
134: {
135: Field field = (Field) idMan.readObjectId(bb).getObject();
136: try
137: {
138: field.setAccessible(true);
139: Object value = field.get(obj);
140: Value.writeTaggedValue(os, value);
141: }
142: catch (IllegalArgumentException ex)
143: {
144:
145: throw new InvalidFieldException(ex);
146: }
147: catch (IllegalAccessException ex)
148: {
149:
150: throw new JdwpInternalErrorException(ex);
151: }
152: }
153: }
154:
155: private void executeSetValues(ByteBuffer bb, DataOutputStream os)
156: throws JdwpException, IOException
157: {
158: ObjectId oid = idMan.readObjectId(bb);
159: Object obj = oid.getObject();
160:
161: int numFields = bb.getInt();
162:
163: for (int i = 0; i < numFields; i++)
164: {
165: Field field = (Field) idMan.readObjectId(bb).getObject();
166: Object value = Value.getUntaggedObj(bb, field.getType());
167: try
168: {
169: field.setAccessible(true);
170: field.set(obj, value);
171: }
172: catch (IllegalArgumentException ex)
173: {
174:
175: throw new InvalidFieldException(ex);
176: }
177: catch (IllegalAccessException ex)
178: {
179:
180: throw new JdwpInternalErrorException(ex);
181: }
182: }
183: }
184:
185: private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os)
186: throws JdwpException
187: {
188:
189:
190: throw new NotImplementedException(
191: "Command ExecuteMonitorInfo not implemented.");
192:
193: }
194:
195: private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
196: throws JdwpException, IOException
197: {
198: ObjectId oid = idMan.readObjectId(bb);
199: Object obj = oid.getObject();
200:
201: ObjectId tid = idMan.readObjectId(bb);
202: Thread thread = (Thread) tid.getObject();
203:
204: ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
205: Class clazz = rid.getType();
206:
207: ObjectId mid = idMan.readObjectId(bb);
208: Method method = (Method) mid.getObject();
209:
210: int args = bb.getInt();
211: Object[] values = new Object[args];
212:
213: for (int i = 0; i < args; i++)
214: {
215: values[i] = Value.getObj(bb);
216: }
217:
218: int invokeOptions = bb.getInt();
219: boolean suspend = ((invokeOptions
220: & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
221: != 0);
222: if (suspend)
223: {
224:
225: VMVirtualMachine.suspendAllThreads ();
226: }
227:
228: boolean nonVirtual = ((invokeOptions
229: & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL)
230: != 0);
231:
232: MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
233: clazz, method,
234: values, nonVirtual);
235: Object value = mr.getReturnedValue();
236: Exception exception = mr.getThrownException();
237:
238: ObjectId eId = idMan.getObjectId(exception);
239: Value.writeTaggedValue(os, value);
240: eId.writeTagged(os);
241: }
242:
243: private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
244: throws JdwpException, IOException
245: {
246: ObjectId oid = idMan.readObjectId(bb);
247: oid.disableCollection();
248: }
249:
250: private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
251: throws JdwpException, IOException
252: {
253: ObjectId oid = idMan.readObjectId(bb);
254: oid.enableCollection();
255: }
256:
257: private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
258: throws JdwpException, IOException
259: {
260: ObjectId oid = idMan.readObjectId(bb);
261: boolean collected = (oid.getReference().get () == null);
262: os.writeBoolean(collected);
263: }
264: }