OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / classpath / jdwp / processor / ObjectReferenceCommandSet.java
index 38bb0cd..23a64c3 100644 (file)
@@ -1,4 +1,4 @@
-/* ObjectReferenceCommandSet.java -- lass to implement the ObjectReference
+/* ObjectReferenceCommandSet.java -- class to implement the ObjectReference
    Command Set
    Copyright (C) 2005 Free Software Foundation
 
@@ -36,20 +36,19 @@ this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+
 package gnu.classpath.jdwp.processor;
 
-import gnu.classpath.jdwp.IVirtualMachine;
-import gnu.classpath.jdwp.Jdwp;
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.VMVirtualMachine;
 import gnu.classpath.jdwp.exception.InvalidFieldException;
 import gnu.classpath.jdwp.exception.JdwpException;
 import gnu.classpath.jdwp.exception.JdwpInternalErrorException;
 import gnu.classpath.jdwp.exception.NotImplementedException;
-import gnu.classpath.jdwp.id.IdManager;
 import gnu.classpath.jdwp.id.ObjectId;
 import gnu.classpath.jdwp.id.ReferenceTypeId;
 import gnu.classpath.jdwp.util.Value;
-import gnu.classpath.jdwp.util.MethodInvoker;
+import gnu.classpath.jdwp.util.MethodResult;
 
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -62,14 +61,9 @@ import java.nio.ByteBuffer;
  * 
  * @author Aaron Luchko <aluchko@redhat.com>
  */
-public class ObjectReferenceCommandSet implements CommandSet
+public class ObjectReferenceCommandSet
+  extends CommandSet
 {
-  // Our hook into the jvm
-  private final IVirtualMachine vm = Jdwp.getIVirtualMachine();
-
-  // Manages all the different ids that are assigned by jdwp
-  private final IdManager idMan = Jdwp.getIdManager();
-
   public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
     throws JdwpException
   {
@@ -103,7 +97,7 @@ public class ObjectReferenceCommandSet implements CommandSet
             break;
           default:
             throw new NotImplementedException("Command " + command +
-              " not found in String Reference Command Set.");
+              " not found in ObjectReference Command Set.");
           }
       }
     catch (IOException ex)
@@ -118,7 +112,7 @@ public class ObjectReferenceCommandSet implements CommandSet
   private void executeReferenceType(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     Object obj = oid.getObject();
     Class clazz = obj.getClass();
     ReferenceTypeId refId = idMan.getReferenceTypeId(clazz);
@@ -128,7 +122,7 @@ public class ObjectReferenceCommandSet implements CommandSet
   private void executeGetValues(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     Object obj = oid.getObject();
 
     int numFields = bb.getInt();
@@ -137,25 +131,41 @@ public class ObjectReferenceCommandSet implements CommandSet
 
     for (int i = 0; i < numFields; i++)
       {
-        Field field = (Field) idMan.readId(bb).getObject();
-        Value.writeValueFromField(os, field, obj);
+        Field field = (Field) idMan.readObjectId(bb).getObject();
+        try
+          {
+            field.setAccessible(true); // Might be a private field
+            Object value = field.get(obj);
+            Value.writeTaggedValue(os, value);
+          }
+        catch (IllegalArgumentException ex)
+          {
+            // I suppose this would best qualify as an invalid field then
+            throw new InvalidFieldException(ex);
+          }
+        catch (IllegalAccessException ex)
+          {
+            // Since we set it as accessible this really shouldn't happen
+            throw new JdwpInternalErrorException(ex);
+          }
       }
   }
 
   private void executeSetValues(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     Object obj = oid.getObject();
 
     int numFields = bb.getInt();
 
     for (int i = 0; i < numFields; i++)
       {
-        Field field = (Field) idMan.readId(bb).getObject();
-        Object value = Value.getObj(bb, field);
+        Field field = (Field) idMan.readObjectId(bb).getObject();
+        Object value = Value.getUntaggedObj(bb, field.getType());
         try
           {
+            field.setAccessible(true); // Might be a private field
             field.set(obj, value);
           }
         catch (IllegalArgumentException ex)
@@ -165,7 +175,7 @@ public class ObjectReferenceCommandSet implements CommandSet
           }
         catch (IllegalAccessException ex)
           {
-            // We should be able to access any field
+            // Since we set it as accessible this really shouldn't happen
             throw new JdwpInternalErrorException(ex);
           }
       }
@@ -184,16 +194,16 @@ public class ObjectReferenceCommandSet implements CommandSet
   private void executeInvokeMethod(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     Object obj = oid.getObject();
 
-    ObjectId tid = idMan.readId(bb);
+    ObjectId tid = idMan.readObjectId(bb);
     Thread thread = (Thread) tid.getObject();
 
     ReferenceTypeId rid = idMan.readReferenceTypeId(bb);
     Class clazz = rid.getType();
 
-    ObjectId mid = idMan.readId(bb);
+    ObjectId mid = idMan.readObjectId(bb);
     Method method = (Method) mid.getObject();
 
     int args = bb.getInt();
@@ -205,45 +215,49 @@ public class ObjectReferenceCommandSet implements CommandSet
       }
 
     int invokeOptions = bb.getInt();
-
-    if ((invokeOptions & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED) != 0)
-      { // We must suspend all other running threads first
-        vm.suspendAllThreads();
+    boolean suspend = ((invokeOptions
+                       & JdwpConstants.InvokeOptions.INVOKE_SINGLE_THREADED)
+                      != 0);
+    if (suspend)
+      {
+       // We must suspend all other running threads first
+        VMVirtualMachine.suspendAllThreads ();
       }
-    boolean nonVirtual;
-    if ((invokeOptions & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL) != 0)
-      nonVirtual = true;
-    else
-      nonVirtual = false;
-    MethodInvoker vmi = new MethodInvoker(vm);
-
-    vmi.executeMethod(obj, thread, clazz, method, values, nonVirtual);
-    Object value = vmi.getReturnedValue();
-    ObjectId exceptionId = vmi.getExceptionId();
-    
-    Value.writeValue(os, value);
-    exceptionId.writeTagged(os);
+
+    boolean nonVirtual = ((invokeOptions
+                          & JdwpConstants.InvokeOptions.INVOKE_NONVIRTUAL)
+                         != 0);
+
+    MethodResult mr = VMVirtualMachine.executeMethod(obj, thread,
+                                                    clazz, method,
+                                                    values, nonVirtual);
+    Object value = mr.getReturnedValue();
+    Exception exception = mr.getThrownException();
+
+    ObjectId eId = idMan.getObjectId(exception);
+    Value.writeTaggedValue(os, value);
+    eId.writeTagged(os);
   }
 
   private void executeDisableCollection(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     oid.disableCollection();
   }
 
   private void executeEnableCollection(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
+    ObjectId oid = idMan.readObjectId(bb);
     oid.enableCollection();
   }
 
   private void executeIsCollected(ByteBuffer bb, DataOutputStream os)
     throws JdwpException, IOException
   {
-    ObjectId oid = idMan.readId(bb);
-    boolean collected = oid.isCollected();
+    ObjectId oid = idMan.readObjectId(bb);
+    boolean collected = (oid.getReference().get () == null);
     os.writeBoolean(collected);
   }
 }