OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / classpath / jdwp / processor / ObjectReferenceCommandSet.java
1 /* ObjectReferenceCommandSet.java -- class to implement the ObjectReference
2    Command Set
3    Copyright (C) 2005 Free Software Foundation
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING.  If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library.  Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module.  An independent module is a module which is not derived from
34 or based on this library.  If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so.  If you do not wish to do so, delete this
37 exception statement from your version. */
38
39
40 package gnu.classpath.jdwp.processor;
41
42 import gnu.classpath.jdwp.JdwpConstants;
43 import gnu.classpath.jdwp.VMVirtualMachine;
44 import gnu.classpath.jdwp.exception.InvalidFieldException;
45 import gnu.classpath.jdwp.exception.JdwpException;
46 import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
47 import gnu.classpath.jdwp.exception.NotImplementedException;
48 import gnu.classpath.jdwp.id.ObjectId;
49 import gnu.classpath.jdwp.id.ReferenceTypeId;
50 import gnu.classpath.jdwp.util.Value;
51 import gnu.classpath.jdwp.util.MethodResult;
52
53 import java.io.DataOutputStream;
54 import java.io.IOException;
55 import java.lang.reflect.Field;
56 import java.lang.reflect.Method;
57 import java.nio.ByteBuffer;
58
59 /**
60  * A class representing the ObjectReference Command Set.
61  * 
62  * @author Aaron Luchko <aluchko@redhat.com>
63  */
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         // The DataOutputStream we're using isn't talking to a socket at all
106         // So if we throw an IOException we're in serious trouble
107         throw new JdwpInternalErrorException(ex);
108       }
109     return true;
110   }
111
112   private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
113     throws JdwpException, IOException
114   {
115     ObjectId oid = idMan.readObjectId(bb);
116     Object obj = oid.getObject();
117     Class clazz = obj.getClass();
118     ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
119     refId.writeTagged(os);
120   }
121
122   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
123     throws JdwpException, IOException
124   {
125     ObjectId oid = idMan.readObjectId(bb);
126     Object obj = oid.getObject();
127
128     int numFields = bb.getInt();
129
130     os.writeInt(numFields); // Looks pointless but this is the protocol
131
132     for (int i = 0; i < numFields; i++)
133       {
134         Field field = (Field) idMan.readObjectId(bb).getObject();
135         try
136           {
137             field.setAccessible(true); // Might be a private field
138             Object value = field.get(obj);
139             Value.writeTaggedValue(os, value);
140           }
141         catch (IllegalArgumentException ex)
142           {
143             // I suppose this would best qualify as an invalid field then
144             throw new InvalidFieldException(ex);
145           }
146         catch (IllegalAccessException ex)
147           {
148             // Since we set it as accessible this really shouldn't happen
149             throw new JdwpInternalErrorException(ex);
150           }
151       }
152   }
153
154   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
155     throws JdwpException, IOException
156   {
157     ObjectId oid = idMan.readObjectId(bb);
158     Object obj = oid.getObject();
159
160     int numFields = bb.getInt();
161
162     for (int i = 0; i < numFields; i++)
163       {
164         Field field = (Field) idMan.readObjectId(bb).getObject();
165         Object value = Value.getUntaggedObj(bb, field.getType());
166         try
167           {
168             field.setAccessible(true); // Might be a private field
169             field.set(obj, value);
170           }
171         catch (IllegalArgumentException ex)
172           {
173             // I suppose this would best qualify as an invalid field then
174             throw new InvalidFieldException(ex);
175           }
176         catch (IllegalAccessException ex)
177           {
178             // Since we set it as accessible this really shouldn't happen
179             throw new JdwpInternalErrorException(ex);
180           }
181       }
182   }
183
184   private void executeMonitorInfo(ByteBuffer bb, DataOutputStream os)
185     throws JdwpException
186   {
187     // This command is optional, determined by VirtualMachines CapabilitiesNew
188     // so we'll leave it till later to implement
189     throw new NotImplementedException(
190       "Command ExecuteMonitorInfo not implemented.");
191
192   }
193
194   private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
195     throws JdwpException, IOException
196   {
197     ObjectId oid = idMan.readObjectId(bb);
198     Object obj = oid.getObject();
199
200     ObjectId tid = idMan.readObjectId(bb);
201     Thread thread = (Thread) tid.getObject();
202
203     ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
204     Class clazz = rid.getType();
205
206     ObjectId mid = idMan.readObjectId(bb);
207     Method method = (Method) mid.getObject();
208
209     int args = bb.getInt();
210     Object[] values = new Object[args];
211
212     for (int i = 0; i < args; i++)
213       {
214         values[i] = Value.getObj(bb);
215       }
216
217     int invokeOptions = bb.getInt();
218     boolean suspend = ((invokeOptions
219                         & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
220                        != 0);
221     if (suspend)
222       {
223         // We must suspend all other running threads first
224         VMVirtualMachine.suspendAllThreads ();
225       }
226
227     boolean nonVirtual = ((invokeOptions
228                            & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL)
229                           != 0);
230
231     MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
232                                                      clazz, method,
233                                                      values, nonVirtual);
234     Object value = mr.getReturnedValue();
235     Exception exception = mr.getThrownException();
236
237     ObjectId eId = idMan.getObjectId(exception);
238     Value.writeTaggedValue(os, value);
239     eId.writeTagged(os);
240   }
241
242   private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
243     throws JdwpException, IOException
244   {
245     ObjectId oid = idMan.readObjectId(bb);
246     oid.disableCollection();
247   }
248
249   private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
250     throws JdwpException, IOException
251   {
252     ObjectId oid = idMan.readObjectId(bb);
253     oid.enableCollection();
254   }
255
256   private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
257     throws JdwpException, IOException
258   {
259     ObjectId oid = idMan.readObjectId(bb);
260     boolean collected = (oid.getReference().get () == null);
261     os.writeBoolean(collected);
262   }
263 }