1 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 ; 32 import java.util.Collection ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.WeakHashMap ; 36 37 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl; 38 39 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 55 private WeakHashMap <Mirror, WeakReference <Object >> cache = new WeakHashMap <Mirror, WeakReference <Object >>(); 56 57 58 64 private ObjectTranslation (JPDADebuggerImpl debugger, int translationID) { 65 this.debugger = debugger; 66 this.translationID = translationID; 67 } 68 69 75 private Object createTranslation (Object 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 (""+o); 91 } 92 } 93 94 private Object createTranslation (Object o, Object 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 (""+o); 116 } 117 } 118 119 126 public Object translate (Mirror o) { 127 Object r = null; 128 synchronized (cache) { 129 WeakReference 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 <Object >(r)); 135 } 136 } 137 return r; 138 } 139 140 143 public Collection getTranslated() { 144 Collection translated = new HashSet (); 145 synchronized (cache) { 146 Collection references = cache.values(); 147 for (Iterator it = references.iterator(); it.hasNext(); ) { 148 WeakReference wr = (WeakReference ) it.next(); 149 Object r = wr.get(); 150 if (r != null) { 151 translated.add(r); 152 } 153 } 154 } 155 return translated; 156 } 157 158 166 public Object translate (Mirror o, Object v) { 167 Object r = null; 168 WeakReference 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 <Object >(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 |