Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/javap/TryBlockWriter.java
58461 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javap;2627import com.sun.tools.classfile.Code_attribute;28import com.sun.tools.classfile.Code_attribute.Exception_data;29import com.sun.tools.classfile.Instruction;30import java.util.ArrayList;31import java.util.HashMap;32import java.util.List;33import java.util.ListIterator;34import java.util.Map;3536/**37* Annotate instructions with details about try blocks.38*39* <p><b>This is NOT part of any supported API.40* If you write code that depends on this, you do so at your own risk.41* This code and its internal interfaces are subject to change or42* deletion without notice.</b>43*/44public class TryBlockWriter extends InstructionDetailWriter {45public enum NoteKind {46START("try") {47public boolean match(Exception_data entry, int pc) {48return (pc == entry.start_pc);49}50},51END("end try") {52public boolean match(Exception_data entry, int pc) {53return (pc == entry.end_pc);54}55},56HANDLER("catch") {57public boolean match(Exception_data entry, int pc) {58return (pc == entry.handler_pc);59}60};61NoteKind(String text) {62this.text = text;63}64public abstract boolean match(Exception_data entry, int pc);65public final String text;66}6768static TryBlockWriter instance(Context context) {69TryBlockWriter instance = context.get(TryBlockWriter.class);70if (instance == null)71instance = new TryBlockWriter(context);72return instance;73}7475protected TryBlockWriter(Context context) {76super(context);77context.put(TryBlockWriter.class, this);78constantWriter = ConstantWriter.instance(context);79}8081public void reset(Code_attribute attr) {82indexMap = new HashMap<>();83pcMap = new HashMap<>();84for (int i = 0; i < attr.exception_table.length; i++) {85Exception_data entry = attr.exception_table[i];86indexMap.put(entry, i);87put(entry.start_pc, entry);88put(entry.end_pc, entry);89put(entry.handler_pc, entry);90}91}9293public void writeDetails(Instruction instr) {94writeTrys(instr, NoteKind.END);95writeTrys(instr, NoteKind.START);96writeTrys(instr, NoteKind.HANDLER);97}9899public void writeTrys(Instruction instr, NoteKind kind) {100String indent = space(2); // get from Options?101int pc = instr.getPC();102List<Exception_data> entries = pcMap.get(pc);103if (entries != null) {104for (ListIterator<Exception_data> iter =105entries.listIterator(kind == NoteKind.END ? entries.size() : 0);106kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) {107Exception_data entry =108kind == NoteKind.END ? iter.previous() : iter.next();109if (kind.match(entry, pc)) {110print(indent);111print(kind.text);112print("[");113print(indexMap.get(entry));114print("] ");115if (entry.catch_type == 0)116print("finally");117else {118print("#" + entry.catch_type);119print(" // ");120constantWriter.write(entry.catch_type);121}122println();123}124}125}126}127128private void put(int pc, Exception_data entry) {129List<Exception_data> list = pcMap.get(pc);130if (list == null) {131list = new ArrayList<>();132pcMap.put(pc, list);133}134if (!list.contains(entry))135list.add(entry);136}137138private Map<Integer, List<Exception_data>> pcMap;139private Map<Exception_data, Integer> indexMap;140private ConstantWriter constantWriter;141}142143144