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

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
parent 255
07da2ffbb76b
child 554
9d9f26857129
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

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

mercurial