001: /*
002: * Copyright 1995-2003 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 sun.tools.java;
027:
028: import java.io.*;
029:
030: /**
031: * WARNING: The contents of this source file are not part of any
032: * supported API. Code that depends on them does so at its own risk:
033: * they are subject to change or removal without notice.
034: */
035: public class BinaryCode implements Constants {
036: int maxStack; // maximum stack used by code
037: int maxLocals; // maximum locals used by code
038: BinaryExceptionHandler exceptionHandlers[];
039: BinaryAttribute atts; // code attributes
040: BinaryConstantPool cpool; // constant pool of the class
041: byte code[]; // the byte code
042:
043: /**
044: * Construct the binary code from the code attribute
045: */
046:
047: public BinaryCode(byte data[], BinaryConstantPool cpool,
048: Environment env) {
049: DataInputStream in = new DataInputStream(
050: new ByteArrayInputStream(data));
051: try {
052: this .cpool = cpool;
053: // JVM 4.7.4 CodeAttribute.max_stack
054: this .maxStack = in.readUnsignedShort();
055: // JVM 4.7.4 CodeAttribute.max_locals
056: this .maxLocals = in.readUnsignedShort();
057: // JVM 4.7.4 CodeAttribute.code_length
058: int code_length = in.readInt();
059: this .code = new byte[code_length];
060: // JVM 4.7.4 CodeAttribute.code[]
061: in.read(this .code);
062: // JVM 4.7.4 CodeAttribute.exception_table_length
063: int exception_count = in.readUnsignedShort();
064: this .exceptionHandlers = new BinaryExceptionHandler[exception_count];
065: for (int i = 0; i < exception_count; i++) {
066: // JVM 4.7.4 CodeAttribute.exception_table.start_pc
067: int start = in.readUnsignedShort();
068: // JVM 4.7.4 CodeAttribute.exception_table.end_pc
069: int end = in.readUnsignedShort();
070: // JVM 4.7.4 CodeAttribute.exception_table.handler_pc
071: int handler = in.readUnsignedShort();
072: // JVM 4.7.4 CodeAttribute.exception_table.catch_type
073: ClassDeclaration xclass = cpool.getDeclaration(env, in
074: .readUnsignedShort());
075: this .exceptionHandlers[i] = new BinaryExceptionHandler(
076: start, end, handler, xclass);
077: }
078: this .atts = BinaryAttribute.load(in, cpool, ~0);
079: if (in.available() != 0) {
080: System.err
081: .println("Should have exhausted input stream!");
082: }
083: } catch (IOException e) {
084: throw new CompilerError(e);
085: }
086: }
087:
088: /**
089: * Accessors
090: */
091:
092: public BinaryExceptionHandler getExceptionHandlers()[] {
093: return exceptionHandlers;
094: }
095:
096: public byte getCode()[] {
097: return code;
098: }
099:
100: public int getMaxStack() {
101: return maxStack;
102: }
103:
104: public int getMaxLocals() {
105: return maxLocals;
106: }
107:
108: public BinaryAttribute getAttributes() {
109: return atts;
110: }
111:
112: /**
113: * Load a binary class
114: */
115: public static BinaryCode load(BinaryMember bf,
116: BinaryConstantPool cpool, Environment env) {
117: byte code[] = bf.getAttribute(idCode);
118: return (code != null) ? new BinaryCode(code, cpool, env) : null;
119: }
120: }
|