langtools/src/share/classes/com/sun/tools/classfile/Code_attribute.java
author jjg
Mon, 30 Mar 2009 15:08:09 -0700
changeset 2512 70eb5f17c5f8
parent 2212 1d3dc0e0ba0c
child 5520 86e4b9a9da40
permissions -rw-r--r--
6819246: improve support for decoding instructions in classfile library Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     1
/*
2212
1d3dc0e0ba0c 6814575: Update copyright year
xdono
parents: 1870
diff changeset
     2
 * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     4
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    10
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    15
 * accompanied this code).
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    16
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    20
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    23
 * have any questions.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    24
 */
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    25
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    26
package com.sun.tools.classfile;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    27
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    28
import java.io.IOException;
2512
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
    29
import java.util.Iterator;
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
    30
import java.util.NoSuchElementException;
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    31
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    32
/**
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    33
 * See JVMS3, section 4.8.3.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    34
 *
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    35
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    36
 *  you write code that depends on this, you do so at your own risk.
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    37
 *  This code and its internal interfaces are subject to change or
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    38
 *  deletion without notice.</b>
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    39
 */
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    40
public class Code_attribute extends Attribute {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    41
    public class InvalidIndex extends AttributeException {
1870
57a1138dffc8 6795903: fix latent build warnings in langtools repository
jjg
parents: 735
diff changeset
    42
        private static final long serialVersionUID = -8904527774589382802L;
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    43
        InvalidIndex(int index) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    44
            this.index = index;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    45
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    46
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    47
        @Override
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    48
        public String getMessage() {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    49
            // i18n
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    50
            return "invalid index " + index + " in Code attribute";
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    51
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    52
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    53
        public final int index;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    54
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    55
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    56
    Code_attribute(ClassReader cr, int name_index, int length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    57
            throws IOException, ConstantPoolException {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    58
        super(name_index, length);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    59
        max_stack = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    60
        max_locals = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    61
        code_length = cr.readInt();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    62
        code = new byte[code_length];
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    63
        cr.readFully(code);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    64
        exception_table_langth = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    65
        exception_table = new Exception_data[exception_table_langth];
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    66
        for (int i = 0; i < exception_table_langth; i++)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    67
            exception_table[i] = new Exception_data(cr);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    68
        attributes = new Attributes(cr);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    69
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    70
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    71
    public int getByte(int offset) throws InvalidIndex {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    72
        if (offset < 0 || offset >= code.length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    73
            throw new InvalidIndex(offset);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    74
        return code[offset];
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    75
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    76
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    77
    public int getUnsignedByte(int offset) throws InvalidIndex {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    78
        if (offset < 0 || offset >= code.length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    79
            throw new InvalidIndex(offset);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    80
        return code[offset] & 0xff;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    81
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    82
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    83
    public int getShort(int offset) throws InvalidIndex {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    84
        if (offset < 0 || offset + 1 >= code.length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    85
            throw new InvalidIndex(offset);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    86
        return (code[offset] << 8) | (code[offset + 1] & 0xFF);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    87
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    88
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    89
    public int getUnsignedShort(int offset) throws InvalidIndex {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    90
        if (offset < 0 || offset + 1 >= code.length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    91
            throw new InvalidIndex(offset);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    92
        return ((code[offset] << 8) | (code[offset + 1] & 0xFF)) & 0xFFFF;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    93
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    94
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    95
    public int getInt(int offset) throws InvalidIndex {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    96
        if (offset < 0 || offset + 3 >= code.length)
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    97
            throw new InvalidIndex(offset);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    98
        return (getShort(offset) << 16) | (getShort(offset + 2) & 0xFFFF);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
    99
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   100
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   101
    public <R, D> R accept(Visitor<R, D> visitor, D data) {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   102
        return visitor.visitCode(this, data);
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   103
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   104
2512
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   105
    public Iterable<Instruction> getInstructions() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   106
        return new Iterable<Instruction>() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   107
            public Iterator<Instruction> iterator() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   108
                return new Iterator<Instruction>() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   109
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   110
                    public boolean hasNext() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   111
                        return (next != null);
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   112
                    }
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   113
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   114
                    public Instruction next() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   115
                        if (next == null)
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   116
                            throw new NoSuchElementException();
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   117
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   118
                        current = next;
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   119
                        pc += current.length();
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   120
                        next = (pc < code.length ? new Instruction(code, pc) : null);
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   121
                        return current;
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   122
                    }
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   123
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   124
                    public void remove() {
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   125
                        throw new UnsupportedOperationException("Not supported.");
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   126
                    }
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   127
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   128
                    Instruction current = null;
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   129
                    int pc = 0;
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   130
                    Instruction next = new Instruction(code, pc);
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   131
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   132
                };
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   133
            }
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   134
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   135
        };
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   136
    }
70eb5f17c5f8 6819246: improve support for decoding instructions in classfile library
jjg
parents: 2212
diff changeset
   137
727
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   138
    public final int max_stack;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   139
    public final int max_locals;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   140
    public final int code_length;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   141
    public final byte[] code;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   142
    public final int exception_table_langth;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   143
    public final Exception_data[] exception_table;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   144
    public final Attributes attributes;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   145
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   146
    public class Exception_data {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   147
        Exception_data(ClassReader cr) throws IOException {
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   148
            start_pc = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   149
            end_pc = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   150
            handler_pc = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   151
            catch_type = cr.readUnsignedShort();
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   152
        }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   153
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   154
        public final int start_pc;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   155
        public final int end_pc;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   156
        public final int handler_pc;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   157
        public final int catch_type;
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   158
    }
cb50c1ae7bab 4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff changeset
   159
}