286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.classfile;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/Aimport java.io.*;
286N/A
286N/A/**
286N/A * This class represents a chunk of Java byte code contained in a
286N/A * method. It is instantiated by the
286N/A * <em>Attribute.readAttribute()</em> method. A <em>Code</em>
286N/A * attribute contains informations about operand stack, local
286N/A * variables, byte code and the exceptions handled within this
286N/A * method.
286N/A *
286N/A * This attribute has attributes itself, namely <em>LineNumberTable</em> which
286N/A * is used for debugging purposes and <em>LocalVariableTable</em> which
286N/A * contains information about the local variables.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A * @see Attribute
286N/A * @see CodeException
286N/A * @see LineNumberTable
286N/A * @see LocalVariableTable
286N/A */
286N/Apublic final class Code extends Attribute {
286N/A private int max_stack; // Maximum size of stack used by this method
286N/A private int max_locals; // Number of local variables
286N/A private int code_length; // Length of code in bytes
286N/A private byte[] code; // Actual byte code
286N/A
286N/A private int exception_table_length;
286N/A private CodeException[] exception_table; // Table of handled exceptions
286N/A private int attributes_count; // Attributes of code: LineNumber
286N/A private Attribute[] attributes; // or LocalVariable
286N/A
286N/A /**
286N/A * Initialize from another object. Note that both objects use the same
286N/A * references (shallow copy). Use copy() for a physical copy.
286N/A */
286N/A public Code(Code c) {
286N/A this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(),
286N/A c.getCode(), c.getExceptionTable(), c.getAttributes(),
286N/A c.getConstantPool());
286N/A }
286N/A
286N/A /**
286N/A * @param name_index Index pointing to the name <em>Code</em>
286N/A * @param length Content length in bytes
286N/A * @param file Input stream
286N/A * @param constant_pool Array of constants
286N/A */
286N/A Code(int name_index, int length, DataInputStream file,
286N/A ConstantPool constant_pool) throws IOException
286N/A {
286N/A // Initialize with some default values which will be overwritten later
286N/A this(name_index, length,
286N/A file.readUnsignedShort(), file.readUnsignedShort(),
286N/A (byte[])null, (CodeException[])null, (Attribute[])null,
286N/A constant_pool);
286N/A
286N/A code_length = file.readInt();
286N/A code = new byte[code_length]; // Read byte code
286N/A file.readFully(code);
286N/A
286N/A /* Read exception table that contains all regions where an exception
286N/A * handler is active, i.e., a try { ... } catch() block.
286N/A */
286N/A exception_table_length = file.readUnsignedShort();
286N/A exception_table = new CodeException[exception_table_length];
286N/A
286N/A for(int i=0; i < exception_table_length; i++)
286N/A exception_table[i] = new CodeException(file);
286N/A
286N/A /* Read all attributes, currently `LineNumberTable' and
286N/A * `LocalVariableTable'
286N/A */
286N/A attributes_count = file.readUnsignedShort();
286N/A attributes = new Attribute[attributes_count];
286N/A for(int i=0; i < attributes_count; i++)
286N/A attributes[i] = Attribute.readAttribute(file, constant_pool);
286N/A
286N/A /* Adjust length, because of setAttributes in this(), s.b. length
286N/A * is incorrect, because it didn't take the internal attributes
286N/A * into account yet! Very subtle bug, fixed in 3.1.1.
286N/A */
286N/A this.length = length;
286N/A }
286N/A
286N/A /**
286N/A * @param name_index Index pointing to the name <em>Code</em>
286N/A * @param length Content length in bytes
286N/A * @param max_stack Maximum size of stack
286N/A * @param max_locals Number of local variables
286N/A * @param code Actual byte code
286N/A * @param exception_table Table of handled exceptions
286N/A * @param attributes Attributes of code: LineNumber or LocalVariable
286N/A * @param constant_pool Array of constants
286N/A */
286N/A public Code(int name_index, int length,
286N/A int max_stack, int max_locals,
286N/A byte[] code,
286N/A CodeException[] exception_table,
286N/A Attribute[] attributes,
286N/A ConstantPool constant_pool)
286N/A {
286N/A super(Constants.ATTR_CODE, name_index, length, constant_pool);
286N/A
286N/A this.max_stack = max_stack;
286N/A this.max_locals = max_locals;
286N/A
286N/A setCode(code);
286N/A setExceptionTable(exception_table);
286N/A setAttributes(attributes); // Overwrites length!
286N/A }
286N/A
286N/A /**
286N/A * Called by objects that are traversing the nodes of the tree implicitely
286N/A * defined by the contents of a Java class. I.e., the hierarchy of methods,
286N/A * fields, attributes, etc. spawns a tree of objects.
286N/A *
286N/A * @param v Visitor object
286N/A */
286N/A public void accept(Visitor v) {
286N/A v.visitCode(this);
286N/A }
286N/A
286N/A /**
286N/A * Dump code attribute to file stream in binary format.
286N/A *
286N/A * @param file Output file stream
286N/A * @throws IOException
286N/A */
286N/A public final void dump(DataOutputStream file) throws IOException
286N/A {
286N/A super.dump(file);
286N/A
286N/A file.writeShort(max_stack);
286N/A file.writeShort(max_locals);
286N/A file.writeInt(code_length);
286N/A file.write(code, 0, code_length);
286N/A
286N/A file.writeShort(exception_table_length);
286N/A for(int i=0; i < exception_table_length; i++)
286N/A exception_table[i].dump(file);
286N/A
286N/A file.writeShort(attributes_count);
286N/A for(int i=0; i < attributes_count; i++)
286N/A attributes[i].dump(file);
286N/A }
286N/A
286N/A /**
286N/A * @return Collection of code attributes.
286N/A * @see Attribute
286N/A */
286N/A public final Attribute[] getAttributes() { return attributes; }
286N/A
286N/A /**
286N/A * @return LineNumberTable of Code, if it has one
286N/A */
286N/A public LineNumberTable getLineNumberTable() {
286N/A for(int i=0; i < attributes_count; i++)
286N/A if(attributes[i] instanceof LineNumberTable)
286N/A return (LineNumberTable)attributes[i];
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * @return LocalVariableTable of Code, if it has one
286N/A */
286N/A public LocalVariableTable getLocalVariableTable() {
286N/A for(int i=0; i < attributes_count; i++)
286N/A if(attributes[i] instanceof LocalVariableTable)
286N/A return (LocalVariableTable)attributes[i];
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * @return Actual byte code of the method.
286N/A */
286N/A public final byte[] getCode() { return code; }
286N/A
286N/A /**
286N/A * @return Table of handled exceptions.
286N/A * @see CodeException
286N/A */
286N/A public final CodeException[] getExceptionTable() { return exception_table; }
286N/A
286N/A /**
286N/A * @return Number of local variables.
286N/A */
286N/A public final int getMaxLocals() { return max_locals; }
286N/A
286N/A /**
286N/A * @return Maximum size of stack used by this method.
286N/A */
286N/A
286N/A public final int getMaxStack() { return max_stack; }
286N/A
286N/A /**
286N/A * @return the internal length of this code attribute (minus the first 6 bytes)
286N/A * and excluding all its attributes
286N/A */
286N/A private final int getInternalLength() {
286N/A return 2 /*max_stack*/ + 2 /*max_locals*/ + 4 /*code length*/
286N/A + code_length /*byte-code*/
286N/A + 2 /*exception-table length*/
286N/A + 8 * exception_table_length /* exception table */
286N/A + 2 /* attributes count */;
286N/A }
286N/A
286N/A /**
286N/A * @return the full size of this code attribute, minus its first 6 bytes,
286N/A * including the size of all its contained attributes
286N/A */
286N/A private final int calculateLength() {
286N/A int len = 0;
286N/A
286N/A for(int i=0; i < attributes_count; i++)
286N/A len += attributes[i].length + 6 /*attribute header size*/;
286N/A
286N/A return len + getInternalLength();
286N/A }
286N/A
286N/A /**
286N/A * @param attributes.
286N/A */
286N/A public final void setAttributes(Attribute[] attributes) {
286N/A this.attributes = attributes;
286N/A attributes_count = (attributes == null)? 0 : attributes.length;
286N/A length = calculateLength(); // Adjust length
286N/A }
286N/A
286N/A /**
286N/A * @param code byte code
286N/A */
286N/A public final void setCode(byte[] code) {
286N/A this.code = code;
286N/A code_length = (code == null)? 0 : code.length;
286N/A }
286N/A
286N/A /**
286N/A * @param exception_table exception table
286N/A */
286N/A public final void setExceptionTable(CodeException[] exception_table) {
286N/A this.exception_table = exception_table;
286N/A exception_table_length = (exception_table == null)? 0 :
286N/A exception_table.length;
286N/A }
286N/A
286N/A /**
286N/A * @param max_locals maximum number of local variables
286N/A */
286N/A public final void setMaxLocals(int max_locals) {
286N/A this.max_locals = max_locals;
286N/A }
286N/A
286N/A /**
286N/A * @param max_stack maximum stack size
286N/A */
286N/A public final void setMaxStack(int max_stack) {
286N/A this.max_stack = max_stack;
286N/A }
286N/A
286N/A /**
286N/A * @return String representation of code chunk.
286N/A */
286N/A public final String toString(boolean verbose) {
286N/A StringBuffer buf;
286N/A
286N/A buf = new StringBuffer("Code(max_stack = " + max_stack +
286N/A ", max_locals = " + max_locals +
286N/A ", code_length = " + code_length + ")\n" +
286N/A Utility.codeToString(code, constant_pool, 0, -1, verbose));
286N/A
286N/A if(exception_table_length > 0) {
286N/A buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n");
286N/A
286N/A for(int i=0; i < exception_table_length; i++)
286N/A buf.append(exception_table[i].toString(constant_pool, verbose) + "\n");
286N/A }
286N/A
286N/A if(attributes_count > 0) {
286N/A buf.append("\nAttribute(s) = \n");
286N/A
286N/A for(int i=0; i < attributes_count; i++)
286N/A buf.append(attributes[i].toString() + "\n");
286N/A }
286N/A
286N/A return buf.toString();
286N/A }
286N/A
286N/A /**
286N/A * @return String representation of code chunk.
286N/A */
286N/A public final String toString() {
286N/A return toString(true);
286N/A }
286N/A
286N/A /**
286N/A * @return deep copy of this attribute
286N/A */
286N/A public Attribute copy(ConstantPool constant_pool) {
286N/A Code c = (Code)clone();
286N/A c.code = (byte[])code.clone();
286N/A c.constant_pool = constant_pool;
286N/A
286N/A c.exception_table = new CodeException[exception_table_length];
286N/A for(int i=0; i < exception_table_length; i++)
286N/A c.exception_table[i] = exception_table[i].copy();
286N/A
286N/A c.attributes = new Attribute[attributes_count];
286N/A for(int i=0; i < attributes_count; i++)
286N/A c.attributes[i] = attributes[i].copy(constant_pool);
286N/A
286N/A return c;
286N/A }
286N/A}