KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > SourceManager


1 /*
2  * @(#)SourceManager.java 1.12 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

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 /**
46  * Manage the list of source files.
47  * Origin of SourceListener events.
48  */

49 public class SourceManager {
50
51     //### TODO: The source cache should be aged, and some cap
52
//### put on memory consumption by source files loaded into core.
53

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     /**
64      * Hold on to it so it can be removed.
65      */

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     /**
80      * Set path for access to source code.
81      */

82     public void setSourcePath(SearchPath sp) {
83     sourcePath = sp;
84     // Old cached sources are now invalid.
85
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     /**
107      * Get path for access to source code.
108      */

109     public SearchPath getSourcePath() {
110     return sourcePath;
111     }
112     
113     /**
114      * Get source object associated with a Location.
115      */

116     public SourceModel sourceForLocation(Location loc) {
117     return sourceForClass(loc.declaringType());
118     }
119
120     /**
121      * Get source object associated with a class or interface.
122      * Returns null if not available.
123      */

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 JavaDoc filename = refType.sourceName();
131             String JavaDoc refName = refType.name();
132             int iDot = refName.lastIndexOf('.');
133             String JavaDoc pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
134             String JavaDoc 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     /**
148      * Get source object associated with an absolute file path.
149      */

150     //### Use hash table for this?
151
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(); // Will move to start of list.
159
break;
160             }
161         }
162         if (sm == null && path.exists()) {
163         sm = new SourceModel(env, path);
164         }
165         if (sm != null) {
166             // At start of list for faster access
167
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             //### iterate through looking for (e.getTypeName()).
185
//### then remove it.
186
}
187     }
188 }
189
Popular Tags