author | duke |
Wed, 05 Jul 2017 20:46:50 +0200 | |
changeset 32264 | 58af228a68fa |
parent 30846 | 2b3f379840f0 |
permissions | -rw-r--r-- |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
1 |
/* |
7681 | 2 |
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
4 |
* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
10 |
* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
15 |
* accompanied this code). |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
16 |
* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
20 |
* |
5520 | 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. |
|
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
24 |
*/ |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
25 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
26 |
package com.sun.tools.javap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
27 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
28 |
import java.io.BufferedReader; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
29 |
import java.io.IOException; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
30 |
import java.io.StringReader; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
31 |
import java.util.ArrayList; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
32 |
import java.util.List; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
33 |
import java.util.Set; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
34 |
import java.util.SortedMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
35 |
import java.util.SortedSet; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
36 |
import java.util.TreeMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
37 |
import java.util.TreeSet; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
38 |
import javax.tools.JavaFileManager; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
39 |
import javax.tools.JavaFileManager.Location; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
40 |
import javax.tools.JavaFileObject; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
41 |
import javax.tools.StandardLocation; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
42 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
43 |
import com.sun.tools.classfile.Attribute; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
44 |
import com.sun.tools.classfile.ClassFile; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
45 |
import com.sun.tools.classfile.Code_attribute; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
46 |
import com.sun.tools.classfile.ConstantPoolException; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
47 |
import com.sun.tools.classfile.Instruction; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
48 |
import com.sun.tools.classfile.LineNumberTable_attribute; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
49 |
import com.sun.tools.classfile.SourceFile_attribute; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
50 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
51 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
52 |
/** |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
53 |
* Annotate instructions with source code. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
54 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
55 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
56 |
* If you write code that depends on this, you do so at your own risk. |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
57 |
* This code and its internal interfaces are subject to change or |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
58 |
* deletion without notice.</b> |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
59 |
*/ |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
60 |
public class SourceWriter extends InstructionDetailWriter { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
61 |
static SourceWriter instance(Context context) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
62 |
SourceWriter instance = context.get(SourceWriter.class); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
63 |
if (instance == null) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
64 |
instance = new SourceWriter(context); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
65 |
return instance; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
66 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
67 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
68 |
protected SourceWriter(Context context) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
69 |
super(context); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
70 |
context.put(SourceWriter.class, this); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
71 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
72 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
73 |
void setFileManager(JavaFileManager fileManager) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
74 |
this.fileManager = fileManager; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
75 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
76 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
77 |
public void reset(ClassFile cf, Code_attribute attr) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
78 |
setSource(cf); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
79 |
setLineMap(attr); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
80 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
81 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
82 |
public void writeDetails(Instruction instr) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
83 |
String indent = space(40); // could get from Options? |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
84 |
Set<Integer> lines = lineMap.get(instr.getPC()); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
85 |
if (lines != null) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
86 |
for (int line: lines) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
87 |
print(indent); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
88 |
print(String.format(" %4d ", line)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
89 |
if (line < sourceLines.length) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
90 |
print(sourceLines[line]); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
91 |
println(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
92 |
int nextLine = nextLine(line); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
93 |
for (int i = line + 1; i < nextLine; i++) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
94 |
print(indent); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
95 |
print(String.format("(%4d)", i)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
96 |
if (i < sourceLines.length) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
97 |
print(sourceLines[i]); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
98 |
println(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
99 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
100 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
101 |
} |
6578
964b4d71d26f
6980017: javap -XDdetail:source behaves badly if source not available.
jjg
parents:
5847
diff
changeset
|
102 |
} |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
103 |
|
6578
964b4d71d26f
6980017: javap -XDdetail:source behaves badly if source not available.
jjg
parents:
5847
diff
changeset
|
104 |
public boolean hasSource() { |
964b4d71d26f
6980017: javap -XDdetail:source behaves badly if source not available.
jjg
parents:
5847
diff
changeset
|
105 |
return (sourceLines.length > 0); |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
106 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
107 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
108 |
private void setLineMap(Code_attribute attr) { |
22163 | 109 |
SortedMap<Integer, SortedSet<Integer>> map = new TreeMap<>(); |
110 |
SortedSet<Integer> allLines = new TreeSet<>(); |
|
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
111 |
for (Attribute a: attr.attributes) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
112 |
if (a instanceof LineNumberTable_attribute) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
113 |
LineNumberTable_attribute t = (LineNumberTable_attribute) a; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
114 |
for (LineNumberTable_attribute.Entry e: t.line_number_table) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
115 |
int start_pc = e.start_pc; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
116 |
int line = e.line_number; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
117 |
SortedSet<Integer> pcLines = map.get(start_pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
118 |
if (pcLines == null) { |
22163 | 119 |
pcLines = new TreeSet<>(); |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
120 |
map.put(start_pc, pcLines); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
121 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
122 |
pcLines.add(line); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
123 |
allLines.add(line); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
124 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
125 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
126 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
127 |
lineMap = map; |
22163 | 128 |
lineList = new ArrayList<>(allLines); |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
129 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
130 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
131 |
private void setSource(ClassFile cf) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
132 |
if (cf != classFile) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
133 |
classFile = cf; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
134 |
sourceLines = splitLines(readSource(cf)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
135 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
136 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
137 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
138 |
private String readSource(ClassFile cf) { |
3141
79c119fae7fb
6852856: javap changes to facilitate subclassing javap for variants
jjg
parents:
2979
diff
changeset
|
139 |
if (fileManager == null) |
79c119fae7fb
6852856: javap changes to facilitate subclassing javap for variants
jjg
parents:
2979
diff
changeset
|
140 |
return null; |
79c119fae7fb
6852856: javap changes to facilitate subclassing javap for variants
jjg
parents:
2979
diff
changeset
|
141 |
|
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
142 |
Location location; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
143 |
if (fileManager.hasLocation((StandardLocation.SOURCE_PATH))) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
144 |
location = StandardLocation.SOURCE_PATH; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
145 |
else |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
146 |
location = StandardLocation.CLASS_PATH; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
147 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
148 |
// Guess the source file for a class from the package name for this |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
149 |
// class and the base of the source file. This avoids having to read |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
150 |
// additional classes to determine the outmost class from any |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
151 |
// InnerClasses and EnclosingMethod attributes. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
152 |
try { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
153 |
String className = cf.getName(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
154 |
SourceFile_attribute sf = |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
155 |
(SourceFile_attribute) cf.attributes.get(Attribute.SourceFile); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
156 |
if (sf == null) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
157 |
report(messages.getMessage("err.no.SourceFile.attribute")); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
158 |
return null; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
159 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
160 |
String sourceFile = sf.getSourceFile(cf.constant_pool); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
161 |
String fileBase = sourceFile.endsWith(".java") |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
162 |
? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
163 |
int sep = className.lastIndexOf("/"); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
164 |
String pkgName = (sep == -1 ? "" : className.substring(0, sep+1)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
165 |
String topClassName = (pkgName + fileBase).replace('/', '.'); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
166 |
JavaFileObject fo = |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
167 |
fileManager.getJavaFileForInput(location, |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
168 |
topClassName, |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
169 |
JavaFileObject.Kind.SOURCE); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
170 |
if (fo == null) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
171 |
report(messages.getMessage("err.source.file.not.found")); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
172 |
return null; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
173 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
174 |
return fo.getCharContent(true).toString(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
175 |
} catch (ConstantPoolException e) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
176 |
report(e); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
177 |
return null; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
178 |
} catch (IOException e) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
179 |
report(e.getLocalizedMessage()); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
180 |
return null; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
181 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
182 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
183 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
184 |
private static String[] splitLines(String text) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
185 |
if (text == null) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
186 |
return new String[0]; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
187 |
|
22163 | 188 |
List<String> lines = new ArrayList<>(); |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
189 |
lines.add(""); // dummy line 0 |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
190 |
try { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
191 |
BufferedReader r = new BufferedReader(new StringReader(text)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
192 |
String line; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
193 |
while ((line = r.readLine()) != null) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
194 |
lines.add(line); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
195 |
} catch (IOException ignore) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
196 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
197 |
return lines.toArray(new String[lines.size()]); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
198 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
199 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
200 |
private int nextLine(int line) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
201 |
int i = lineList.indexOf(line); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
202 |
if (i == -1 || i == lineList.size() - 1) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
203 |
return - 1; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
204 |
return lineList.get(i + 1); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
205 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
206 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
207 |
private JavaFileManager fileManager; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
208 |
private ClassFile classFile; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
209 |
private SortedMap<Integer, SortedSet<Integer>> lineMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
210 |
private List<Integer> lineList; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
211 |
private String[] sourceLines; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
212 |
} |