jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/LOOKUPSWITCH.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
    19  * limitations under the License.
    19  * limitations under the License.
    20  */
    20  */
    21 
    21 
    22 package com.sun.org.apache.bcel.internal.generic;
    22 package com.sun.org.apache.bcel.internal.generic;
    23 
    23 
    24 import java.io.*;
    24 import java.io.DataOutputStream;
       
    25 import java.io.IOException;
       
    26 
    25 import com.sun.org.apache.bcel.internal.util.ByteSequence;
    27 import com.sun.org.apache.bcel.internal.util.ByteSequence;
    26 
    28 
    27 /**
    29 /**
    28  * LOOKUPSWITCH - Switch with unordered set of values
    30  * LOOKUPSWITCH - Switch with unordered set of values
    29  *
    31  *
    30  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    32  * @version $Id: LOOKUPSWITCH.java 1747278 2016-06-07 17:28:43Z britter $
    31  * @see SWITCH
    33  * @see SWITCH
    32  */
    34  */
    33 public class LOOKUPSWITCH extends Select {
    35 public class LOOKUPSWITCH extends Select {
    34   /**
       
    35    * Empty constructor needed for the Class.newInstance() statement in
       
    36    * Instruction.readInstruction(). Not to be used otherwise.
       
    37    */
       
    38   LOOKUPSWITCH() {}
       
    39 
    36 
    40   public LOOKUPSWITCH(int[] match, InstructionHandle[] targets,
    37     /**
    41                       InstructionHandle target) {
    38      * Empty constructor needed for the Class.newInstance() statement in
    42     super(com.sun.org.apache.bcel.internal.Constants.LOOKUPSWITCH, match, targets, target);
    39      * Instruction.readInstruction(). Not to be used otherwise.
    43 
    40      */
    44     length = (short)(9 + match_length * 8); /* alignment remainder assumed
    41     LOOKUPSWITCH() {
    45                                              * 0 here, until dump time. */
       
    46     fixed_length = length;
       
    47   }
       
    48 
       
    49   /**
       
    50    * Dump instruction as byte code to stream out.
       
    51    * @param out Output stream
       
    52    */
       
    53   public void dump(DataOutputStream out) throws IOException {
       
    54     super.dump(out);
       
    55     out.writeInt(match_length);       // npairs
       
    56 
       
    57     for(int i=0; i < match_length; i++) {
       
    58       out.writeInt(match[i]);         // match-offset pairs
       
    59       out.writeInt(indices[i] = getTargetOffset(targets[i]));
       
    60     }
    42     }
    61   }
       
    62 
       
    63   /**
       
    64    * Read needed data (e.g. index) from file.
       
    65    */
       
    66   protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException
       
    67   {
       
    68     super.initFromFile(bytes, wide); // reads padding
       
    69 
       
    70     match_length = bytes.readInt();
       
    71     fixed_length = (short)(9 + match_length * 8);
       
    72     length       = (short)(fixed_length + padding);
       
    73 
       
    74     match   = new int[match_length];
       
    75     indices = new int[match_length];
       
    76     targets = new InstructionHandle[match_length];
       
    77 
       
    78     for(int i=0; i < match_length; i++) {
       
    79       match[i]   = bytes.readInt();
       
    80       indices[i] = bytes.readInt();
       
    81     }
       
    82   }
       
    83 
    43 
    84 
    44 
    85   /**
    45     public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets,
    86    * Call corresponding visitor method(s). The order is:
    46             final InstructionHandle defaultTarget) {
    87    * Call visitor methods of implemented interfaces first, then
    47         super(com.sun.org.apache.bcel.internal.Const.LOOKUPSWITCH, match, targets, defaultTarget);
    88    * call methods according to the class hierarchy in descending order,
    48         /* alignment remainder assumed 0 here, until dump time. */
    89    * i.e., the most specific visitXXX() call comes last.
    49         final short _length = (short) (9 + getMatch_length() * 8);
    90    *
    50         super.setLength(_length);
    91    * @param v Visitor object
    51         setFixed_length(_length);
    92    */
    52     }
    93   public void accept(Visitor v) {
    53 
    94     v.visitVariableLengthInstruction(this);
    54 
    95     v.visitStackProducer(this);
    55     /**
    96     v.visitBranchInstruction(this);
    56      * Dump instruction as byte code to stream out.
    97     v.visitSelect(this);
    57      * @param out Output stream
    98     v.visitLOOKUPSWITCH(this);
    58      */
    99   }
    59     @Override
       
    60     public void dump( final DataOutputStream out ) throws IOException {
       
    61         super.dump(out);
       
    62         final int _match_length = getMatch_length();
       
    63         out.writeInt(_match_length); // npairs
       
    64         for (int i = 0; i < _match_length; i++) {
       
    65             out.writeInt(super.getMatch(i)); // match-offset pairs
       
    66             out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
       
    67         }
       
    68     }
       
    69 
       
    70 
       
    71     /**
       
    72      * Read needed data (e.g. index) from file.
       
    73      */
       
    74     @Override
       
    75     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
       
    76         super.initFromFile(bytes, wide); // reads padding
       
    77         final int _match_length = bytes.readInt();
       
    78         setMatch_length(_match_length);
       
    79         final short _fixed_length = (short) (9 + _match_length * 8);
       
    80         setFixed_length(_fixed_length);
       
    81         final short _length = (short) (_match_length + super.getPadding());
       
    82         super.setLength(_length);
       
    83         super.setMatches(new int[_match_length]);
       
    84         super.setIndices(new int[_match_length]);
       
    85         super.setTargets(new InstructionHandle[_match_length]);
       
    86         for (int i = 0; i < _match_length; i++) {
       
    87             super.setMatch(i, bytes.readInt());
       
    88             super.setIndices(i, bytes.readInt());
       
    89         }
       
    90     }
       
    91 
       
    92 
       
    93     /**
       
    94      * Call corresponding visitor method(s). The order is:
       
    95      * Call visitor methods of implemented interfaces first, then
       
    96      * call methods according to the class hierarchy in descending order,
       
    97      * i.e., the most specific visitXXX() call comes last.
       
    98      *
       
    99      * @param v Visitor object
       
   100      */
       
   101     @Override
       
   102     public void accept( final Visitor v ) {
       
   103         v.visitVariableLengthInstruction(this);
       
   104         v.visitStackConsumer(this);
       
   105         v.visitBranchInstruction(this);
       
   106         v.visitSelect(this);
       
   107         v.visitLOOKUPSWITCH(this);
       
   108     }
   100 }
   109 }