001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.debugger.jpda.models;
043:
044: import com.sun.jdi.LocalVariable;
045: import com.sun.jdi.Mirror;
046: import com.sun.jdi.ObjectReference;
047: import com.sun.jdi.PrimitiveValue;
048: import com.sun.jdi.ReferenceType;
049: import com.sun.jdi.StackFrame;
050: import com.sun.jdi.ThreadGroupReference;
051: import com.sun.jdi.ThreadReference;
052: import com.sun.jdi.Value;
053:
054: import java.lang.ref.WeakReference;
055: import java.util.Collection;
056: import java.util.HashSet;
057: import java.util.Iterator;
058: import java.util.WeakHashMap;
059:
060: import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
061:
062: /**
063: * Helps to translate one tree to another.
064: *
065: * Used just for ThreadsTreeModel
066: *
067: * @author Jan Jancura
068: */
069: public final class ObjectTranslation {
070:
071: private static final int THREAD_ID = 0;
072: private static final int LOCALS_ID = 2;
073:
074: private JPDADebuggerImpl debugger;
075: private int translationID;
076:
077: /* original Object to a new one.*/
078: private WeakHashMap<Mirror, WeakReference<Object>> cache = new WeakHashMap<Mirror, WeakReference<Object>>();
079:
080: /**
081: * Creates a new instance of translating tree model for given
082: * tree model.
083: *
084: * @param model a tree model to be translated
085: */
086: private ObjectTranslation(JPDADebuggerImpl debugger,
087: int translationID) {
088: this .debugger = debugger;
089: this .translationID = translationID;
090: }
091:
092: /**
093: * Creates a new translated node for given original one.
094: *
095: * @param o a node to be translated
096: * @return a new translated node
097: */
098: private Object createTranslation(Object o) {
099: switch (translationID) {
100: case THREAD_ID:
101: if (o instanceof ThreadReference) {
102: return new JPDAThreadImpl((ThreadReference) o, debugger);
103: } else if (o instanceof ThreadGroupReference) {
104: return new JPDAThreadGroupImpl(
105: (ThreadGroupReference) o, debugger);
106: } else {
107: return null;
108: }
109: case LOCALS_ID:
110: if (o instanceof ReferenceType) {
111: return new JPDAClassTypeImpl(debugger,
112: (ReferenceType) o);
113: }
114: default:
115: throw new IllegalStateException("" + o);
116: }
117: }
118:
119: private Object createTranslation(Object o, Object v) {
120: switch (translationID) {
121: case LOCALS_ID:
122: if (o instanceof LocalVariable
123: && (v == null || v instanceof Value)) {
124: LocalVariable lv = (LocalVariable) o;
125: org.netbeans.api.debugger.jpda.LocalVariable local;
126: if (v instanceof ObjectReference) {
127: local = new ObjectLocalVariable(debugger,
128: (ObjectReference) v, null, lv,
129: JPDADebuggerImpl.getGenericSignature(lv),
130: null);
131: } else {
132: local = new Local(debugger, (PrimitiveValue) v,
133: null, lv, null);
134: }
135: return local;
136: }
137: default:
138: throw new IllegalStateException("" + o);
139: }
140: }
141:
142: /**
143: * Translates a debuggee Mirror to a wrapper object.
144: *
145: * @param o the Mirror object in the debuggee
146: * @return translated object or <code>null</code> when the argument
147: * is not possible to translate.
148: */
149: public Object translate(Mirror o) {
150: Object r = null;
151: synchronized (cache) {
152: WeakReference wr = cache.get(o);
153: if (wr != null)
154: r = wr.get();
155: if (r == null) {
156: r = createTranslation(o);
157: cache.put(o, new WeakReference<Object>(r));
158: }
159: }
160: return r;
161: }
162:
163: /**
164: * Gen an existing wrapper object translation of a debuggee Mirror.
165: *
166: * @param o the Mirror object in the debuggee
167: * @return translated object or <code>null</code> when there is no existing
168: * translation.
169: */
170: public Object translateExisting(Mirror o) {
171: Object r = null;
172: synchronized (cache) {
173: WeakReference wr = cache.get(o);
174: if (wr != null)
175: r = wr.get();
176: }
177: return r;
178: }
179:
180: /**
181: * Get all live objects that were translated.
182: */
183: public Collection getTranslated() {
184: Collection translated = new HashSet();
185: synchronized (cache) {
186: Collection references = cache.values();
187: for (Iterator it = references.iterator(); it.hasNext();) {
188: WeakReference wr = (WeakReference) it.next();
189: Object r = wr.get();
190: if (r != null) {
191: translated.add(r);
192: }
193: }
194: }
195: return translated;
196: }
197:
198: /**
199: * Translates a debuggee Mirror to a wrapper object.
200: *
201: * @param o the Mirror object in the debuggee
202: * @param v an additional argument used for the translation
203: * @return translated object or <code>null</code> when the argument
204: * is not possible to translate.
205: */
206: public Object translate(Mirror o, Object v) {
207: Object r = null;
208: synchronized (cache) {
209: WeakReference wr = cache.get(o);
210: if (wr != null)
211: r = wr.get();
212: if (r == null) {
213: r = createTranslation(o, v);
214: cache.put(o, new WeakReference<Object>(r));
215: }
216: }
217: return r;
218: }
219:
220: /**
221: * Explicitly remove the translation of the mirror object.
222: */
223: public void remove(Mirror o) {
224: synchronized (cache) {
225: cache.remove(o);
226: }
227: }
228:
229: public static ObjectTranslation createThreadTranslation(
230: JPDADebuggerImpl debugger) {
231: return new ObjectTranslation(debugger, THREAD_ID);
232: }
233:
234: public static ObjectTranslation createLocalsTranslation(
235: JPDADebuggerImpl debugger) {
236: return new ObjectTranslation(debugger, LOCALS_ID);
237: }
238:
239: }
|