blob: 56667100dd13c19051219c20ef11e848311d6049 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.tools.example.debug.gui;
27
28import java.io.*;
29import java.util.*;
30
31import com.sun.jdi.*;
32
33import com.sun.tools.example.debug.event.*;
34import com.sun.tools.example.debug.bdi.*;
35
36/**
37 * Manage the list of source files.
38 * Origin of SourceListener events.
39 */
40public class SourceManager {
41
42 //### TODO: The source cache should be aged, and some cap
43 //### put on memory consumption by source files loaded into core.
44
45 private List<SourceModel> sourceList;
46 private SearchPath sourcePath;
47
48 private Vector<SourceListener> sourceListeners = new Vector<SourceListener>();
49
50 private Map<ReferenceType, SourceModel> classToSource = new HashMap<ReferenceType, SourceModel>();
51
52 private Environment env;
53
54 /**
55 * Hold on to it so it can be removed.
56 */
57 private SMClassListener classListener = new SMClassListener();
58
59 public SourceManager(Environment env) {
60 this(env, new SearchPath(""));
61 }
62
63 public SourceManager(Environment env, SearchPath sourcePath) {
64 this.env = env;
65 this.sourceList = new LinkedList<SourceModel>();
66 this.sourcePath = sourcePath;
67 env.getExecutionManager().addJDIListener(classListener);
68 }
69
70 /**
71 * Set path for access to source code.
72 */
73 public void setSourcePath(SearchPath sp) {
74 sourcePath = sp;
75 // Old cached sources are now invalid.
76 sourceList = new LinkedList<SourceModel>();
77 notifySourcepathChanged();
78 classToSource = new HashMap<ReferenceType, SourceModel>();
79 }
80
81 public void addSourceListener(SourceListener l) {
82 sourceListeners.addElement(l);
83 }
84
85 public void removeSourceListener(SourceListener l) {
86 sourceListeners.removeElement(l);
87 }
88
89 private void notifySourcepathChanged() {
90 Vector l = (Vector)sourceListeners.clone();
91 SourcepathChangedEvent evt = new SourcepathChangedEvent(this);
92 for (int i = 0; i < l.size(); i++) {
93 ((SourceListener)l.elementAt(i)).sourcepathChanged(evt);
94 }
95 }
96
97 /**
98 * Get path for access to source code.
99 */
100 public SearchPath getSourcePath() {
101 return sourcePath;
102 }
103
104 /**
105 * Get source object associated with a Location.
106 */
107 public SourceModel sourceForLocation(Location loc) {
108 return sourceForClass(loc.declaringType());
109 }
110
111 /**
112 * Get source object associated with a class or interface.
113 * Returns null if not available.
114 */
115 public SourceModel sourceForClass(ReferenceType refType) {
116 SourceModel sm = (SourceModel)classToSource.get(refType);
117 if (sm != null) {
118 return sm;
119 }
120 try {
121 String filename = refType.sourceName();
122 String refName = refType.name();
123 int iDot = refName.lastIndexOf('.');
124 String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
125 String full = pkgName.replace('.', File.separatorChar) + filename;
126 File path = sourcePath.resolve(full);
127 if (path != null) {
128 sm = sourceForFile(path);
129 classToSource.put(refType, sm);
130 return sm;
131 }
132 return null;
133 } catch (AbsentInformationException e) {
134 return null;
135 }
136 }
137
138 /**
139 * Get source object associated with an absolute file path.
140 */
141 //### Use hash table for this?
142 public SourceModel sourceForFile(File path) {
143 Iterator iter = sourceList.iterator();
144 SourceModel sm = null;
145 while (iter.hasNext()) {
146 SourceModel candidate = (SourceModel)iter.next();
147 if (candidate.fileName().equals(path)) {
148 sm = candidate;
149 iter.remove(); // Will move to start of list.
150 break;
151 }
152 }
153 if (sm == null && path.exists()) {
154 sm = new SourceModel(env, path);
155 }
156 if (sm != null) {
157 // At start of list for faster access
158 sourceList.add(0, sm);
159 }
160 return sm;
161 }
162
163 private class SMClassListener extends JDIAdapter
164 implements JDIListener {
165
166 public void classPrepare(ClassPrepareEventSet e) {
167 ReferenceType refType = e.getReferenceType();
168 SourceModel sm = sourceForClass(refType);
169 if (sm != null) {
170 sm.addClass(refType);
171 }
172 }
173
174 public void classUnload(ClassUnloadEventSet e) {
175 //### iterate through looking for (e.getTypeName()).
176 //### then remove it.
177 }
178 }
179}