|
1 /* |
|
2 * Copyright 2009 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 |
|
26 package com.sun.tools.javap; |
|
27 |
|
28 import com.sun.tools.classfile.Code_attribute; |
|
29 import com.sun.tools.classfile.Code_attribute.Exception_data; |
|
30 import com.sun.tools.classfile.Instruction; |
|
31 import java.util.ArrayList; |
|
32 import java.util.HashMap; |
|
33 import java.util.List; |
|
34 import java.util.ListIterator; |
|
35 import java.util.Map; |
|
36 |
|
37 /** |
|
38 * Annotate instructions with details about try blocks. |
|
39 * |
|
40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
41 * you write code that depends on this, you do so at your own risk. |
|
42 * This code and its internal interfaces are subject to change or |
|
43 * deletion without notice.</b> |
|
44 */ |
|
45 public class TryBlockWriter extends InstructionDetailWriter { |
|
46 public enum NoteKind { |
|
47 START("try") { |
|
48 public boolean match(Exception_data entry, int pc) { |
|
49 return (pc == entry.start_pc); |
|
50 } |
|
51 }, |
|
52 END("end try") { |
|
53 public boolean match(Exception_data entry, int pc) { |
|
54 return (pc == entry.end_pc); |
|
55 } |
|
56 }, |
|
57 HANDLER("catch") { |
|
58 public boolean match(Exception_data entry, int pc) { |
|
59 return (pc == entry.handler_pc); |
|
60 } |
|
61 }; |
|
62 NoteKind(String text) { |
|
63 this.text = text; |
|
64 } |
|
65 public abstract boolean match(Exception_data entry, int pc); |
|
66 public final String text; |
|
67 }; |
|
68 |
|
69 static TryBlockWriter instance(Context context) { |
|
70 TryBlockWriter instance = context.get(TryBlockWriter.class); |
|
71 if (instance == null) |
|
72 instance = new TryBlockWriter(context); |
|
73 return instance; |
|
74 } |
|
75 |
|
76 protected TryBlockWriter(Context context) { |
|
77 super(context); |
|
78 context.put(TryBlockWriter.class, this); |
|
79 constantWriter = ConstantWriter.instance(context); |
|
80 } |
|
81 |
|
82 public void reset(Code_attribute attr) { |
|
83 indexMap = new HashMap<Exception_data, Integer>(); |
|
84 pcMap = new HashMap<Integer, List<Exception_data>>(); |
|
85 for (int i = 0; i < attr.exception_table.length; i++) { |
|
86 Exception_data entry = attr.exception_table[i]; |
|
87 indexMap.put(entry, i); |
|
88 put(entry.start_pc, entry); |
|
89 put(entry.end_pc, entry); |
|
90 put(entry.handler_pc, entry); |
|
91 } |
|
92 } |
|
93 |
|
94 public void writeDetails(Instruction instr) { |
|
95 writeTrys(instr, NoteKind.END); |
|
96 writeTrys(instr, NoteKind.START); |
|
97 writeTrys(instr, NoteKind.HANDLER); |
|
98 } |
|
99 |
|
100 public void writeTrys(Instruction instr, NoteKind kind) { |
|
101 String indent = space(2); // get from Options? |
|
102 int pc = instr.getPC(); |
|
103 List<Exception_data> entries = pcMap.get(pc); |
|
104 if (entries != null) { |
|
105 for (ListIterator<Exception_data> iter = |
|
106 entries.listIterator(kind == NoteKind.END ? entries.size() : 0); |
|
107 kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) { |
|
108 Exception_data entry = |
|
109 kind == NoteKind.END ? iter.previous() : iter.next(); |
|
110 if (kind.match(entry, pc)) { |
|
111 print(indent); |
|
112 print(kind.text); |
|
113 print("["); |
|
114 print(indexMap.get(entry)); |
|
115 print("] "); |
|
116 if (entry.catch_type == 0) |
|
117 print("finally"); |
|
118 else { |
|
119 print("#" + entry.catch_type); |
|
120 print(" // "); |
|
121 constantWriter.write(entry.catch_type); |
|
122 } |
|
123 println(); |
|
124 } |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 private void put(int pc, Exception_data entry) { |
|
130 List<Exception_data> list = pcMap.get(pc); |
|
131 if (list == null) { |
|
132 list = new ArrayList<Exception_data>(); |
|
133 pcMap.put(pc, list); |
|
134 } |
|
135 if (!list.contains(entry)) |
|
136 list.add(entry); |
|
137 } |
|
138 |
|
139 private Map<Integer, List<Exception_data>> pcMap; |
|
140 private Map<Exception_data, Integer> indexMap; |
|
141 private ConstantWriter constantWriter; |
|
142 } |