author | jjg |
Tue, 19 May 2009 11:50:54 -0700 | |
changeset 2979 | ea39317acd3d |
child 5520 | 86e4b9a9da40 |
permissions | -rw-r--r-- |
2979
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
1 |
/* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
2 |
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
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 |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Sun designates this |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
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 |
* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
23 |
* have any questions. |
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 com.sun.tools.classfile.Code_attribute; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
29 |
import com.sun.tools.classfile.Code_attribute.Exception_data; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
30 |
import com.sun.tools.classfile.Instruction; |
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.HashMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
33 |
import java.util.List; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
34 |
import java.util.ListIterator; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
35 |
import java.util.Map; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
36 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
37 |
/** |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
38 |
* Annotate instructions with details about try blocks. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
39 |
* |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
40 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
41 |
* you write code that depends on this, you do so at your own risk. |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
42 |
* This code and its internal interfaces are subject to change or |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
43 |
* deletion without notice.</b> |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
44 |
*/ |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
45 |
public class TryBlockWriter extends InstructionDetailWriter { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
46 |
public enum NoteKind { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
47 |
START("try") { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
48 |
public boolean match(Exception_data entry, int pc) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
49 |
return (pc == entry.start_pc); |
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 |
END("end try") { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
53 |
public boolean match(Exception_data entry, int pc) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
54 |
return (pc == entry.end_pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
55 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
56 |
}, |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
57 |
HANDLER("catch") { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
58 |
public boolean match(Exception_data entry, int pc) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
59 |
return (pc == entry.handler_pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
60 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
61 |
}; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
62 |
NoteKind(String text) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
63 |
this.text = text; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
64 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
65 |
public abstract boolean match(Exception_data entry, int pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
66 |
public final String text; |
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 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
69 |
static TryBlockWriter instance(Context context) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
70 |
TryBlockWriter instance = context.get(TryBlockWriter.class); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
71 |
if (instance == null) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
72 |
instance = new TryBlockWriter(context); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
73 |
return instance; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
74 |
} |
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 |
protected TryBlockWriter(Context context) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
77 |
super(context); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
78 |
context.put(TryBlockWriter.class, this); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
79 |
constantWriter = ConstantWriter.instance(context); |
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 reset(Code_attribute attr) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
83 |
indexMap = new HashMap<Exception_data, Integer>(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
84 |
pcMap = new HashMap<Integer, List<Exception_data>>(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
85 |
for (int i = 0; i < attr.exception_table.length; i++) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
86 |
Exception_data entry = attr.exception_table[i]; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
87 |
indexMap.put(entry, i); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
88 |
put(entry.start_pc, entry); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
89 |
put(entry.end_pc, entry); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
90 |
put(entry.handler_pc, entry); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
91 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
92 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
93 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
94 |
public void writeDetails(Instruction instr) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
95 |
writeTrys(instr, NoteKind.END); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
96 |
writeTrys(instr, NoteKind.START); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
97 |
writeTrys(instr, NoteKind.HANDLER); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
98 |
} |
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 |
public void writeTrys(Instruction instr, NoteKind kind) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
101 |
String indent = space(2); // get from Options? |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
102 |
int pc = instr.getPC(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
103 |
List<Exception_data> entries = pcMap.get(pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
104 |
if (entries != null) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
105 |
for (ListIterator<Exception_data> iter = |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
106 |
entries.listIterator(kind == NoteKind.END ? entries.size() : 0); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
107 |
kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
108 |
Exception_data entry = |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
109 |
kind == NoteKind.END ? iter.previous() : iter.next(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
110 |
if (kind.match(entry, pc)) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
111 |
print(indent); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
112 |
print(kind.text); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
113 |
print("["); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
114 |
print(indexMap.get(entry)); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
115 |
print("] "); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
116 |
if (entry.catch_type == 0) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
117 |
print("finally"); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
118 |
else { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
119 |
print("#" + entry.catch_type); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
120 |
print(" // "); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
121 |
constantWriter.write(entry.catch_type); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
122 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
123 |
println(); |
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 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
128 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
129 |
private void put(int pc, Exception_data entry) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
130 |
List<Exception_data> list = pcMap.get(pc); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
131 |
if (list == null) { |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
132 |
list = new ArrayList<Exception_data>(); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
133 |
pcMap.put(pc, list); |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
134 |
} |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
135 |
if (!list.contains(entry)) |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
136 |
list.add(entry); |
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 |
|
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
139 |
private Map<Integer, List<Exception_data>> pcMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
140 |
private Map<Exception_data, Integer> indexMap; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
141 |
private ConstantWriter constantWriter; |
ea39317acd3d
6824493: experimental support for additional info for instructions
jjg
parents:
diff
changeset
|
142 |
} |