001    /*
002     * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.classfile;
027    
028    import java.io.IOException;
029    
030    /**
031     * See JVMS3, section 4.8.3.
032     *
033     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
034     *  you write code that depends on this, you do so at your own risk.
035     *  This code and its internal interfaces are subject to change or
036     *  deletion without notice.</b>
037     */
038    public class Code_attribute extends Attribute {
039        public class InvalidIndex extends AttributeException {
040            private static final long serialVersionUID = -8904527774589382802L;
041            InvalidIndex(int index) {
042                this.index = index;
043            }
044    
045            @Override
046            public String getMessage() {
047                // i18n
048                return "invalid index " + index + " in Code attribute";
049            }
050    
051            public final int index;
052        }
053    
054        Code_attribute(ClassReader cr, int name_index, int length)
055                throws IOException, ConstantPoolException {
056            super(name_index, length);
057            max_stack = cr.readUnsignedShort();
058            max_locals = cr.readUnsignedShort();
059            code_length = cr.readInt();
060            code = new byte[code_length];
061            cr.readFully(code);
062            exception_table_langth = cr.readUnsignedShort();
063            exception_table = new Exception_data[exception_table_langth];
064            for (int i = 0; i < exception_table_langth; i++)
065                exception_table[i] = new Exception_data(cr);
066            attributes = new Attributes(cr);
067        }
068    
069        public int getByte(int offset) throws InvalidIndex {
070            if (offset < 0 || offset >= code.length)
071                throw new InvalidIndex(offset);
072            return code[offset];
073        }
074    
075        public int getUnsignedByte(int offset) throws InvalidIndex {
076            if (offset < 0 || offset >= code.length)
077                throw new InvalidIndex(offset);
078            return code[offset] & 0xff;
079        }
080    
081        public int getShort(int offset) throws InvalidIndex {
082            if (offset < 0 || offset + 1 >= code.length)
083                throw new InvalidIndex(offset);
084            return (code[offset] << 8) | (code[offset + 1] & 0xFF);
085        }
086    
087        public int getUnsignedShort(int offset) throws InvalidIndex {
088            if (offset < 0 || offset + 1 >= code.length)
089                throw new InvalidIndex(offset);
090            return ((code[offset] << 8) | (code[offset + 1] & 0xFF)) & 0xFFFF;
091        }
092    
093        public int getInt(int offset) throws InvalidIndex {
094            if (offset < 0 || offset + 3 >= code.length)
095                throw new InvalidIndex(offset);
096            return (getShort(offset) << 16) | (getShort(offset + 2) & 0xFFFF);
097        }
098    
099        public <R, D> R accept(Visitor<R, D> visitor, D data) {
100            return visitor.visitCode(this, data);
101        }
102    
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;
110    
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            }
118    
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    }