Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / debugger / command / GetArray.java
1 package jde.debugger.command;
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import com.sun.jdi.ArrayReference;
7 import com.sun.jdi.ObjectReference;
8 import com.sun.jdi.Value;
9 import jde.debugger.Etc;
10 import jde.debugger.JDEException;
11 import jde.debugger.JDE;
12 import jde.debugger.Rep;
13
14 /**
15  * 'get_array' command. Information about a given array, and,
16  * optionally, values of a range of indices
17  * <p>
18  *
19  * <b>Syntax:</b>
20  * <pre>
21  * get_array objectID [index, length]
22  * </pre>
23  *
24  * <b>Returns:</b>
25  * <pre>
26  * (jde-dbo-command-result cmd_id {@link Rep#getArrayRep(ArrayReference, String) array})
27  * </pre>
28  *
29  * Copyright (c) 2000, 2001, 2003    Paul Kinnucan
30  * 
31  * @author Paul Kinnucan
32  * @version $Revision: 1.3 $
33  */
34 public class GetArray extends DebugProcessCommand {
35   private static final int MAX_DISPLAY_ELEMENTS = 30;
36   
37   /**
38    *
39    * @exception jde.debugger.JDEException <description>
40    */
41   public void doCommand() throws JDEException {
42
43     if (m_args.size() < 1)
44       throw new JDEException("Insufficient arguments");
45
46     Long            uniqueID = Etc.safeGetLong(m_args.remove(0), "object ID");
47     ObjectReference oRef     = m_debugger.getStore().get(uniqueID);
48             
49     if (oRef == null) {
50       throw new JDEException("No such object exists");
51     } else if (!(oRef instanceof ArrayReference)) {
52       throw new JDEException("Object is not an array");
53     }
54
55     // Keep track of this array for later reference.
56     m_debugger.getStore().put(oRef);
57         
58     if (m_args.size() == 0) {
59       m_debugger.signalCommandResult(m_cmdID, 
60                                      Rep.getArrayRep((ArrayReference)oRef, ""),
61                                      CMD_OK, NOQUOTE);
62     } else if (m_args.size() == 2) {
63       StringBuffer elements = new StringBuffer();
64             
65       int index  = Etc.safeGetint(m_args.remove(0), "index");
66       int length = Etc.safeGetint(m_args.remove(0), "length");
67             
68       List     elementList = ((ArrayReference) oRef).getValues(index, length);
69       Iterator it          = elementList.iterator();
70       int      numElements = 0;
71             
72       while (it.hasNext() && numElements < MAX_DISPLAY_ELEMENTS) {
73         numElements++;
74         Value value = (Value) it.next();
75                 
76         // store the fields in this object that themselves are objects in the 
77         // ObjectStore, since the user may query for more information about those.
78         if (value instanceof ObjectReference) {
79           m_debugger.getStore().put((ObjectReference) value);
80         }
81                 
82         elements.append(" ");
83         elements.append(Rep.getValueRep(value));
84       }
85             
86       if (it.hasNext()) {
87         JDE.debug(EVENTS, "did not list all elements");
88         // XXX - should be a way to indicate to the debugger that there are more elts.
89       }
90             
91       m_debugger.signalCommandResult(m_cmdID, 
92                                      Rep.getArrayRep((ArrayReference)oRef, elements.toString()),
93                                      CMD_OK, NOQUOTE);
94     } else {
95       throw new JDEException("Syntax error: Wrong number of arguments");
96     }
97  
98   }
99
100   public Object clone() {return new GetArray();}
101   
102 } // GetArray
103
104 /*
105  * $Log: GetArray.java,v $
106  * Revision 1.3  2003/01/15 05:56:26  paulk
107  * Add Petter Mahlen's changes.
108  *
109  * Revision 1.2  2001/03/24 05:42:36  paulk
110  * Updated to reflect reorganization of debugger code.
111  *
112  * Revision 1.1  2000/03/03 07:08:39  paulk
113  * Initial revision.
114  *
115  */
116
117 // End of GetArray.java