langtools/src/share/classes/com/sun/tools/javap/LocalVariableTypeTableWriter.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 22163 3651128c74eb
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4076
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
     4
 *
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f391c41bae7e 6843013: missing files in fix for 6824493
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: 4076
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4076
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    10
 *
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    15
 * accompanied this code).
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    16
 *
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4076
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4076
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4076
diff changeset
    23
 * questions.
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    24
 */
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    25
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    26
package com.sun.tools.javap;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    27
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    28
import com.sun.tools.classfile.Attribute;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    29
import com.sun.tools.classfile.Code_attribute;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    30
import com.sun.tools.classfile.ConstantPool;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    31
import com.sun.tools.classfile.ConstantPoolException;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    32
import com.sun.tools.classfile.Descriptor;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    33
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    34
import com.sun.tools.classfile.Instruction;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    35
import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    36
import com.sun.tools.classfile.Signature;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    37
import java.util.ArrayList;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    38
import java.util.HashMap;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    39
import java.util.List;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    40
import java.util.ListIterator;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    41
import java.util.Map;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    42
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    43
/**
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    44
 * Annotate instructions with details about local variables.
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    45
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    46
 *  <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
    47
 *  If you write code that depends on this, you do so at your own risk.
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    48
 *  This code and its internal interfaces are subject to change or
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    49
 *  deletion without notice.</b>
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    50
 */
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    51
public class LocalVariableTypeTableWriter extends  InstructionDetailWriter {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    52
    public enum NoteKind {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    53
        START("start") {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    54
            public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    55
                return (pc == entry.start_pc);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    56
            }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    57
        },
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    58
        END("end") {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    59
            public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    60
                return (pc == entry.start_pc + entry.length);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    61
            }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    62
        };
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    63
        NoteKind(String text) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    64
            this.text = text;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    65
        }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    66
        public abstract boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    67
        public final String text;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    68
    };
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    69
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    70
    static LocalVariableTypeTableWriter instance(Context context) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    71
        LocalVariableTypeTableWriter instance = context.get(LocalVariableTypeTableWriter.class);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    72
        if (instance == null)
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    73
            instance = new LocalVariableTypeTableWriter(context);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    74
        return instance;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    75
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    76
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    77
    protected LocalVariableTypeTableWriter(Context context) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    78
        super(context);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    79
        context.put(LocalVariableTypeTableWriter.class, this);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    80
        classWriter = ClassWriter.instance(context);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    81
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    82
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    83
    public void reset(Code_attribute attr) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    84
        codeAttr = attr;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    85
        pcMap = new HashMap<Integer, List<LocalVariableTypeTable_attribute.Entry>>();
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    86
        LocalVariableTypeTable_attribute lvt =
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    87
                (LocalVariableTypeTable_attribute) (attr.attributes.get(Attribute.LocalVariableTypeTable));
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    88
        if (lvt == null)
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    89
            return;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    90
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    91
        for (int i = 0; i < lvt.local_variable_table.length; i++) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    92
            LocalVariableTypeTable_attribute.Entry entry = lvt.local_variable_table[i];
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    93
            put(entry.start_pc, entry);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    94
            put(entry.start_pc + entry.length, entry);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    95
        }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    96
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    97
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    98
    public void writeDetails(Instruction instr) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
    99
        int pc = instr.getPC();
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   100
        writeLocalVariables(pc, NoteKind.END);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   101
        writeLocalVariables(pc, NoteKind.START);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   102
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   103
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   104
    @Override
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   105
    public void flush() {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   106
        int pc = codeAttr.code_length;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   107
        writeLocalVariables(pc, NoteKind.END);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   108
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   109
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   110
    public void writeLocalVariables(int pc, NoteKind kind) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   111
        ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   112
        String indent = space(2); // get from Options?
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   113
        List<LocalVariableTypeTable_attribute.Entry> entries = pcMap.get(pc);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   114
        if (entries != null) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   115
            for (ListIterator<LocalVariableTypeTable_attribute.Entry> iter =
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   116
                    entries.listIterator(kind == NoteKind.END ? entries.size() : 0);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   117
                    kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   118
                LocalVariableTypeTable_attribute.Entry entry =
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   119
                        kind == NoteKind.END ? iter.previous() : iter.next();
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   120
                if (kind.match(entry, pc)) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   121
                    print(indent);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   122
                    print(kind.text);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   123
                    print(" generic local ");
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   124
                    print(entry.index);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   125
                    print(" // ");
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   126
                    Descriptor d = new Signature(entry.signature_index);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   127
                    try {
4076
319c19c1f28d 6888367: classfile library parses signature attributes incorrectly
jjg
parents: 2980
diff changeset
   128
                        print(d.getFieldType(constant_pool).toString().replace("/", "."));
2980
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   129
                    } catch (InvalidDescriptor e) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   130
                        print(report(e));
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   131
                    } catch (ConstantPoolException e) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   132
                        print(report(e));
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   133
                    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   134
                    print(" ");
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   135
                    try {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   136
                        print(constant_pool.getUTF8Value(entry.name_index));
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   137
                    } catch (ConstantPoolException e) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   138
                        print(report(e));
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   139
                    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   140
                    println();
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   141
                }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   142
            }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   143
        }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   144
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   145
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   146
    private void put(int pc, LocalVariableTypeTable_attribute.Entry entry) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   147
        List<LocalVariableTypeTable_attribute.Entry> list = pcMap.get(pc);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   148
        if (list == null) {
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   149
            list = new ArrayList<LocalVariableTypeTable_attribute.Entry>();
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   150
            pcMap.put(pc, list);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   151
        }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   152
        if (!list.contains(entry))
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   153
            list.add(entry);
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   154
    }
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   155
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   156
    private ClassWriter classWriter;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   157
    private Code_attribute codeAttr;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   158
    private Map<Integer, List<LocalVariableTypeTable_attribute.Entry>> pcMap;
f391c41bae7e 6843013: missing files in fix for 6824493
jjg
parents:
diff changeset
   159
}