KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > JavaVariablesFilter


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.ui.models;
21
22 import java.util.HashSet JavaDoc;
23 import org.netbeans.api.debugger.jpda.Field;
24 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
25 import org.netbeans.api.debugger.jpda.ObjectVariable;
26 import org.netbeans.api.debugger.jpda.Variable;
27 import org.netbeans.spi.debugger.jpda.VariablesFilterAdapter;
28 import org.netbeans.spi.debugger.ui.Constants;
29 import org.netbeans.spi.viewmodel.TableModel;
30 import org.netbeans.spi.viewmodel.TreeModel;
31 import org.netbeans.spi.viewmodel.UnknownTypeException;
32 import org.openide.ErrorManager;
33
34
35 /**
36  *
37  * @author Jan Jancura
38  */

39 public class JavaVariablesFilter extends VariablesFilterAdapter {
40     
41     public String JavaDoc[] getSupportedTypes () {
42         return new String JavaDoc[] {
43             "java.lang.String",
44             "java.lang.StringBuffer",
45             
46             "java.lang.Character",
47             "java.lang.Integer",
48             "java.lang.Float",
49             "java.lang.Byte",
50             "java.lang.Boolean",
51             "java.lang.Double",
52             "java.lang.Long",
53             "java.lang.Short",
54             
55             "java.lang.ref.WeakReference",
56             
57             "java.util.ArrayList",
58             "java.util.HashSet",
59             "java.util.LinkedHashSet",
60             "java.util.LinkedList",
61             "java.util.Stack",
62             "java.util.TreeSet",
63             "java.util.Vector",
64             "java.util.Hashtable",
65             "java.util.Hashtable$Entry",
66             "java.util.HashMap",
67             "java.util.HashMap$Entry",
68             "java.util.IdentityHashMap",
69             "java.util.AbstractMap$SimpleEntry",
70             "java.util.TreeMap",
71             "java.util.TreeMap$Entry",
72             "java.util.WeakHashMap",
73             "java.util.LinkedHashMap",
74             "java.util.LinkedHashMap$Entry",
75             
76             "java.beans.PropertyChangeSupport"
77         };
78     }
79     
80     public String JavaDoc[] getSupportedAncestors () {
81         return new String JavaDoc[] {
82         };
83     }
84     
85     /**
86      * Returns filtered children for given parent on given indexes.
87      *
88      * @param original the original tree model
89      * @throws NoInformationException if the set of children can not be
90      * resolved
91      * @throws ComputingException if the children resolving process
92      * is time consuming, and will be performed off-line
93      * @throws UnknownTypeException if this TreeModelFilter implementation is not
94      * able to resolve dchildren for given node type
95      *
96      * @return children for given parent on given indexes
97      */

98     public Object JavaDoc[] getChildren (
99         TreeModel original,
100         Variable variable,
101         int from,
102         int to
103     ) throws UnknownTypeException {
104         
105         String JavaDoc type = variable.getType ();
106         
107         if (isToArrayType (type)) {
108             ObjectVariable ov = (ObjectVariable) variable;
109             try {
110                 ov = (ObjectVariable) ov.invokeMethod (
111                     "toArray",
112                     "()[Ljava/lang/Object;",
113                     new Variable [0]
114                 );
115                 return original.getChildren(ov, from, to);
116             } catch (NoSuchMethodException JavaDoc e) {
117                 Field elementData = ov.getField("elementData");
118                 if (elementData != null) {
119                     return original.getChildren(elementData, from, to);
120                 } else {
121                     ErrorManager.getDefault().notify(e);
122                 }
123             } catch (InvalidExpressionException e) {
124                 if ( (e.getTargetException () != null) &&
125                      (e.getTargetException () instanceof
126                        UnsupportedOperationException JavaDoc)
127                 ) {
128                     // PATCH for J2ME. see 45543
129
return original.getChildren (variable, from, to);
130                 }
131                 ErrorManager.getDefault().notify(e);
132             }
133         }
134         if (isMapMapType (type))
135             try {
136                 ObjectVariable ov = (ObjectVariable) variable;
137                 ov = (ObjectVariable) ov.invokeMethod (
138                     "entrySet",
139                     "()Ljava/util/Set;",
140                     new Variable [0]
141                 );
142                 ov = (ObjectVariable) ov.invokeMethod (
143                     "toArray",
144                     "()[Ljava/lang/Object;",
145                     new Variable [0]
146                 );
147                 return original.getChildren(ov, from, to);
148             } catch (InvalidExpressionException e) {
149                 if ( (e.getTargetException () != null) &&
150                      (e.getTargetException () instanceof
151                        UnsupportedOperationException JavaDoc)
152                 ) {
153                     // PATCH for J2ME. see 45543
154
return original.getChildren (variable, from, to);
155                 }
156                 ErrorManager.getDefault().notify(e);
157             } catch (NoSuchMethodException JavaDoc e) {
158                 ErrorManager.getDefault().notify(e);
159             }
160         if ( isMapEntryType (type)
161         ) {
162             ObjectVariable ov = (ObjectVariable) variable;
163             Field[] fs = new Field [2];
164             fs [0] = ov.getField ("key");
165             fs [1] = ov.getField ("value");
166             return fs;
167         }
168         if ( "java.beans.PropertyChangeSupport".equals (type)
169         )
170             try {
171                 ObjectVariable ov = (ObjectVariable) variable;
172                 return ((ObjectVariable) ov.invokeMethod (
173                     "getPropertyChangeListeners",
174                     "()[Ljava/beans/PropertyChangeListener;",
175                     new Variable [0]
176                 )).getFields (from, to);
177             } catch (InvalidExpressionException e) {
178                 if ( (e.getTargetException () != null) &&
179                      (e.getTargetException () instanceof
180                        UnsupportedOperationException JavaDoc)
181                 ) {
182                     // PATCH for J2ME. see 45543
183
return original.getChildren (variable, from, to);
184                 }
185                 ErrorManager.getDefault().notify(e);
186             } catch (NoSuchMethodException JavaDoc e) {
187                 ErrorManager.getDefault().notify(e);
188             }
189 // if ( type.equals ("java.lang.ref.WeakReference")
190
// )
191
// try {
192
// ObjectVariable ov = (ObjectVariable) variable;
193
// return new Object [] {ov.invokeMethod (
194
// "get",
195
// "()Ljava/lang/Object;",
196
// new Variable [0]
197
// )};
198
// } catch (NoSuchMethodException e) {
199
// e.printStackTrace ();
200
// }
201
if ( "java.lang.ref.WeakReference".equals (type)
202         ) {
203             ObjectVariable ov = (ObjectVariable) variable;
204             return new Object JavaDoc [] {ov.getField ("referent")};
205         }
206         return original.getChildren (variable, from, to);
207     }
208
209     /**
210      * Returns number of filtered children for given variable.
211      *
212      * @param original the original tree model
213      * @param variable a variable of returned fields
214      *
215      * @throws NoInformationException if the set of children can not be
216      * resolved
217      * @throws ComputingException if the children resolving process
218      * is time consuming, and will be performed off-line
219      * @throws UnknownTypeException if this TreeModelFilter implementation is not
220      * able to resolve dchildren for given node type
221      *
222      * @return number of filtered children for given variable
223      */

224     public int getChildrenCount (TreeModel original, Variable variable)
225     throws UnknownTypeException {
226
227         String JavaDoc type = variable.getType();
228
229         if (isToArrayType (type)) {
230             ObjectVariable ov = (ObjectVariable) variable;
231             try {
232                 ov = (ObjectVariable) ov.invokeMethod (
233                     "toArray",
234                     "()[Ljava/lang/Object;",
235                     new Variable [0]
236                 );
237                 return original.getChildrenCount(ov);
238             } catch (NoSuchMethodException JavaDoc e) {
239                 Field elementData = ov.getField("elementData");
240                 if (elementData != null) {
241                     return original.getChildrenCount(elementData);
242                 } else {
243                     ErrorManager.getDefault().notify(e);
244                 }
245             } catch (InvalidExpressionException e) {
246                 if ( (e.getTargetException () != null) &&
247                      (e.getTargetException () instanceof
248                        UnsupportedOperationException JavaDoc)
249                 ) {
250                     // PATCH for J2ME. see 45543
251
return original.getChildrenCount(variable);
252                 }
253                 ErrorManager.getDefault().notify(e);
254             }
255         } else if (isMapMapType (type)) {
256             try {
257                 ObjectVariable ov = (ObjectVariable) variable;
258                 ov = (ObjectVariable) ov.invokeMethod (
259                     "entrySet",
260                     "()Ljava/util/Set;",
261                     new Variable [0]
262                 );
263                 ov = (ObjectVariable) ov.invokeMethod (
264                     "toArray",
265                     "()[Ljava/lang/Object;",
266                     new Variable [0]
267                 );
268                 return original.getChildrenCount(ov);
269             } catch (InvalidExpressionException e) {
270                 if ( (e.getTargetException () != null) &&
271                      (e.getTargetException () instanceof
272                        UnsupportedOperationException JavaDoc)
273                 ) {
274                     // PATCH for J2ME. see 45543
275
return original.getChildrenCount(variable);
276                 }
277                 ErrorManager.getDefault().notify(e);
278             } catch (NoSuchMethodException JavaDoc e) {
279                 ErrorManager.getDefault().notify(e);
280             }
281         }
282         else if (isMapEntryType(type)) {
283             return 2;
284         }
285         else if ("java.beans.PropertyChangeSupport".equals(type)) {
286             return getChildren (original, variable, 0, 0).length;
287         }
288         else if ("java.lang.ref.WeakReference".equals(type)) {
289             return 1;
290         }
291         return original.getChildrenCount(variable);
292     }
293
294     /**
295      * Returns true if node is leaf.
296      *
297      * @param original the original tree model
298      * @throws UnknownTypeException if this TreeModel implementation is not
299      * able to resolve dchildren for given node type
300      * @return true if node is leaf
301      */

302     public boolean isLeaf (TreeModel original, Variable variable)
303     throws UnknownTypeException {
304         String JavaDoc type = variable.getType ();
305
306         // PATCH for J2ME
307
if ( isLeafType (type)
308         ) return true;
309         return original.isLeaf (variable);
310     }
311     
312     public Object JavaDoc getValueAt (
313         TableModel original,
314         Variable variable,
315         String JavaDoc columnID
316     ) throws UnknownTypeException {
317
318         String JavaDoc type = variable.getType ();
319         ObjectVariable ov = (ObjectVariable) variable;
320         if ( isMapEntryType (type) &&
321              ( columnID == Constants.LOCALS_VALUE_COLUMN_ID ||
322                columnID == Constants.WATCH_VALUE_COLUMN_ID)
323         ) {
324             return ov.getField ("key").getValue () + "=>" +
325                    ov.getField ("value").getValue ();
326         }
327         if ( isGetValueType (type) &&
328              ( columnID == Constants.LOCALS_VALUE_COLUMN_ID ||
329                columnID == Constants.WATCH_VALUE_COLUMN_ID)
330         ) {
331             return ov.getField ("value").getValue ();
332         }
333         if ( isToStringValueType (type) &&
334              ( columnID == Constants.LOCALS_VALUE_COLUMN_ID ||
335                columnID == Constants.WATCH_VALUE_COLUMN_ID)
336         ) {
337             try {
338                 return "\""+ov.getToStringValue ()+"\"";
339             } catch (InvalidExpressionException ex) {
340                 if ( (ex.getTargetException () != null) &&
341                      (ex.getTargetException () instanceof
342                        UnsupportedOperationException JavaDoc)
343                 ) {
344                     // PATCH for J2ME. see 45543
345
return original.getValueAt (variable, columnID);
346                 }
347                 return ex.getLocalizedMessage ();
348             }
349         }
350         return original.getValueAt (variable, columnID);
351     }
352
353     public void setValueAt(TableModel original, Variable variable,
354                            String JavaDoc columnID, Object JavaDoc value) throws UnknownTypeException {
355         String JavaDoc type = variable.getType();
356         if (isToStringValueType(type) &&
357             (columnID == Constants.LOCALS_VALUE_COLUMN_ID ||
358              columnID == Constants.WATCH_VALUE_COLUMN_ID)) {
359             String JavaDoc expression = (String JavaDoc) value;
360             if (expression.startsWith("\"") && expression.endsWith("\"") && expression.length() > 1) {
361                 // Create a new StringBuffer object with the desired content:
362
expression = "new " + type + "(\"" + convertToStringInitializer(expression.substring(1, expression.length() - 1)) + "\")";
363                 original.setValueAt(variable, columnID, expression);
364                 return ;
365             }
366         }
367         original.setValueAt(variable, columnID, value);
368     }
369     
370     private static String JavaDoc convertToStringInitializer (String JavaDoc s) {
371         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
372         int i, k = s.length ();
373         for (i = 0; i < k; i++)
374             switch (s.charAt (i)) {
375                 case '\b':
376                     sb.append ("\\b");
377                     break;
378                 case '\f':
379                     sb.append ("\\f");
380                     break;
381                 case '\\':
382                     sb.append ("\\\\");
383                     break;
384                 case '\t':
385                     sb.append ("\\t");
386                     break;
387                 case '\r':
388                     sb.append ("\\r");
389                     break;
390                 case '\n':
391                     sb.append ("\\n");
392                     break;
393                 case '\"':
394                     sb.append ("\\\"");
395                     break;
396                 default:
397                     sb.append (s.charAt (i));
398             }
399         return sb.toString();
400     }
401     
402     
403     // other methods ...........................................................
404

405     private static HashSet JavaDoc getValueType;
406     private static boolean isGetValueType (String JavaDoc type) {
407         if (getValueType == null) {
408             getValueType = new HashSet JavaDoc ();
409             getValueType.add ("java.lang.Character");
410             getValueType.add ("java.lang.Integer");
411             getValueType.add ("java.lang.Float");
412             getValueType.add ("java.lang.Byte");
413             getValueType.add ("java.lang.Boolean");
414             getValueType.add ("java.lang.Double");
415             getValueType.add ("java.lang.Long");
416             getValueType.add ("java.lang.Short");
417         }
418         return getValueType.contains (type);
419     }
420     
421     private static HashSet JavaDoc leafType;
422     private static boolean isLeafType (String JavaDoc type) {
423         if (leafType == null) {
424             leafType = new HashSet JavaDoc ();
425             leafType.add ("java.lang.String");
426             leafType.add ("java.lang.Character");
427             leafType.add ("java.lang.Integer");
428             leafType.add ("java.lang.Float");
429             leafType.add ("java.lang.Byte");
430             leafType.add ("java.lang.Boolean");
431             leafType.add ("java.lang.Double");
432             leafType.add ("java.lang.Long");
433             leafType.add ("java.lang.Short");
434         }
435         return leafType.contains (type);
436     }
437     
438     private static HashSet JavaDoc toStringValueType;
439     private static boolean isToStringValueType (String JavaDoc type) {
440         if (toStringValueType == null) {
441             toStringValueType = new HashSet JavaDoc ();
442             toStringValueType.add ("java.lang.StringBuffer");
443         }
444         return toStringValueType.contains (type);
445     }
446     
447     private static HashSet JavaDoc mapEntryType;
448     private static boolean isMapEntryType (String JavaDoc type) {
449         if (mapEntryType == null) {
450             mapEntryType = new HashSet JavaDoc ();
451             mapEntryType.add ("java.util.HashMap$Entry");
452             mapEntryType.add ("java.util.Hashtable$Entry");
453             mapEntryType.add ("java.util.AbstractMap$SimpleEntry");
454             mapEntryType.add ("java.util.LinkedHashMap$Entry");
455             mapEntryType.add ("java.util.TreeMap$Entry");
456         }
457         return mapEntryType.contains (type);
458     }
459     
460     private static HashSet JavaDoc mapMapType;
461     private static boolean isMapMapType (String JavaDoc type) {
462         if (mapMapType == null) {
463             mapMapType = new HashSet JavaDoc ();
464             mapMapType.add ("java.util.HashMap");
465             mapMapType.add ("java.util.IdentityHashMap");
466             mapMapType.add ("java.util.Hashtable");
467             mapMapType.add ("java.util.TreeMap");
468             mapMapType.add ("java.util.WeakHashMap");
469             mapMapType.add ("java.util.LinkedHashMap");
470             mapMapType.add ("java.util.concurrent.ConcurrentHashMap");
471             mapMapType.add ("java.util.EnumMap");
472         }
473         return mapMapType.contains (type);
474     }
475     
476     private static HashSet JavaDoc toArrayType;
477     private static boolean isToArrayType (String JavaDoc type) {
478         if (toArrayType == null) {
479             toArrayType = new HashSet JavaDoc ();
480             toArrayType.add ("java.util.ArrayList");
481             toArrayType.add ("java.util.HashSet");
482             toArrayType.add ("java.util.LinkedHashSet");
483             toArrayType.add ("java.util.LinkedList");
484             toArrayType.add ("java.util.Stack");
485             toArrayType.add ("java.util.TreeSet");
486             toArrayType.add ("java.util.Vector");
487             toArrayType.add ("java.util.concurrent.CopyOnWriteArraySet");
488             toArrayType.add ("java.util.EnumSet");
489         }
490         return toArrayType.contains (type);
491     }
492 }
493
Popular Tags