src/share/classes/com/sun/tools/classfile/Code_attribute.java

Tue, 20 Jan 2009 18:23:13 -0800

author
jjg
date
Tue, 20 Jan 2009 18:23:13 -0800
changeset 198
b4b1f7732289
parent 54
eaf608c64fec
child 229
03bcd66bd8e7
permissions
-rw-r--r--

6795903: fix latent build warnings in langtools repository
Reviewed-by: darcy

     1 /*
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.classfile;
    28 import java.io.IOException;
    30 /**
    31  * See JVMS3, section 4.8.3.
    32  *
    33  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    34  *  you write code that depends on this, you do so at your own risk.
    35  *  This code and its internal interfaces are subject to change or
    36  *  deletion without notice.</b>
    37  */
    38 public class Code_attribute extends Attribute {
    39     public class InvalidIndex extends AttributeException {
    40         private static final long serialVersionUID = -8904527774589382802L;
    41         InvalidIndex(int index) {
    42             this.index = index;
    43         }
    45         @Override
    46         public String getMessage() {
    47             // i18n
    48             return "invalid index " + index + " in Code attribute";
    49         }
    51         public final int index;
    52     }
    54     Code_attribute(ClassReader cr, int name_index, int length)
    55             throws IOException, ConstantPoolException {
    56         super(name_index, length);
    57         max_stack = cr.readUnsignedShort();
    58         max_locals = cr.readUnsignedShort();
    59         code_length = cr.readInt();
    60         code = new byte[code_length];
    61         cr.readFully(code);
    62         exception_table_langth = cr.readUnsignedShort();
    63         exception_table = new Exception_data[exception_table_langth];
    64         for (int i = 0; i < exception_table_langth; i++)
    65             exception_table[i] = new Exception_data(cr);
    66         attributes = new Attributes(cr);
    67     }
    69     public int getByte(int offset) throws InvalidIndex {
    70         if (offset < 0 || offset >= code.length)
    71             throw new InvalidIndex(offset);
    72         return code[offset];
    73     }
    75     public int getUnsignedByte(int offset) throws InvalidIndex {
    76         if (offset < 0 || offset >= code.length)
    77             throw new InvalidIndex(offset);
    78         return code[offset] & 0xff;
    79     }
    81     public int getShort(int offset) throws InvalidIndex {
    82         if (offset < 0 || offset + 1 >= code.length)
    83             throw new InvalidIndex(offset);
    84         return (code[offset] << 8) | (code[offset + 1] & 0xFF);
    85     }
    87     public int getUnsignedShort(int offset) throws InvalidIndex {
    88         if (offset < 0 || offset + 1 >= code.length)
    89             throw new InvalidIndex(offset);
    90         return ((code[offset] << 8) | (code[offset + 1] & 0xFF)) & 0xFFFF;
    91     }
    93     public int getInt(int offset) throws InvalidIndex {
    94         if (offset < 0 || offset + 3 >= code.length)
    95             throw new InvalidIndex(offset);
    96         return (getShort(offset) << 16) | (getShort(offset + 2) & 0xFFFF);
    97     }
    99     public <R, D> R accept(Visitor<R, D> visitor, D data) {
   100         return visitor.visitCode(this, data);
   101     }
   103     public final int max_stack;
   104     public final int max_locals;
   105     public final int code_length;
   106     public final byte[] code;
   107     public final int exception_table_langth;
   108     public final Exception_data[] exception_table;
   109     public final Attributes attributes;
   111     public class Exception_data {
   112         Exception_data(ClassReader cr) throws IOException {
   113             start_pc = cr.readUnsignedShort();
   114             end_pc = cr.readUnsignedShort();
   115             handler_pc = cr.readUnsignedShort();
   116             catch_type = cr.readUnsignedShort();
   117         }
   119         public final int start_pc;
   120         public final int end_pc;
   121         public final int handler_pc;
   122         public final int catch_type;
   123     }
   124 }

mercurial