001: /*
002: * ExceptionTableEntry.java
003: *
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
005: *
006: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common
010: * Development and Distribution License("CDDL") (collectively, the
011: * "License"). You may not use this file except in compliance with the
012: * License. You can obtain a copy of the License at
013: * http://www.netbeans.org/cddl-gplv2.html
014: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
015: * specific language governing permissions and limitations under the
016: * License. When distributing the software, include this License Header
017: * Notice in each file and include the License file at
018: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
019: * particular file as subject to the "Classpath" exception as provided
020: * by Sun in the GPL Version 2 section of the License file that
021: * accompanied this code. If applicable, add the following below the
022: * License Header, with the fields enclosed by brackets [] replaced by
023: * your own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Contributor(s):
027: *
028: * The Original Software is NetBeans. The Initial Developer of the Original
029: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: *
032: * If you wish your version of this file to be governed by only the CDDL
033: * or only the GPL Version 2, indicate your decision by adding
034: * "[Contributor] elects to include this software in this distribution
035: * under the [CDDL or GPL Version 2] license." If you do not indicate a
036: * single choice of license, a recipient has the option to distribute
037: * your version of this file under either the CDDL, the GPL Version 2 or
038: * to extend the choice of license to its licensees as provided above.
039: * However, if you add GPL Version 2 code and therefore, elected the GPL
040: * Version 2 license, then the option applies only if the new code is
041: * made subject to such option by the copyright holder.
042: *
043: * Contributor(s): Thomas Ball
044: *
045: * Version: $Revision$
046: */
047:
048: package org.netbeans.modules.classfile;
049:
050: import java.io.DataInputStream;
051: import java.io.IOException;
052:
053: /**
054: * An entry in the exception table of a method's code attribute.
055: *
056: * @author Thomas Ball
057: */
058: public final class ExceptionTableEntry {
059:
060: int startPC;
061: int endPC;
062: int handlerPC;
063: CPClassInfo catchType; // may be null for "finally" exception handler
064:
065: static ExceptionTableEntry[] loadExceptionTable(DataInputStream in,
066: ConstantPool pool) throws IOException {
067: int n = in.readUnsignedShort();
068: ExceptionTableEntry[] exceptions = new ExceptionTableEntry[n];
069: for (int i = 0; i < n; i++)
070: exceptions[i] = new ExceptionTableEntry(in, pool);
071: return exceptions;
072: }
073:
074: /** Creates new ExceptionTableEntry */
075: ExceptionTableEntry(DataInputStream in, ConstantPool pool)
076: throws IOException {
077: loadExceptionEntry(in, pool);
078: }
079:
080: private void loadExceptionEntry(DataInputStream in,
081: ConstantPool pool) throws IOException {
082: startPC = in.readUnsignedShort();
083: endPC = in.readUnsignedShort();
084: handlerPC = in.readUnsignedShort();
085: int typeIndex = in.readUnsignedShort();
086: if (typeIndex != 0) // may be 0 for "finally" exception handler
087: try {
088: catchType = pool.getClass(typeIndex);
089: } catch (IndexOutOfBoundsException e) {
090: throw new InvalidClassFileAttributeException(
091: "invalid catchType (" + typeIndex
092: + ") in exception table entry", e);
093: }
094: }
095:
096: /**
097: * Returns the beginning offset into the method's bytecodes of this
098: * exception handler.
099: */
100: public final int getStartPC() {
101: return startPC;
102: }
103:
104: /**
105: * Returns the ending offset into the method's bytecodes of this
106: * exception handler, or the length of the bytecode array if the
107: * handler supports the method's last bytecodes (JVM 4.7.3).
108: */
109: public final int getEndPC() {
110: return endPC;
111: }
112:
113: /**
114: * Returns the starting offset into the method's bytecodes of the
115: * exception handling code.
116: */
117: public final int getHandlerPC() {
118: return handlerPC;
119: }
120:
121: /**
122: * Returns the type of exception handler, or <code>null</code>
123: * if this handler catches all exceptions, such as an exception
124: * handler for a "<code>finally</code>" clause (JVM 4.7.3).
125: */
126: public final CPClassInfo getCatchType() {
127: return catchType;
128: }
129: }
|