classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [PATCH/JDWP] ID refinements


From: Keith Seitz
Subject: [cp-patches] [PATCH/JDWP] ID refinements
Date: Sat, 20 Aug 2005 12:10:50 -0700
User-agent: Mozilla Thunderbird 1.0.6-1.1.fc4 (X11/20050720)

Hello,

I've committed the following patch which does two things: 1) add convenience methods to all the IDs to fetch the underlying object (throwing the appropriate exception when the ID is invalid) and 2) a few miscellaneous cleanups/additions to ObjectId (implement disable/enable garbage collection, etc).

Keith

ChangeLog
2005-08-20  Keith Seitz  <address@hidden>

        * gnu/classpath/jdwp/id/ClassLoaderId.java (getClassLoader): New
        method.
        * gnu/classpath/jdwp/id/ClassObjectId.java (getClassObject): New
        method.
        * gnu/classpath/jdwp/id/JdwpId.java
        (getReference): New method.
        (setReference): New method.
        * gnu/classpath/jdwp/id/ObjectId.java (getObject): New method.
        * gnu/classpath/jdwp/id/ReferenceTypeId.java (getType): New
         method.
        * gnu/classpath/jdwp/id/StringId.java (getString): New method.
        * gnu/classpath/jdwp/id/ThreadGroupId.java (getThreadGroup): New
        method.
        * gnu/classpath/jdwp/id/ThreadId.java (getThread): New method.

        * gnu/classpath/jdwp/id/ObjectId.java (setId): New method.
        (disableCollection): New method.
        (enableCollection): New method.

        * gnu/classpath/jdwp/id/JdwpId.java (equals): Remove test for
        class equality.
        (setId): Make public.
Index: gnu/classpath/jdwp/id/ClassLoaderId.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ClassLoaderId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ClassLoaderId.java
--- gnu/classpath/jdwp/id/ClassLoaderId.java    2 Jul 2005 20:32:10 -0000       
1.2
+++ gnu/classpath/jdwp/id/ClassLoaderId.java    20 Aug 2005 19:07:26 -0000
@@ -40,6 +40,7 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidClassLoaderException;
 
 /**
  * A class which represents a JDWP thread id
@@ -60,5 +61,22 @@ public class ClassLoaderId
   public ClassLoaderId ()
   {
     super (JdwpConstants.Tag.CLASS_LOADER);
+  }
+
+  /**
+   * Gets the ClassLoader represented by this ID
+   *
+   * @throws InvalidClassLoaderException if ClassLoader is garbage collected,
+   *           or otherwise invalid
+   */
+  public ClassLoader getClassLoader ()
+    throws InvalidClassLoaderException
+  {
+    ClassLoader cl = (ClassLoader) _reference.get ();
+
+    if (cl == null)
+      throw new InvalidClassLoaderException (getId ());
+
+    return cl;
   }
 }
Index: gnu/classpath/jdwp/id/ClassObjectId.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ClassObjectId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ClassObjectId.java
--- gnu/classpath/jdwp/id/ClassObjectId.java    2 Jul 2005 20:32:10 -0000       
1.2
+++ gnu/classpath/jdwp/id/ClassObjectId.java    20 Aug 2005 19:07:26 -0000
@@ -40,6 +40,7 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidClassException;
 
 /**
  * A class which represents a JDWP class object id
@@ -60,5 +61,22 @@ public class ClassObjectId
   public ClassObjectId ()
   {
     super (JdwpConstants.Tag.CLASS_OBJECT);
+  }
+
+  /**
+   * Gets the Class object represented by this ID
+   *
+   * @throws InvalidClassException if Class is garbage collected,
+   *           or otherwise invalid
+   */
+  public Class getClassObject ()
+    throws InvalidClassException
+  {
+    Class cl = (Class) _reference.get ();
+
+    if (cl == null)
+      throw new InvalidClassException (getId ());
+
+    return cl;
   }
 }
