CodeException.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.Constants;
  23. import org.apache.bcel.util.Args;

  24. /**
  25.  * This class represents an entry in the exception table of the <em>Code</em> attribute and is used only there. It
  26.  * contains a range in which a particular exception handler is active.
  27.  *
  28.  * <pre>
  29.  * Code_attribute {
  30.  *   u2 attribute_name_index;
  31.  *   u4 attribute_length;
  32.  *   u2 max_stack;
  33.  *   u2 max_locals;
  34.  *   u4 code_length;
  35.  *   u1 code[code_length];
  36.  *   u2 exception_table_length;
  37.  *   {
  38.  *     u2 start_pc;
  39.  *     u2 end_pc;
  40.  *     u2 handler_pc;
  41.  *     u2 catch_type;
  42.  *   } exception_table[exception_table_length];
  43.  *   u2 attributes_count;
  44.  *   attribute_info attributes[attributes_count];
  45.  * }
  46.  * </pre>
  47.  *
  48.  * @see Code
  49.  */
  50. public final class CodeException implements Cloneable, Node, Constants {

  51.     /**
  52.      * Empty array.
  53.      */
  54.     static final CodeException[] EMPTY_ARRAY = {};

  55.     /** Range in the code the exception handler. */
  56.     private int startPc;

  57.     /** Active. startPc is inclusive, endPc exclusive. */
  58.     private int endPc;

  59.     /**
  60.      * Starting address of exception handler, i.e., an offset from start of code.
  61.      */
  62.     private int handlerPc;

  63.     /*
  64.      * If this is zero the handler catches any exception, otherwise it points to the exception class which is to be caught.
  65.      */
  66.     private int catchType;

  67.     /**
  68.      * Constructs a new instance from another instance.
  69.      *
  70.      * @param c Source for copying.
  71.      */
  72.     public CodeException(final CodeException c) {
  73.         this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
  74.     }

  75.     /**
  76.      * Constructs a new instance from a DataInput.
  77.      *
  78.      * @param file Input stream
  79.      * @throws IOException if an I/O error occurs.
  80.      */
  81.     CodeException(final DataInput file) throws IOException {
  82.         this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort());
  83.     }

  84.     /**
  85.      * Constructs a new instance.
  86.      *
  87.      * @param startPc Range in the code the exception handler is active, startPc is inclusive while
  88.      * @param endPc is exclusive
  89.      * @param handlerPc Starting address of exception handler, i.e., an offset from start of code.
  90.      * @param catchType If zero the handler catches any exception, otherwise it points to the exception class which is to be
  91.      *        caught.
  92.      */
  93.     public CodeException(final int startPc, final int endPc, final int handlerPc, final int catchType) {
  94.         this.startPc = Args.requireU2(startPc, "startPc");
  95.         this.endPc = Args.requireU2(endPc, "endPc");
  96.         this.handlerPc = Args.requireU2(handlerPc, "handlerPc");
  97.         this.catchType = Args.requireU2(catchType, "catchType");
  98.     }

  99.     /**
  100.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  101.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  102.      *
  103.      * @param v Visitor object
  104.      */
  105.     @Override
  106.     public void accept(final Visitor v) {
  107.         v.visitCodeException(this);
  108.     }

  109.     /**
  110.      * @return deep copy of this object
  111.      */
  112.     public CodeException copy() {
  113.         try {
  114.             return (CodeException) clone();
  115.         } catch (final CloneNotSupportedException e) {
  116.             // TODO should this throw?
  117.         }
  118.         return null;
  119.     }

  120.     /**
  121.      * Dumps code exception to file stream in binary format.
  122.      *
  123.      * @param file Output file stream
  124.      * @throws IOException if an I/O error occurs.
  125.      */
  126.     public void dump(final DataOutputStream file) throws IOException {
  127.         file.writeShort(startPc);
  128.         file.writeShort(endPc);
  129.         file.writeShort(handlerPc);
  130.         file.writeShort(catchType);
  131.     }

  132.     /**
  133.      * @return 0, if the handler catches any exception, otherwise it points to the exception class which is to be caught.
  134.      */
  135.     public int getCatchType() {
  136.         return catchType;
  137.     }

  138.     /**
  139.      * @return Exclusive end index of the region where the handler is active.
  140.      */
  141.     public int getEndPC() {
  142.         return endPc;
  143.     }

  144.     /**
  145.      * @return Starting address of exception handler, relative to the code.
  146.      */
  147.     public int getHandlerPC() {
  148.         return handlerPc;
  149.     }

  150.     /**
  151.      * @return Inclusive start index of the region where the handler is active.
  152.      */
  153.     public int getStartPC() {
  154.         return startPc;
  155.     }

  156.     /**
  157.      * @param catchType the type of exception that is caught
  158.      */
  159.     public void setCatchType(final int catchType) {
  160.         this.catchType = catchType;
  161.     }

  162.     /**
  163.      * @param endPc end of handled block
  164.      */
  165.     public void setEndPC(final int endPc) {
  166.         this.endPc = endPc;
  167.     }

  168.     /**
  169.      * @param handlerPc where the actual code is
  170.      */
  171.     public void setHandlerPC(final int handlerPc) { // TODO unused
  172.         this.handlerPc = handlerPc;
  173.     }

  174.     /**
  175.      * @param startPc start of handled block
  176.      */
  177.     public void setStartPC(final int startPc) { // TODO unused
  178.         this.startPc = startPc;
  179.     }

  180.     /**
  181.      * @return String representation.
  182.      */
  183.     @Override
  184.     public String toString() {
  185.         return "CodeException(startPc = " + startPc + ", endPc = " + endPc + ", handlerPc = " + handlerPc + ", catchType = " + catchType + ")";
  186.     }

  187.     public String toString(final ConstantPool cp) {
  188.         return toString(cp, true);
  189.     }

  190.     /**
  191.      * @param cp constant pool source.
  192.      * @param verbose Output more if true.
  193.      * @return String representation.
  194.      */
  195.     public String toString(final ConstantPool cp, final boolean verbose) {
  196.         String str;
  197.         if (catchType == 0) {
  198.             str = "<Any exception>(0)";
  199.         } else {
  200.             str = Utility.compactClassName(cp.getConstantString(catchType, Const.CONSTANT_Class), false) + (verbose ? "(" + catchType + ")" : "");
  201.         }
  202.         return startPc + "\t" + endPc + "\t" + handlerPc + "\t" + str;
  203.     }
  204. }