2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.gui;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.util.*;
|
|
30 |
|
|
31 |
import com.sun.jdi.*;
|
|
32 |
import com.sun.jdi.request.*;
|
|
33 |
|
|
34 |
import com.sun.tools.example.debug.bdi.*;
|
|
35 |
|
|
36 |
import javax.swing.*;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Represents and manages one source file.
|
|
40 |
* Caches source lines. Holds other source file info.
|
|
41 |
*/
|
|
42 |
public class SourceModel extends AbstractListModel {
|
|
43 |
|
|
44 |
private File path;
|
|
45 |
|
|
46 |
boolean isActuallySource = true;
|
|
47 |
|
|
48 |
private List<ReferenceType> classes = new ArrayList<ReferenceType>();
|
|
49 |
|
|
50 |
private Environment env;
|
|
51 |
|
|
52 |
// Cached line-by-line access.
|
|
53 |
|
|
54 |
//### Unify this with source model used in source view?
|
|
55 |
//### What is our cache-management policy for these?
|
|
56 |
//### Even with weak refs, we won't discard any part of the
|
|
57 |
//### source if the SourceModel object is reachable.
|
|
58 |
/**
|
|
59 |
* List of Line.
|
|
60 |
*/
|
|
61 |
private List<Line> sourceLines = null;
|
|
62 |
|
|
63 |
public static class Line {
|
|
64 |
public String text;
|
|
65 |
public boolean hasBreakpoint = false;
|
|
66 |
public ReferenceType refType = null;
|
|
67 |
Line(String text) {
|
|
68 |
this.text = text;
|
|
69 |
}
|
|
70 |
public boolean isExecutable() {
|
|
71 |
return refType != null;
|
|
72 |
}
|
|
73 |
public boolean hasBreakpoint() {
|
|
74 |
return hasBreakpoint;
|
|
75 |
}
|
|
76 |
};
|
|
77 |
|
|
78 |
// 132 characters long, all printable characters.
|
|
79 |
public static final Line prototypeCellValue = new Line(
|
|
80 |
"abcdefghijklmnopqrstuvwxyz" +
|
|
81 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
82 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
83 |
"1234567890~!@#$%^&*()_+{}|" +
|
|
84 |
":<>?`-=[];',.XXXXXXXXXXXX/\\\"");
|
|
85 |
|
|
86 |
SourceModel(Environment env, File path) {
|
|
87 |
this.env = env;
|
|
88 |
this.path = path;
|
|
89 |
}
|
|
90 |
|
|
91 |
public SourceModel(String message) {
|
|
92 |
this.path = null;
|
|
93 |
setMessage(message);
|
|
94 |
}
|
|
95 |
|
|
96 |
private void setMessage(String message) {
|
|
97 |
isActuallySource = false;
|
|
98 |
sourceLines = new ArrayList<Line>();
|
|
99 |
sourceLines.add(new Line(message));
|
|
100 |
}
|
|
101 |
|
|
102 |
// **** Implement ListModel *****
|
|
103 |
|
|
104 |
public Object getElementAt(int index) {
|
|
105 |
if (sourceLines == null) {
|
|
106 |
initialize();
|
|
107 |
}
|
|
108 |
return sourceLines.get(index);
|
|
109 |
}
|
|
110 |
|
|
111 |
public int getSize() {
|
|
112 |
if (sourceLines == null) {
|
|
113 |
initialize();
|
|
114 |
}
|
|
115 |
return sourceLines.size();
|
|
116 |
}
|
|
117 |
|
|
118 |
// ***** Other functionality *****
|
|
119 |
|
|
120 |
public File fileName() {
|
|
121 |
return path;
|
|
122 |
}
|
|
123 |
|
|
124 |
public BufferedReader sourceReader() throws IOException {
|
|
125 |
return new BufferedReader(new FileReader(path));
|
|
126 |
}
|
|
127 |
|
|
128 |
public Line line(int lineNo) {
|
|
129 |
if (sourceLines == null) {
|
|
130 |
initialize();
|
|
131 |
}
|
|
132 |
int index = lineNo - 1; // list is 0-indexed
|
|
133 |
if (index >= sourceLines.size() || index < 0) {
|
|
134 |
return null;
|
|
135 |
} else {
|
|
136 |
return sourceLines.get(index);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
public String sourceLine(int lineNo) {
|
|
141 |
Line line = line(lineNo);
|
|
142 |
if (line == null) {
|
|
143 |
return null;
|
|
144 |
} else {
|
|
145 |
return line.text;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
void addClass(ReferenceType refType) {
|
|
150 |
// Logically is Set
|
|
151 |
if (classes.indexOf(refType) == -1) {
|
|
152 |
classes.add(refType);
|
|
153 |
if (sourceLines != null) {
|
|
154 |
markClassLines(refType);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* @return List of currently known {@link com.sun.jdi.ReferenceType}
|
|
161 |
* in this source file.
|
|
162 |
*/
|
|
163 |
public List<ReferenceType> referenceTypes() {
|
|
164 |
return Collections.unmodifiableList(classes);
|
|
165 |
}
|
|
166 |
|
|
167 |
private void initialize() {
|
|
168 |
try {
|
|
169 |
rawInit();
|
|
170 |
} catch (IOException exc) {
|
|
171 |
setMessage("[Error reading source code]");
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
public void showBreakpoint(int ln, boolean hasBreakpoint) {
|
|
176 |
line(ln).hasBreakpoint = hasBreakpoint;
|
|
177 |
fireContentsChanged(this, ln, ln);
|
|
178 |
}
|
|
179 |
|
|
180 |
public void showExecutable(int ln, ReferenceType refType) {
|
|
181 |
line(ln).refType = refType;
|
|
182 |
fireContentsChanged(this, ln, ln);
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Mark executable lines and breakpoints, but only
|
|
187 |
* when sourceLines is set.
|
|
188 |
*/
|
|
189 |
private void markClassLines(ReferenceType refType) {
|
51
|
190 |
for (Method meth : refType.methods()) {
|
2
|
191 |
try {
|
51
|
192 |
for (Location loc : meth.allLineLocations()) {
|
2
|
193 |
showExecutable(loc.lineNumber(), refType);
|
|
194 |
}
|
|
195 |
} catch (AbsentInformationException exc) {
|
|
196 |
// do nothing
|
|
197 |
}
|
|
198 |
}
|
51
|
199 |
for (BreakpointRequest bp :
|
|
200 |
env.getExecutionManager().eventRequestManager().breakpointRequests()) {
|
2
|
201 |
if (bp.location() != null) {
|
|
202 |
Location loc = bp.location();
|
|
203 |
if (loc.declaringType().equals(refType)) {
|
|
204 |
showBreakpoint(loc.lineNumber(),true);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
private void rawInit() throws IOException {
|
|
211 |
sourceLines = new ArrayList<Line>();
|
|
212 |
BufferedReader reader = sourceReader();
|
|
213 |
try {
|
|
214 |
String line = reader.readLine();
|
|
215 |
while (line != null) {
|
|
216 |
sourceLines.add(new Line(expandTabs(line)));
|
|
217 |
line = reader.readLine();
|
|
218 |
}
|
|
219 |
} finally {
|
|
220 |
reader.close();
|
|
221 |
}
|
51
|
222 |
for (ReferenceType refType : classes) {
|
|
223 |
markClassLines(refType);
|
2
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
private String expandTabs(String s) {
|
|
228 |
int col = 0;
|
|
229 |
int len = s.length();
|
|
230 |
StringBuffer sb = new StringBuffer(132);
|
|
231 |
for (int i = 0; i < len; i++) {
|
|
232 |
char c = s.charAt(i);
|
|
233 |
sb.append(c);
|
|
234 |
if (c == '\t') {
|
|
235 |
int pad = (8 - (col % 8));
|
|
236 |
for (int j = 0; j < pad; j++) {
|
|
237 |
sb.append(' ');
|
|
238 |
}
|
|
239 |
col += pad;
|
|
240 |
} else {
|
|
241 |
col++;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
return sb.toString();
|
|
245 |
}
|
|
246 |
|
|
247 |
}
|