Tue, 08 Jan 2013 10:17:29 +0100
8005184: Restructure DeferredAttr to allow pluggable deferred type completers
Summary: Add hooks to generalize deferred type completion via custom helper objects
Reviewed-by: jjg
jjg@283 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
jjg@283 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@283 | 4 | * |
jjg@283 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@283 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@283 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@283 | 10 | * |
jjg@283 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@283 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@283 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@283 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@283 | 15 | * accompanied this code). |
jjg@283 | 16 | * |
jjg@283 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@283 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@283 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@283 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
jjg@283 | 24 | */ |
jjg@283 | 25 | |
jjg@283 | 26 | package com.sun.tools.javap; |
jjg@283 | 27 | |
jjg@283 | 28 | import com.sun.tools.classfile.Code_attribute; |
jjg@283 | 29 | import com.sun.tools.classfile.Code_attribute.Exception_data; |
jjg@283 | 30 | import com.sun.tools.classfile.Instruction; |
jjg@283 | 31 | import java.util.ArrayList; |
jjg@283 | 32 | import java.util.HashMap; |
jjg@283 | 33 | import java.util.List; |
jjg@283 | 34 | import java.util.ListIterator; |
jjg@283 | 35 | import java.util.Map; |
jjg@283 | 36 | |
jjg@283 | 37 | /** |
jjg@283 | 38 | * Annotate instructions with details about try blocks. |
jjg@283 | 39 | * |
jjg@581 | 40 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 41 | * If you write code that depends on this, you do so at your own risk. |
jjg@283 | 42 | * This code and its internal interfaces are subject to change or |
jjg@283 | 43 | * deletion without notice.</b> |
jjg@283 | 44 | */ |
jjg@283 | 45 | public class TryBlockWriter extends InstructionDetailWriter { |
jjg@283 | 46 | public enum NoteKind { |
jjg@283 | 47 | START("try") { |
jjg@283 | 48 | public boolean match(Exception_data entry, int pc) { |
jjg@283 | 49 | return (pc == entry.start_pc); |
jjg@283 | 50 | } |
jjg@283 | 51 | }, |
jjg@283 | 52 | END("end try") { |
jjg@283 | 53 | public boolean match(Exception_data entry, int pc) { |
jjg@283 | 54 | return (pc == entry.end_pc); |
jjg@283 | 55 | } |
jjg@283 | 56 | }, |
jjg@283 | 57 | HANDLER("catch") { |
jjg@283 | 58 | public boolean match(Exception_data entry, int pc) { |
jjg@283 | 59 | return (pc == entry.handler_pc); |
jjg@283 | 60 | } |
jjg@283 | 61 | }; |
jjg@283 | 62 | NoteKind(String text) { |
jjg@283 | 63 | this.text = text; |
jjg@283 | 64 | } |
jjg@283 | 65 | public abstract boolean match(Exception_data entry, int pc); |
jjg@283 | 66 | public final String text; |
jjg@283 | 67 | }; |
jjg@283 | 68 | |
jjg@283 | 69 | static TryBlockWriter instance(Context context) { |
jjg@283 | 70 | TryBlockWriter instance = context.get(TryBlockWriter.class); |
jjg@283 | 71 | if (instance == null) |
jjg@283 | 72 | instance = new TryBlockWriter(context); |
jjg@283 | 73 | return instance; |
jjg@283 | 74 | } |
jjg@283 | 75 | |
jjg@283 | 76 | protected TryBlockWriter(Context context) { |
jjg@283 | 77 | super(context); |
jjg@283 | 78 | context.put(TryBlockWriter.class, this); |
jjg@283 | 79 | constantWriter = ConstantWriter.instance(context); |
jjg@283 | 80 | } |
jjg@283 | 81 | |
jjg@283 | 82 | public void reset(Code_attribute attr) { |
jjg@283 | 83 | indexMap = new HashMap<Exception_data, Integer>(); |
jjg@283 | 84 | pcMap = new HashMap<Integer, List<Exception_data>>(); |
jjg@283 | 85 | for (int i = 0; i < attr.exception_table.length; i++) { |
jjg@283 | 86 | Exception_data entry = attr.exception_table[i]; |
jjg@283 | 87 | indexMap.put(entry, i); |
jjg@283 | 88 | put(entry.start_pc, entry); |
jjg@283 | 89 | put(entry.end_pc, entry); |
jjg@283 | 90 | put(entry.handler_pc, entry); |
jjg@283 | 91 | } |
jjg@283 | 92 | } |
jjg@283 | 93 | |
jjg@283 | 94 | public void writeDetails(Instruction instr) { |
jjg@283 | 95 | writeTrys(instr, NoteKind.END); |
jjg@283 | 96 | writeTrys(instr, NoteKind.START); |
jjg@283 | 97 | writeTrys(instr, NoteKind.HANDLER); |
jjg@283 | 98 | } |
jjg@283 | 99 | |
jjg@283 | 100 | public void writeTrys(Instruction instr, NoteKind kind) { |
jjg@283 | 101 | String indent = space(2); // get from Options? |
jjg@283 | 102 | int pc = instr.getPC(); |
jjg@283 | 103 | List<Exception_data> entries = pcMap.get(pc); |
jjg@283 | 104 | if (entries != null) { |
jjg@283 | 105 | for (ListIterator<Exception_data> iter = |
jjg@283 | 106 | entries.listIterator(kind == NoteKind.END ? entries.size() : 0); |
jjg@283 | 107 | kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) { |
jjg@283 | 108 | Exception_data entry = |
jjg@283 | 109 | kind == NoteKind.END ? iter.previous() : iter.next(); |
jjg@283 | 110 | if (kind.match(entry, pc)) { |
jjg@283 | 111 | print(indent); |
jjg@283 | 112 | print(kind.text); |
jjg@283 | 113 | print("["); |
jjg@283 | 114 | print(indexMap.get(entry)); |
jjg@283 | 115 | print("] "); |
jjg@283 | 116 | if (entry.catch_type == 0) |
jjg@283 | 117 | print("finally"); |
jjg@283 | 118 | else { |
jjg@283 | 119 | print("#" + entry.catch_type); |
jjg@283 | 120 | print(" // "); |
jjg@283 | 121 | constantWriter.write(entry.catch_type); |
jjg@283 | 122 | } |
jjg@283 | 123 | println(); |
jjg@283 | 124 | } |
jjg@283 | 125 | } |
jjg@283 | 126 | } |
jjg@283 | 127 | } |
jjg@283 | 128 | |
jjg@283 | 129 | private void put(int pc, Exception_data entry) { |
jjg@283 | 130 | List<Exception_data> list = pcMap.get(pc); |
jjg@283 | 131 | if (list == null) { |
jjg@283 | 132 | list = new ArrayList<Exception_data>(); |
jjg@283 | 133 | pcMap.put(pc, list); |
jjg@283 | 134 | } |
jjg@283 | 135 | if (!list.contains(entry)) |
jjg@283 | 136 | list.add(entry); |
jjg@283 | 137 | } |
jjg@283 | 138 | |
jjg@283 | 139 | private Map<Integer, List<Exception_data>> pcMap; |
jjg@283 | 140 | private Map<Exception_data, Integer> indexMap; |
jjg@283 | 141 | private ConstantWriter constantWriter; |
jjg@283 | 142 | } |