Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / debugger / command / GetThis.java
1 package jde.debugger.command;
2 import java.util.Iterator;
3 import java.util.Map;
4
5 import com.sun.jdi.IncompatibleThreadStateException;
6 import com.sun.jdi.InvalidStackFrameException;
7 import com.sun.jdi.ObjectCollectedException;
8 import com.sun.jdi.ObjectReference;
9 import com.sun.jdi.StackFrame;
10 import com.sun.jdi.ThreadReference;
11 import com.sun.jdi.Value;
12 import jde.debugger.Etc;
13 import jde.debugger.JDEException;
14 import jde.debugger.Rep;
15
16
17
18 /**
19  * Gets the "this" object of a specified stack frame.
20  * <p>
21  *
22  * <b>Syntax:</b>
23  * <pre>
24  * get_this threadID stackFrameIndex
25  * </pre>
26  *
27  * <b>Returns:</b>
28  * <pre>
29  *  (jde-dbo-command-result cmd_id {@link Rep#getObjectRep(ObjectReference) detailed-object-info})
30  * </pre>
31  *
32  * <b>Comments:</b>
33  * <ul>
34  * <li> Note that stackFrameIndex = 0 corresponds to the
35  * current stackframe.
36  * <li> The threadID and stackFrameIndex can be got from the
37  *      'get_threads' command.
38  * </ul>
39  *
40  * 
41  * @author Paul Kinnucan
42  * @version $Revision: 1.4 $
43  * @copyright Copyright (c) 2000, 2001, 2003    Paul Kinnucan
44  */
45 public class GetThis extends DebugProcessCommand {
46   
47   /**
48    *
49    * @exception jde.debugger.JDEException <description>
50    */
51   public void doCommand() throws JDEException {
52         
53     boolean weSuspendedThread = false;
54     ThreadReference tRef = null;
55         
56     if (m_args.size() != 2) 
57       throw new JDEException("Insufficient arguments");
58         
59     try {
60
61       Long   uniqueID   = Etc.safeGetLong(m_args.remove(0), "thread ID");
62       int    frameIndex = Etc.safeGetint(m_args.remove(0), "frame index");
63       Object oRef       = m_debugger.getStore().get(uniqueID);
64             
65       if (oRef == null) {
66         throw new JDEException("No such thread exists");
67       } else if (!(oRef instanceof ThreadReference)) {
68         throw new JDEException("Object is not a thread");
69       }
70             
71       tRef = (ThreadReference) oRef;
72             
73       if (!tRef.isSuspended()) {
74         tRef.suspend();
75         weSuspendedThread = true;
76       } 
77
78       StackFrame frame = null;
79       try {
80         frame = tRef.frame(frameIndex);
81       } catch (IncompatibleThreadStateException ex) {
82         throw new JDEException("Thread is not suspended");
83       } catch (IndexOutOfBoundsException ex) {
84         throw new JDEException("Invalid frame");
85       } catch (ObjectCollectedException ex) {
86         throw new JDEException("The frame has already been garbage collected");
87       }
88
89       if (frame == null) {
90         throw new JDEException("Error ascertaining frame");
91       }
92
93       ObjectReference thisObj = null;
94       try {
95         thisObj = frame.thisObject();
96       } catch (InvalidStackFrameException ex) {
97         throw new JDEException("Invalid stack frame.");
98       } 
99             
100       if (thisObj != null) {
101         // Keep track of this object, for future reference from the UI
102         m_debugger.getStore().put(thisObj);
103                 
104         // store the fields in this object that themselves are objects in the 
105         // ObjectStore, since the user may query for more information about those.
106         Map  fieldValues = thisObj.getValues(thisObj.referenceType().visibleFields());
107                 
108         Iterator iter = fieldValues.values().iterator(); 
109         while (iter.hasNext()) {
110           Value value = (Value) iter.next();
111                     
112           if (value instanceof ObjectReference) {
113             ObjectReference obj = (ObjectReference) value;
114                         
115             //                  Debug.printIf(Debug.EVENTS, "storing field with ID: " + obj.uniqueID() + 
116             //                                " of type: " + obj.referenceType().name());
117             m_debugger.getStore().put((ObjectReference) value);
118           }
119         }
120
121         // XXX - report 'this' to the GUI
122       }
123                 
124       m_debugger.signalCommandResult(m_cmdID, 
125                                      Rep.getObjectRep(thisObj, true),
126                                      CMD_OK);
127             
128     } finally {
129       if (weSuspendedThread && (tRef != null)) tRef.resume();
130     }
131  
132   }
133
134   public Object clone() {return new GetThis();}
135   
136 } // GetThis
137
138 /*
139  * $Log: GetThis.java,v $
140  * Revision 1.4  2003/01/15 05:56:26  paulk
141  * Add Petter Mahlen's changes.
142  *
143  * Revision 1.3  2001/03/24 05:42:36  paulk
144  * Updated to reflect reorganization of debugger code.
145  *
146  * Revision 1.2  2000/08/14 02:42:16  paulk
147  * DebugCommandFactory.java
148  *
149  * Revision 1.1  2000/04/10 05:35:23  paulk
150  * Initial revision.
151  *
152  *
153  */
154
155 // End of GetTbis.java