Index: gnu/classpath/jdwp/id/JdwpId.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/classpath/jdwp/id/JdwpId.java,v
retrieving revision 1.2
diff -u -p -r1.2 JdwpId.java
--- gnu/classpath/jdwp/id/JdwpId.java   2 Jul 2005 20:32:10 -0000       1.2
+++ gnu/classpath/jdwp/id/JdwpId.java   20 Aug 2005 19:07:26 -0000
@@ -41,6 +41,7 @@ package gnu.classpath.jdwp.id;
 
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.lang.ref.SoftReference;
 
 /**
  * A baseclass for all object types reported to the debugger
@@ -62,6 +63,11 @@ public abstract class JdwpId
   private byte _tag;
 
   /**
+   * The object/class represented by this Id
+   */
+  protected SoftReference _reference;
+
+  /**
    * Constructs an empty <code>JdwpId</code>
    */
   public JdwpId (byte tag)
@@ -72,7 +78,7 @@ public abstract class JdwpId
   /**
    * Sets the id for this object reference
    */
-  void setId (long id)
+  public void setId (long id)
   {
     _id = id;
   }
@@ -86,15 +92,33 @@ public abstract class JdwpId
   }
 
   /**
+   * Gets the object/class reference for this ID
+   *
+   * @returns a refernce to the object or class
+   */
+  public SoftReference getReference ()
+  {
+    return _reference;
+  }
+
+  /**
+   * Sets the object/class reference for this ID
+   *
+   * @param ref a refernce to the object or class
+   */
+  public void setReference (SoftReference ref)
+  {
+    _reference = ref;
+  }
+
+  /**
    * Compares two object ids for equality. Two object ids
    * are equal if they point to the same type and contain to
-   * the same id number. (NOTE: This is a much stricter check
-   * than is necessary: all <code>JdwpId</code>s have unique
-   * ids.)
+   * the same id number. 
    */
   public boolean equals (JdwpId id)
   {
-    return ((id.getClass () == getClass ()) && (id.getId () == getId ()));
+    return (id.getId () == getId ());
   }
 
   /**
Index: gnu/classpath/jdwp/id/ObjectId.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ObjectId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ObjectId.java
--- gnu/classpath/jdwp/id/ObjectId.java 2 Jul 2005 20:32:10 -0000       1.2
+++ gnu/classpath/jdwp/id/ObjectId.java 20 Aug 2005 19:07:26 -0000
@@ -40,12 +40,15 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidObjectException;
 
 import java.io.DataOutputStream;
 import java.io.IOException;
 
 /**
- * A class which represents a JDWP object id for an object
+ * This is a base class for all ObjectID-like entities in JDWP,
+ * inculding Objects, ClassObject, ClassLoader, Thread, ThreadGroup,
+ * etc.
  *
  * @author Keith Seitz  <address@hidden>
  */
@@ -57,6 +60,9 @@ public class ObjectId
    */
   public static final Class typeClass = Object.class;
 
+  // Handle to disable garbage collection
+  private Object _handle;
+
   /**
    * Constructs a new <code>ObjectId</code>
    */
@@ -85,6 +91,23 @@ public class ObjectId
   }
 
   /**
+   * Returns the object referred to by this ID
+   *
+   * @returns the object
+   * @throws InvalidObjectException if the object was garbage collected
+   *           or is invalid
+   */
+  public Object getObject ()
+    throws InvalidObjectException
+  {
+    Object obj = _reference.get ();
+    if (obj == null)
+      throw new InvalidObjectException (_id);
+
+    return obj;
+  }
+
+  /**
    * Writes the id to the stream
    *
    * @param outStream  the stream to which to write
@@ -95,5 +118,22 @@ public class ObjectId
   {
     // All we need to do is write out our id as an 8-byte integer
     outStream.writeLong (_id);
+  }
+
+  /**
+   * Disable garbage collection on object
+   */
+  public void disableCollection ()
+    throws InvalidObjectException
+  {
+    _handle = getObject ();
+  }
+
+  /**
+   * Enable garbage collection on object
+   */
+  public void enableCollection ()
+  {
+    _handle = null;
   }
 }
Index: gnu/classpath/jdwp/id/ReferenceTypeId.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ReferenceTypeId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ReferenceTypeId.java
--- gnu/classpath/jdwp/id/ReferenceTypeId.java  2 Jul 2005 20:32:10 -0000       
1.2
+++ gnu/classpath/jdwp/id/ReferenceTypeId.java  20 Aug 2005 19:07:26 -0000
@@ -39,6 +39,8 @@ exception statement from your version. *
 
 package gnu.classpath.jdwp.id;
 
