KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > models > ArrayFieldVariable


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.models;
21
22 import com.sun.jdi.*;
23 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
24 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
25
26
27 /**
28  * @author Jan Jancura
29  */

30 class ArrayFieldVariable extends AbstractVariable implements
31 org.netbeans.api.debugger.jpda.Field {
32
33     final ArrayReference array;
34     int index;
35     int maxIndexLog;
36     private String JavaDoc declaredType;
37
38     ArrayFieldVariable (
39         JPDADebuggerImpl debugger,
40         Value value,
41         String JavaDoc declaredType,
42         ArrayReference array,
43         int index,
44         int maxIndex,
45         String JavaDoc parentID
46     ) {
47         super (
48             debugger,
49             value,
50             parentID + '.' + index +
51                 (value instanceof ObjectReference ? "^" : "")
52         );
53         this.index = index;
54         this.maxIndexLog = log10(maxIndex);
55         this.declaredType = declaredType;
56         this.array = array;
57     }
58
59     
60     // LocalVariable impl.......................................................
61

62
63     /**
64     * Returns string representation of type of this variable.
65     *
66     * @return string representation of type of this variable.
67     */

68     public String JavaDoc getName () {
69         int num0 = maxIndexLog - log10(index);
70         if (num0 > 0) {
71             return "[" + zeros(num0) + index + "]";
72         } else {
73             return "[" + index + "]";
74         }
75     }
76     
77     static int log10(int n) {
78         int l = 1;
79         while ((n = n / 10) > 0) l++;
80         return l;
81     }
82     
83     //private static final String ZEROS = "000000000000"; // NOI18N
84
private static final String JavaDoc ZEROS = " "; // NOI18N
85

86     static String JavaDoc zeros(int n) {
87         if (n < ZEROS.length()) {
88             return ZEROS.substring(0, n);
89         } else {
90             String JavaDoc z = ZEROS;
91             while (z.length() < n) z += " "; // NOI18N
92
return z;
93         }
94     }
95
96     /**
97      * Returns name of enclosing class.
98      *
99      * @return name of enclosing class
100      */

101     public String JavaDoc getClassName () {
102         return getType ();
103     }
104
105     /**
106      * Returns <code>true</code> for static fields.
107      *
108      * @return <code>true</code> for static fields
109      */

110     public boolean isStatic () {
111         return false;
112     }
113     
114     /**
115     * Returns string representation of type of this variable.
116     *
117     * @return string representation of type of this variable.
118     */

119     public String JavaDoc getDeclaredType () {
120         return declaredType;
121     }
122
123     /**
124      * Sets new value of this variable.
125      *
126      * @param value ne value
127      * @throws InvalidExpressionException if the value is invalid
128      */

129     protected void setValue (Value value) throws InvalidExpressionException {
130         try {
131             array.setValue(index, value);
132         } catch (InvalidTypeException ex) {
133             throw new InvalidExpressionException (ex);
134         } catch (ClassNotLoadedException ex) {
135             throw new InvalidExpressionException (ex);
136         }
137     }
138
139     public ArrayFieldVariable clone() {
140         ArrayFieldVariable clon = new ArrayFieldVariable(
141                 getDebugger(),
142                 getJDIValue(),
143                 declaredType,
144                 array,
145                 index,
146                 0,
147                 getID().substring(0, getID().length() - ('.' + index + (getJDIValue() instanceof ObjectReference ? "^" : "")).length()));
148         clon.maxIndexLog = this.maxIndexLog;
149         return clon;
150     }
151     
152     // other methods ...........................................................
153

154     public String JavaDoc toString () {
155         return "FieldVariable " + getName ();
156     }
157 }
158
159
Popular Tags