jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/CodeException.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
     1 /*
     1 /*
     2  * reserved comment block
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT REMOVE OR ALTER!
       
     4  */
     3  */
     5 /*
     4 /*
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     7  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     8  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    19  * limitations under the License.
    18  * limitations under the License.
    20  */
    19  */
    21 
    20 
    22 package com.sun.org.apache.bcel.internal.classfile;
    21 package com.sun.org.apache.bcel.internal.classfile;
    23 
    22 
    24 
    23 import java.io.DataInput;
    25 import  com.sun.org.apache.bcel.internal.Constants;
    24 import java.io.DataOutputStream;
    26 import  java.io.*;
    25 import java.io.IOException;
       
    26 
       
    27 import com.sun.org.apache.bcel.internal.Const;
    27 
    28 
    28 /**
    29 /**
    29  * This class represents an entry in the exception table of the <em>Code</em>
    30  * This class represents an entry in the exception table of the <em>Code</em>
    30  * attribute and is used only there. It contains a range in which a
    31  * attribute and is used only there. It contains a range in which a
    31  * particular exception handler is active.
    32  * particular exception handler is active.
    32  *
    33  *
    33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    34  * @version $Id: CodeException.java 1749603 2016-06-21 20:50:19Z ggregory $
    34  * @see     Code
    35  * @see     Code
    35  */
    36  */
    36 public final class CodeException
    37 public final class CodeException implements Cloneable, Node {
    37   implements Cloneable, Constants, Node, Serializable
    38 
    38 {
    39     private int start_pc; // Range in the code the exception handler is
    39   private int start_pc;   // Range in the code the exception handler is
    40     private int end_pc; // active. start_pc is inclusive, end_pc exclusive
    40   private int end_pc;     // active. start_pc is inclusive, end_pc exclusive
    41     private int handler_pc; /* Starting address of exception handler, i.e.,
    41   private int handler_pc; /* Starting address of exception handler, i.e.,
    42      * an offset from start of code.
    42                            * an offset from start of code.
    43      */
    43                            */
    44     private int catch_type; /* If this is zero the handler catches any
    44   private int catch_type; /* If this is zero the handler catches any
    45      * exception, otherwise it points to the
    45                            * exception, otherwise it points to the
    46      * exception class which is to be caught.
    46                            * exception class which is to be caught.
    47      */
    47                            */
    48 
    48   /**
    49 
    49    * Initialize from another object.
    50     /**
    50    */
    51      * Initialize from another object.
    51   public CodeException(CodeException c) {
    52      */
    52     this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
    53     public CodeException(final CodeException c) {
    53   }
    54         this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType());
    54 
    55     }
    55   /**
    56 
    56    * Construct object from file stream.
    57 
    57    * @param file Input stream
    58     /**
    58    * @throws IOException
    59      * Construct object from file stream.
    59    */
    60      * @param file Input stream
    60   CodeException(DataInputStream file) throws IOException
    61      * @throws IOException
    61   {
    62      */
    62     this(file.readUnsignedShort(), file.readUnsignedShort(),
    63     CodeException(final DataInput file) throws IOException {
    63          file.readUnsignedShort(), file.readUnsignedShort());
    64         this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), file
    64   }
    65                 .readUnsignedShort());
    65 
    66     }
    66   /**
    67 
    67    * @param start_pc Range in the code the exception handler is active,
    68 
    68    * start_pc is inclusive while
    69     /**
    69    * @param end_pc is exclusive
    70      * @param start_pc Range in the code the exception handler is active,
    70    * @param handler_pc Starting address of exception handler, i.e.,
    71      * start_pc is inclusive while
    71    * an offset from start of code.
    72      * @param end_pc is exclusive
    72    * @param catch_type If zero the handler catches any
    73      * @param handler_pc Starting address of exception handler, i.e.,
    73    * exception, otherwise it points to the exception class which is
    74      * an offset from start of code.
    74    * to be caught.
    75      * @param catch_type If zero the handler catches any
    75    */
    76      * exception, otherwise it points to the exception class which is
    76   public CodeException(int start_pc, int end_pc, int handler_pc,
    77      * to be caught.
    77                        int catch_type)
    78      */
    78   {
    79     public CodeException(final int start_pc, final int end_pc, final int handler_pc, final int catch_type) {
    79     this.start_pc   = start_pc;
    80         this.start_pc = start_pc;
    80     this.end_pc     = end_pc;
    81         this.end_pc = end_pc;
    81     this.handler_pc = handler_pc;
    82         this.handler_pc = handler_pc;
    82     this.catch_type = catch_type;
    83         this.catch_type = catch_type;
    83   }
    84     }
    84 
    85 
    85   /**
    86 
    86    * Called by objects that are traversing the nodes of the tree implicitely
    87     /**
    87    * defined by the contents of a Java class. I.e., the hierarchy of methods,
    88      * Called by objects that are traversing the nodes of the tree implicitely
    88    * fields, attributes, etc. spawns a tree of objects.
    89      * defined by the contents of a Java class. I.e., the hierarchy of methods,
    89    *
    90      * fields, attributes, etc. spawns a tree of objects.
    90    * @param v Visitor object
    91      *
    91    */
    92      * @param v Visitor object
    92   public void accept(Visitor v) {
    93      */
    93     v.visitCodeException(this);
    94     @Override
    94   }
    95     public void accept( final Visitor v ) {
    95   /**
    96         v.visitCodeException(this);
    96    * Dump code exception to file stream in binary format.
    97     }
    97    *
    98 
    98    * @param file Output file stream
    99 
    99    * @throws IOException
   100     /**
   100    */
   101      * Dump code exception to file stream in binary format.
   101   public final void dump(DataOutputStream file) throws IOException
   102      *
   102   {
   103      * @param file Output file stream
   103     file.writeShort(start_pc);
   104      * @throws IOException
   104     file.writeShort(end_pc);
   105      */
   105     file.writeShort(handler_pc);
   106     public final void dump( final DataOutputStream file ) throws IOException {
   106     file.writeShort(catch_type);
   107         file.writeShort(start_pc);
   107   }
   108         file.writeShort(end_pc);
   108 
   109         file.writeShort(handler_pc);
   109   /**
   110         file.writeShort(catch_type);
   110    * @return 0, if the handler catches any exception, otherwise it points to
   111     }
   111    * the exception class which is to be caught.
   112 
   112    */
   113 
   113   public final int getCatchType() { return catch_type; }
   114     /**
   114 
   115      * @return 0, if the handler catches any exception, otherwise it points to
   115   /**
   116      * the exception class which is to be caught.
   116    * @return Exclusive end index of the region where the handler is active.
   117      */
   117    */
   118     public final int getCatchType() {
   118   public final int getEndPC() { return end_pc; }
   119         return catch_type;
   119 
   120     }
   120   /**
   121 
   121    * @return Starting address of exception handler, relative to the code.
   122 
   122    */
   123     /**
   123   public final int getHandlerPC() { return handler_pc; }
   124      * @return Exclusive end index of the region where the handler is active.
   124 
   125      */
   125   /**
   126     public final int getEndPC() {
   126    * @return Inclusive start index of the region where the handler is active.
   127         return end_pc;
   127    */
   128     }
   128   public final int getStartPC() { return start_pc; }
   129 
   129 
   130 
   130   /**
   131     /**
   131    * @param catch_type.
   132      * @return Starting address of exception handler, relative to the code.
   132    */
   133      */
   133   public final void setCatchType(int catch_type) {
   134     public final int getHandlerPC() {
   134     this.catch_type = catch_type;
   135         return handler_pc;
   135   }
   136     }
   136 
   137 
   137   /**
   138 
   138    * @param end_pc end of handled block
   139     /**
   139    */
   140      * @return Inclusive start index of the region where the handler is active.
   140   public final void setEndPC(int end_pc) {
   141      */
   141     this.end_pc = end_pc;
   142     public final int getStartPC() {
   142   }
   143         return start_pc;
   143 
   144     }
   144   /**
   145 
   145    * @param handler_pc where the actual code is
   146 
   146    */
   147     /**
   147   public final void setHandlerPC(int handler_pc) {
   148      * @param catch_type the type of exception that is caught
   148     this.handler_pc = handler_pc;
   149      */
   149   }
   150     public final void setCatchType( final int catch_type ) {
   150 
   151         this.catch_type = catch_type;
   151   /**
   152     }
   152    * @param start_pc start of handled block
   153 
   153    */
   154 
   154   public final void setStartPC(int start_pc) {
   155     /**
   155     this.start_pc = start_pc;
   156      * @param end_pc end of handled block
   156   }
   157      */
   157 
   158     public final void setEndPC( final int end_pc ) {
   158   /**
   159         this.end_pc = end_pc;
   159    * @return String representation.
   160     }
   160    */
   161 
   161   public final String toString() {
   162 
   162     return "CodeException(start_pc = " + start_pc +
   163     /**
   163       ", end_pc = " + end_pc +
   164      * @param handler_pc where the actual code is
   164       ", handler_pc = " + handler_pc + ", catch_type = " + catch_type + ")";
   165      */
   165   }
   166     public final void setHandlerPC( final int handler_pc ) { // TODO unused
   166 
   167         this.handler_pc = handler_pc;
   167   /**
   168     }
   168    * @return String representation.
   169 
   169    */
   170 
   170   public final String toString(ConstantPool cp, boolean verbose) {
   171     /**
   171     String str;
   172      * @param start_pc start of handled block
   172 
   173      */
   173     if(catch_type == 0)
   174     public final void setStartPC( final int start_pc ) { // TODO unused
   174       str = "<Any exception>(0)";
   175         this.start_pc = start_pc;
   175     else
   176     }
   176       str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false) +
   177 
   177         (verbose? "(" + catch_type + ")" : "");
   178 
   178 
   179     /**
   179     return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
   180      * @return String representation.
   180   }
   181      */
   181 
   182     @Override
   182   public final String toString(ConstantPool cp) {
   183     public final String toString() {
   183     return toString(cp, true);
   184         return "CodeException(start_pc = " + start_pc + ", end_pc = " + end_pc + ", handler_pc = "
   184   }
   185                 + handler_pc + ", catch_type = " + catch_type + ")";
   185 
   186     }
   186   /**
   187 
   187    * @return deep copy of this object
   188 
   188    */
   189     /**
   189   public CodeException copy() {
   190      * @return String representation.
   190     try {
   191      */
   191       return (CodeException)clone();
   192     public final String toString( final ConstantPool cp, final boolean verbose ) {
   192     } catch(CloneNotSupportedException e) {}
   193         String str;
   193 
   194         if (catch_type == 0) {
   194     return null;
   195             str = "<Any exception>(0)";
   195   }
   196         } else {
       
   197             str = Utility.compactClassName(cp.getConstantString(catch_type, Const.CONSTANT_Class), false)
       
   198                     + (verbose ? "(" + catch_type + ")" : "");
       
   199         }
       
   200         return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str;
       
   201     }
       
   202 
       
   203 
       
   204     public final String toString( final ConstantPool cp ) {
       
   205         return toString(cp, true);
       
   206     }
       
   207 
       
   208 
       
   209     /**
       
   210      * @return deep copy of this object
       
   211      */
       
   212     public CodeException copy() {
       
   213         try {
       
   214             return (CodeException) clone();
       
   215         } catch (final CloneNotSupportedException e) {
       
   216             // TODO should this throw?
       
   217         }
       
   218         return null;
       
   219     }
   196 }
   220 }