1 7 34 35 package com.sun.tools.example.debug.gui; 36 37 import java.io.*; 38 import java.util.*; 39 40 import com.sun.jdi.*; 41 42 import com.sun.tools.example.debug.event.*; 43 import com.sun.tools.example.debug.bdi.*; 44 45 49 public class SourceManager { 50 51 54 private List sourceList; 55 private SearchPath sourcePath; 56 57 private Vector sourceListeners = new Vector(); 58 59 private Map classToSource = new HashMap(); 60 61 private Environment env; 62 63 66 private SMClassListener classListener = new SMClassListener(); 67 68 public SourceManager(Environment env) { 69 this(env, new SearchPath("")); 70 } 71 72 public SourceManager(Environment env, SearchPath sourcePath) { 73 this.env = env; 74 this.sourceList = new LinkedList(); 75 this.sourcePath = sourcePath; 76 env.getExecutionManager().addJDIListener(classListener); 77 } 78 79 82 public void setSourcePath(SearchPath sp) { 83 sourcePath = sp; 84 sourceList = new LinkedList(); 86 notifySourcepathChanged(); 87 classToSource = new HashMap(); 88 } 89 90 public void addSourceListener(SourceListener l) { 91 sourceListeners.addElement(l); 92 } 93 94 public void removeSourceListener(SourceListener l) { 95 sourceListeners.removeElement(l); 96 } 97 98 private void notifySourcepathChanged() { 99 Vector l = (Vector)sourceListeners.clone(); 100 SourcepathChangedEvent evt = new SourcepathChangedEvent(this); 101 for (int i = 0; i < l.size(); i++) { 102 ((SourceListener)l.elementAt(i)).sourcepathChanged(evt); 103 } 104 } 105 106 109 public SearchPath getSourcePath() { 110 return sourcePath; 111 } 112 113 116 public SourceModel sourceForLocation(Location loc) { 117 return sourceForClass(loc.declaringType()); 118 } 119 120 124 public SourceModel sourceForClass(ReferenceType refType) { 125 SourceModel sm = (SourceModel)classToSource.get(refType); 126 if (sm != null) { 127 return sm; 128 } 129 try { 130 String filename = refType.sourceName(); 131 String refName = refType.name(); 132 int iDot = refName.lastIndexOf('.'); 133 String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : ""; 134 String full = pkgName.replace('.', File.separatorChar) + filename; 135 File path = sourcePath.resolve(full); 136 if (path != null) { 137 sm = sourceForFile(path); 138 classToSource.put(refType, sm); 139 return sm; 140 } 141 return null; 142 } catch (AbsentInformationException e) { 143 return null; 144 } 145 } 146 147 150 public SourceModel sourceForFile(File path) { 152 Iterator iter = sourceList.iterator(); 153 SourceModel sm = null; 154 while (iter.hasNext()) { 155 SourceModel candidate = (SourceModel)iter.next(); 156 if (candidate.fileName().equals(path)) { 157 sm = candidate; 158 iter.remove(); break; 160 } 161 } 162 if (sm == null && path.exists()) { 163 sm = new SourceModel(env, path); 164 } 165 if (sm != null) { 166 sourceList.add(0, sm); 168 } 169 return sm; 170 } 171 172 private class SMClassListener extends JDIAdapter 173 implements JDIListener { 174 175 public void classPrepare(ClassPrepareEventSet e) { 176 ReferenceType refType = e.getReferenceType(); 177 SourceModel sm = sourceForClass(refType); 178 if (sm != null) { 179 sm.addClass(refType); 180 } 181 } 182 183 public void classUnload(ClassUnloadEventSet e) { 184 } 187 } 188 } 189 | Popular Tags |