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: import ;
53: import ;
54: import ;
55:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
67: public class ObjectReferenceCommandSet
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.ObjectReference.REFERENCE_TYPE:
78: executeReferenceType(bb, os);
79: break;
80: case JdwpConstants.CommandSet.ObjectReference.GET_VALUES:
81: executeGetValues(bb, os);
82: break;
83: case JdwpConstants.CommandSet.ObjectReference.SET_VALUES:
84: executeSetValues(bb, os);
85: break;
86: case JdwpConstants.CommandSet.ObjectReference.MONITOR_INFO:
87: executeMonitorInfo(bb, os);
88: break;
89: case JdwpConstants.CommandSet.ObjectReference.INVOKE_METHOD:
90: executeInvokeMethod(bb, os);
91: break;
92: case JdwpConstants.CommandSet.ObjectReference.DISABLE_COLLECTION:
93: executeDisableCollection(bb, os);
94: break;
95: case JdwpConstants.CommandSet.ObjectReference.ENABLE_COLLECTION:
96: executeEnableCollection(bb, os);
97: break;
98: case JdwpConstants.CommandSet.ObjectReference.IS_COLLECTED:
99: executeIsCollected(bb, os);
100: break;
101: default:
102: throw new NotImplementedException("Command " + command +
103: " not found in ObjectReference Command Set.");
104: }
105: }
106: catch (IOException ex)
107: {
108:
109:
110: throw new JdwpInternalErrorException(ex);
111: }
112:
113: return false;
114: }
115:
116: private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
117: throws JdwpException, IOException
118: {
119: ObjectId oid = idMan.readObjectId(bb);
120: Object obj = oid.getObject();
121: Class clazz = obj.getClass();
122: ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
123: refId.writeTagged(os);
124: }
125:
126: private void executeGetValues(ByteBuffer bb, DataOutputStream os)
127: throws JdwpException, IOException
128: {
129: ObjectId oid = idMan.readObjectId(bb);
130: Object obj = oid.getObject();
131:
132: int numFields = bb.getInt();
133:
134: os.writeInt(numFields);
135:
136: for (int i = 0; i < numFields; i++)
137: {
138: Field field = (Field) idMan.readObjectId(bb).getObject();
139: try
140: {
141: field.setAccessible(true);
142: Object value = field.get(obj);
143: Value val = ValueFactory.createFromObject(value,
144: field.getType());
145: val.writeTagged(os);
146: }
147: catch (IllegalArgumentException ex)
148: {
149:
150: throw new InvalidFieldException(ex);
151: }
152: catch (IllegalAccessException ex)
153: {
154:
155: throw new JdwpInternalErrorException(ex);
156: }
157: }
158: }
159:
160: private void executeSetValues(ByteBuffer bb, DataOutputStream os)
161: throws JdwpException, IOException
162: {
163: ObjectId oid = idMan.readObjectId(bb);
164: Object obj = oid.getObject();
165:
166: int numFields = bb.getInt();
167:
168: for (int i = 0; i < numFields; i++)
169: {
170: Field field = (Field) idMan.readObjectId(bb).getObject();
171: Object value = Value.getUntaggedObject(bb, field.getType());
172: try
173: {
174: field.setAccessible(true);
175: field.set(obj, value);
176: }
177: catch (IllegalArgumentException ex)
178: {
179:
180: throw new InvalidFieldException(ex);
181: }
182: catch (IllegalAccessException ex)
183: {
184:
185: throw new JdwpInternalErrorException(ex);
186: }
187: }
188: }
189:
190: private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os)
191: throws JdwpException, IOException
192: {
193: if (!VMVirtualMachine.canGetMonitorInfo)
194: {
195: String msg = "getting monitor info not supported";
196: throw new NotImplementedException(msg);
197: }
198:
199: ObjectId oid = idMan.readObjectId(bb);
200: Object obj = oid.getObject();
201: MonitorInfo info = VMVirtualMachine.getMonitorInfo(obj);
202: info.write(os);
203: }
204:
205: private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
206: throws JdwpException, IOException
207: {
208: ObjectId oid = idMan.readObjectId(bb);
209: Object obj = oid.getObject();
210:
211: ObjectId tid = idMan.readObjectId(bb);
212: Thread thread = (Thread) tid.getObject();
213:
214: ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
215: Class clazz = rid.getType();
216:
217: VMMethod method = VMMethod.readId(clazz, bb);
218:
219: int args = bb.getInt();
220: Value[] values = new Value[args];
221:
222: for (int i = 0; i < args; i++)
223: values[i] = ValueFactory.createFromTagged(bb);
224:
225: int invokeOptions = bb.getInt();
226: MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
227: clazz, method,
228: values, invokeOptions);
229: Throwable exception = mr.getThrownException();
230: ObjectId eId = idMan.getObjectId(exception);
231: mr.getReturnedValue().writeTagged(os);
232: eId.writeTagged(os);
233: }
234:
235: private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
236: throws JdwpException, IOException
237: {
238: ObjectId oid = idMan.readObjectId(bb);
239: oid.disableCollection();
240: }
241:
242: private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
243: throws JdwpException, IOException
244: {
245: ObjectId oid = idMan.readObjectId(bb);
246: oid.enableCollection();
247: }
248:
249: private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
250: throws JdwpException, IOException
251: {
252: ObjectId oid = idMan.readObjectId(bb);
253: boolean collected = (oid.getReference().get () == null);
254: os.writeBoolean(collected);
255: }
256: }