2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. 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
|
|
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 |
|
|
26 |
package com.sun.tools.example.debug.tty;
|
|
27 |
|
|
28 |
import com.sun.jdi.Location;
|
|
29 |
import com.sun.jdi.AbsentInformationException;
|
|
30 |
import java.util.Map;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.ArrayList;
|
|
34 |
import java.util.Iterator;
|
|
35 |
import java.util.StringTokenizer;
|
|
36 |
import java.io.*;
|
|
37 |
|
|
38 |
class SourceMapper {
|
|
39 |
|
|
40 |
private final String[] dirs;
|
|
41 |
|
51
|
42 |
SourceMapper(List<String> sourcepath) {
|
2
|
43 |
/*
|
|
44 |
* sourcepath can arrive from the debugee as a List.
|
|
45 |
* (via PathSearchingVirtualMachine.classPath())
|
|
46 |
*/
|
|
47 |
List<String> dirList = new ArrayList<String>();
|
51
|
48 |
for (String element : sourcepath) {
|
2
|
49 |
//XXX remove .jar and .zip files; we want only directories on
|
|
50 |
//the source path. (Bug ID 4186582)
|
|
51 |
if ( ! (element.endsWith(".jar") ||
|
|
52 |
element.endsWith(".zip"))) {
|
|
53 |
dirList.add(element);
|
|
54 |
}
|
|
55 |
}
|
51
|
56 |
dirs = dirList.toArray(new String[0]);
|
2
|
57 |
}
|
|
58 |
|
|
59 |
SourceMapper(String sourcepath) {
|
|
60 |
/*
|
|
61 |
* sourcepath can also arrive from the command line
|
|
62 |
* as a String. (via "-sourcepath")
|
|
63 |
*
|
|
64 |
* Using File.pathSeparator as delimiter below is OK
|
|
65 |
* because we are on the same machine as the command
|
|
66 |
* line originiated.
|
|
67 |
*/
|
|
68 |
StringTokenizer st = new StringTokenizer(sourcepath,
|
|
69 |
File.pathSeparator);
|
|
70 |
List<String> dirList = new ArrayList<String>();
|
|
71 |
while (st.hasMoreTokens()) {
|
|
72 |
String s = st.nextToken();
|
|
73 |
//XXX remove .jar and .zip files; we want only directories on
|
|
74 |
//the source path. (Bug ID 4186582)
|
|
75 |
if ( ! (s.endsWith(".jar") ||
|
|
76 |
s.endsWith(".zip"))) {
|
|
77 |
dirList.add(s);
|
|
78 |
}
|
|
79 |
}
|
51
|
80 |
dirs = dirList.toArray(new String[0]);
|
2
|
81 |
}
|
|
82 |
|
|
83 |
/*
|
|
84 |
* Return the current sourcePath as a String.
|
|
85 |
*/
|
|
86 |
String getSourcePath() {
|
|
87 |
int i = 0;
|
|
88 |
StringBuffer sp;
|
|
89 |
if (dirs.length < 1) {
|
|
90 |
return ""; //The source path is empty.
|
|
91 |
} else {
|
|
92 |
sp = new StringBuffer(dirs[i++]);
|
|
93 |
}
|
|
94 |
for (; i < dirs.length; i++) {
|
|
95 |
sp.append(File.pathSeparator);
|
|
96 |
sp.append(dirs[i]);
|
|
97 |
}
|
|
98 |
return sp.toString();
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Return a File cooresponding to the source of this location.
|
|
103 |
* Return null if not available.
|
|
104 |
*/
|
|
105 |
File sourceFile(Location loc) {
|
|
106 |
try {
|
|
107 |
String filename = loc.sourceName();
|
|
108 |
String refName = loc.declaringType().name();
|
|
109 |
int iDot = refName.lastIndexOf('.');
|
|
110 |
String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
|
|
111 |
String full = pkgName.replace('.', File.separatorChar) + filename;
|
|
112 |
for (int i= 0; i < dirs.length; ++i) {
|
|
113 |
File path = new File(dirs[i], full);
|
|
114 |
if (path.exists()) {
|
|
115 |
return path;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
return null;
|
|
119 |
} catch (AbsentInformationException e) {
|
|
120 |
return null;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Return a BufferedReader cooresponding to the source
|
|
126 |
* of this location.
|
|
127 |
* Return null if not available.
|
|
128 |
* Note: returned reader must be closed.
|
|
129 |
*/
|
|
130 |
BufferedReader sourceReader(Location loc) {
|
|
131 |
File sourceFile = sourceFile(loc);
|
|
132 |
if (sourceFile == null) {
|
|
133 |
return null;
|
|
134 |
}
|
|
135 |
try {
|
|
136 |
return new BufferedReader(new FileReader(sourceFile));
|
|
137 |
} catch(IOException exc) {
|
|
138 |
}
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
}
|