+import gnu.classpath.jdwp.exception.InvalidClassException;
+
 import java.io.DataOutputStream;
 import java.io.IOException;
 
@@ -65,6 +67,22 @@ public class ReferenceTypeId
   public int size ()
   {
     return 8;
+  }
+
+  /**
+   * Gets the class associated with this ID
+   *
+   * @returns the class
+   * @throws InvalidClassException if the class is not valid
+   */
+  public Class getType ()
+    throws InvalidClassException
+  {
+    Class clazz = (Class) _reference.get ();
+    if (clazz == null)
+      throw new InvalidClassException (_id);
+
+    return clazz;
   }
 
   /**
Index: gnu/classpath/jdwp/id/StringId.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/classpath/jdwp/id/StringId.java,v
retrieving revision 1.2
diff -u -p -r1.2 StringId.java
--- gnu/classpath/jdwp/id/StringId.java 2 Jul 2005 20:32:10 -0000       1.2
+++ gnu/classpath/jdwp/id/StringId.java 20 Aug 2005 19:07:26 -0000
@@ -40,6 +40,7 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidStringException;
 
 /**
  * A class which represents a JDWP string id
@@ -60,5 +61,22 @@ public class StringId
   public StringId ()
   {
     super (JdwpConstants.Tag.STRING);
+  }
+
+  /**
+   * Gets the String represented by this ID
+   *
+   * @throws InvalidStringException if String is garbage collected,
+   *           or otherwise invalid
+   */
+  public String getString ()
+    throws InvalidStringException
+  {
+    String string = (String) _reference.get ();
+
+    if (string == null)
+      throw new InvalidStringException (getId ());
+
+    return string;
   }
 }
Index: gnu/classpath/jdwp/id/ThreadGroupId.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ThreadGroupId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ThreadGroupId.java
--- gnu/classpath/jdwp/id/ThreadGroupId.java    2 Jul 2005 20:32:10 -0000       
1.2
+++ gnu/classpath/jdwp/id/ThreadGroupId.java    20 Aug 2005 19:07:26 -0000
@@ -40,6 +40,7 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidThreadGroupException;
 
 /**
  * A class which represents a JDWP thread group id
@@ -60,5 +61,22 @@ public class ThreadGroupId
   public ThreadGroupId ()
   {
     super (JdwpConstants.Tag.THREAD_GROUP);
+  }
+
+  /**
+   * Gets the thread group represented by this ID
+   *
+   * @throws InvalidThreadGroupException if the group is invalid
+   *           or garbage collected
+   */
+  public ThreadGroup getThreadGroup ()
+    throws InvalidThreadGroupException
+  {
+    ThreadGroup group = (ThreadGroup) _reference.get ();
+
+    if (group == null)
+      throw new InvalidThreadGroupException (getId ());
+
+    return group;
   }
 }
Index: gnu/classpath/jdwp/id/ThreadId.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/classpath/jdwp/id/ThreadId.java,v
retrieving revision 1.2
diff -u -p -r1.2 ThreadId.java
--- gnu/classpath/jdwp/id/ThreadId.java 2 Jul 2005 20:32:10 -0000       1.2
+++ gnu/classpath/jdwp/id/ThreadId.java 20 Aug 2005 19:07:26 -0000
@@ -40,6 +40,7 @@ exception statement from your version. *
 package gnu.classpath.jdwp.id;
 
 import gnu.classpath.jdwp.JdwpConstants;
+import gnu.classpath.jdwp.exception.InvalidThreadException;
 
 /**
  * A class which represents a JDWP thread id
@@ -60,5 +61,25 @@ public class ThreadId
   public ThreadId ()
   {
     super (JdwpConstants.Tag.THREAD);
+  }
+
+  /**
+   * Gets the Thread represented by this ID
+   *
+   * @throws InvalidThreadException if thread is garbage collected,
+   *           exited, or otherwise invalid
+   */
+  public Thread getThread ()
+    throws InvalidThreadException
+  {
+    Thread thread = (Thread) _reference.get ();
+
+    /* Spec says if thread is null, not valid, or exited,
+       throw invalid thread */
+    // FIXME: not valid? exited? Is this check valid?
+    if (thread == null || !thread.isAlive ())
+      throw new InvalidThreadException (getId ());
+
+    return thread;
   }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]