langtools/src/share/classes/com/sun/tools/javap/TryBlockWriter.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 5847 1908176fd6e3
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2979
ea39317acd3d 6824493: experimental support for additional info for instructions
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
     2
 * Copyright (c) 2009, 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
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
     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
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
     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
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 2979
diff changeset
    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 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
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    40
 *  <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
    41
 *  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
    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;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 5847
diff changeset
    67
    }
2979
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) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 5847
diff changeset
    83
        indexMap = new HashMap<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 5847
diff changeset
    84
        pcMap = new HashMap<>();
2979
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) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 5847
diff changeset
   132
            list = new ArrayList<>();
2979
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
}