KickJava   Java API By Example, From Geeks To Geeks.

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


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.LocalVariable;
23 import com.sun.jdi.Mirror;
24 import com.sun.jdi.ObjectReference;
25 import com.sun.jdi.ReferenceType;
26 import com.sun.jdi.StackFrame;
27 import com.sun.jdi.ThreadGroupReference;
28 import com.sun.jdi.ThreadReference;
29 import com.sun.jdi.Value;
30
31 import java.lang.ref.WeakReference JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36
37 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
38
39 /**
40  * Helps to translate one tree to another.
41  *
42  * Used just for ThreadsTreeModel
43  *
44  * @author Jan Jancura
45  */

46 public final class ObjectTranslation {
47     
48     private static final int THREAD_ID = 0;
49     private static final int LOCALS_ID = 2;
50     
51     private JPDADebuggerImpl debugger;
52     private int translationID;
53     
54     /* original Object to a new one.*/
55     private WeakHashMap JavaDoc<Mirror, WeakReference JavaDoc<Object JavaDoc>> cache = new WeakHashMap JavaDoc<Mirror, WeakReference JavaDoc<Object JavaDoc>>();
56     
57     
58     /**
59      * Creates a new instance of translating tree model for given
60      * tree model.
61      *
62      * @param model a tree model to be translated
63      */

64     private ObjectTranslation (JPDADebuggerImpl debugger, int translationID) {
65         this.debugger = debugger;
66         this.translationID = translationID;
67     }
68     
69     /**
70      * Creates a new translated node for given original one.
71      *
72      * @param o a node to be translated
73      * @return a new translated node
74      */

75     private Object JavaDoc createTranslation (Object JavaDoc o) {
76         switch (translationID) {
77             case THREAD_ID:
78                 if (o instanceof ThreadReference) {
79                     return new JPDAThreadImpl ((ThreadReference) o, debugger);
80                 } else if (o instanceof ThreadGroupReference) {
81                     return new JPDAThreadGroupImpl ((ThreadGroupReference) o, debugger);
82                 } else {
83                     return null;
84                 }
85             case LOCALS_ID:
86                 if (o instanceof ReferenceType) {
87                     return new JPDAClassTypeImpl(debugger, (ReferenceType) o);
88                 }
89             default:
90                 throw new IllegalStateException JavaDoc(""+o);
91         }
92     }
93     
94     private Object JavaDoc createTranslation (Object JavaDoc o, Object JavaDoc v) {
95         switch (translationID) {
96             case LOCALS_ID:
97                 if (o instanceof LocalVariable && (v == null || v instanceof Value)) {
98                     LocalVariable lv = (LocalVariable) o;
99                     Local local;
100                     if (v instanceof ObjectReference) {
101                         local = new ObjectLocalVariable (
102                             debugger,
103                             (Value) v,
104                             null,
105                             lv,
106                             JPDADebuggerImpl.getGenericSignature (lv),
107                             null
108                         );
109                     } else {
110                         local = new Local (debugger, (Value) v, null, lv, null);
111                     }
112                     return local;
113                 }
114             default:
115                 throw new IllegalStateException JavaDoc(""+o);
116         }
117     }
118     
119     /**
120      * Translates a debuggee Mirror to a wrapper object.
121      *
122      * @param o the Mirror object in the debuggee
123      * @return translated object or <code>null</code> when the argument
124      * is not possible to translate.
125      */

126     public Object JavaDoc translate (Mirror o) {
127         Object JavaDoc r = null;
128         synchronized (cache) {
129             WeakReference JavaDoc wr = cache.get (o);
130             if (wr != null)
131                 r = wr.get ();
132             if (r == null) {
133                 r = createTranslation (o);
134                 cache.put (o, new WeakReference JavaDoc<Object JavaDoc>(r));
135             }
136         }
137         return r;
138     }
139     
140     /**
141      * Get all live objects that were translated.
142      */

143     public Collection JavaDoc getTranslated() {
144         Collection JavaDoc translated = new HashSet JavaDoc();
145         synchronized (cache) {
146             Collection JavaDoc references = cache.values();
147             for (Iterator JavaDoc it = references.iterator(); it.hasNext(); ) {
148                 WeakReference JavaDoc wr = (WeakReference JavaDoc) it.next();
149                 Object JavaDoc r = wr.get();
150                 if (r != null) {
151                     translated.add(r);
152                 }
153             }
154         }
155         return translated;
156     }
157     
158     /**
159      * Translates a debuggee Mirror to a wrapper object.
160      *
161      * @param o the Mirror object in the debuggee
162      * @param v an additional argument used for the translation
163      * @return translated object or <code>null</code> when the argument
164      * is not possible to translate.
165      */

166     public Object JavaDoc translate (Mirror o, Object JavaDoc v) {
167         Object JavaDoc r = null;
168         WeakReference JavaDoc wr = cache.get (o);
169         if (wr != null)
170             r = wr.get ();
171         if (r == null) {
172             r = createTranslation (o, v);
173             cache.put (o, new WeakReference JavaDoc<Object JavaDoc>(r));
174         }
175         return r;
176     }
177     
178     public static ObjectTranslation createThreadTranslation(JPDADebuggerImpl debugger) {
179         return new ObjectTranslation(debugger, THREAD_ID);
180     }
181     
182     public static ObjectTranslation createLocalsTranslation(JPDADebuggerImpl debugger) {
183         return new ObjectTranslation(debugger, LOCALS_ID);
184     }
185     
186 }
187
Popular Tags