Select.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.generic;

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

  20. import org.apache.bcel.util.ByteSequence;

  21. /**
  22.  * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions.
  23.  *
  24.  * <p>
  25.  * We use our super's {@code target} property as the default target.
  26.  *
  27.  * @see LOOKUPSWITCH
  28.  * @see TABLESWITCH
  29.  * @see InstructionList
  30.  */
  31. public abstract class Select extends BranchInstruction implements VariableLengthInstruction, StackConsumer /* @since 6.0 */, StackProducer {

  32.     /**
  33.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  34.      */
  35.     @Deprecated
  36.     protected int[] match; // matches, i.e., case 1: ... TODO could be package-protected?

  37.     /**
  38.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  39.      */
  40.     @Deprecated
  41.     protected int[] indices; // target offsets TODO could be package-protected?

  42.     /**
  43.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  44.      */
  45.     @Deprecated
  46.     protected InstructionHandle[] targets; // target objects in instruction list TODO could be package-protected?

  47.     /**
  48.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  49.      */
  50.     @Deprecated
  51.     protected int fixed_length; // fixed length defined by subclasses TODO could be package-protected?

  52.     /**
  53.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  54.      */
  55.     @Deprecated
  56.     protected int match_length; // number of cases TODO could be package-protected?

  57.     /**
  58.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  59.      */
  60.     @Deprecated
  61.     protected int padding; // number of pad bytes for alignment TODO could be package-protected?

  62.     /**
  63.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  64.      */
  65.     Select() {
  66.     }

  67.     /**
  68.      * (Match, target) pairs for switch. 'Match' and 'targets' must have the same length of course.
  69.      *
  70.      * @param match array of matching values
  71.      * @param targets instruction targets
  72.      * @param defaultTarget default instruction target
  73.      */
  74.     Select(final short opcode, final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
  75.         // don't set default target before instruction is built
  76.         super(opcode, null);
  77.         this.match = match;
  78.         this.targets = targets;
  79.         // now it's safe to set default target
  80.         setTarget(defaultTarget);
  81.         for (final InstructionHandle target2 : targets) {
  82.             notifyTarget(null, target2, this);
  83.         }
  84.         if ((match_length = match.length) != targets.length) {
  85.             throw new ClassGenException("Match and target array have not the same length: Match length: " + match.length + " Target length: " + targets.length);
  86.         }
  87.         indices = new int[match_length];
  88.     }

  89.     @Override
  90.     protected Object clone() throws CloneNotSupportedException {
  91.         final Select copy = (Select) super.clone();
  92.         copy.match = match.clone();
  93.         copy.indices = indices.clone();
  94.         copy.targets = targets.clone();
  95.         return copy;
  96.     }

  97.     /**
  98.      * @return true, if ih is target of this instruction
  99.      */
  100.     @Override
  101.     public boolean containsTarget(final InstructionHandle ih) {
  102.         if (super.getTarget() == ih) {
  103.             return true;
  104.         }
  105.         for (final InstructionHandle target2 : targets) {
  106.             if (target2 == ih) {
  107.                 return true;
  108.             }
  109.         }
  110.         return false;
  111.     }

  112.     /**
  113.      * Inform targets that they're not targeted anymore.
  114.      */
  115.     @Override
  116.     void dispose() {
  117.         super.dispose();
  118.         for (final InstructionHandle target2 : targets) {
  119.             target2.removeTargeter(this);
  120.         }
  121.     }

  122.     /**
  123.      * Dump instruction as byte code to stream out.
  124.      *
  125.      * @param out Output stream
  126.      */
  127.     @Override
  128.     public void dump(final DataOutputStream out) throws IOException {
  129.         out.writeByte(super.getOpcode());
  130.         for (int i = 0; i < padding; i++) {
  131.             out.writeByte(0);
  132.         }
  133.         super.setIndex(getTargetOffset()); // Write default target offset
  134.         out.writeInt(super.getIndex());
  135.     }

  136.     /**
  137.      * @return the fixed_length
  138.      * @since 6.0
  139.      */
  140.     final int getFixedLength() {
  141.         return fixed_length;
  142.     }

  143.     /**
  144.      * @return array of match target offsets
  145.      */
  146.     public int[] getIndices() {
  147.         return indices;
  148.     }

  149.     /**
  150.      * @return index entry from indices
  151.      * @since 6.0
  152.      */
  153.     final int getIndices(final int index) {
  154.         return indices[index];
  155.     }

  156.     /**
  157.      * @return match entry
  158.      * @since 6.0
  159.      */
  160.     final int getMatch(final int index) {
  161.         return match[index];
  162.     }

  163.     /**
  164.      * @return the match_length
  165.      * @since 6.0
  166.      */
  167.     final int getMatchLength() {
  168.         return match_length;
  169.     }

  170.     /**
  171.      * @return array of match indices
  172.      */
  173.     public int[] getMatchs() {
  174.         return match;
  175.     }

  176.     /**
  177.      *
  178.      * @return the padding
  179.      * @since 6.0
  180.      */
  181.     final int getPadding() {
  182.         return padding;
  183.     }

  184.     /**
  185.      * @return target entry
  186.      * @since 6.0
  187.      */
  188.     final InstructionHandle getTarget(final int index) {
  189.         return targets[index];
  190.     }

  191.     /**
  192.      * @return array of match targets
  193.      */
  194.     public InstructionHandle[] getTargets() {
  195.         return targets;
  196.     }

  197.     /**
  198.      * Read needed data (e.g. index) from file.
  199.      */
  200.     @Override
  201.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  202.         padding = (4 - bytes.getIndex() % 4) % 4; // Compute number of pad bytes
  203.         for (int i = 0; i < padding; i++) {
  204.             bytes.readByte();
  205.         }
  206.         // Default branch target common for both cases (TABLESWITCH, LOOKUPSWITCH)
  207.         super.setIndex(bytes.readInt());
  208.     }

  209.     /**
  210.      * @param fixedLength the fixed_length to set
  211.      * @since 6.0
  212.      */
  213.     final void setFixedLength(final int fixedLength) {
  214.         this.fixed_length = fixedLength;
  215.     }

  216.     /** @since 6.0 */
  217.     final int setIndices(final int i, final int value) {
  218.         indices[i] = value;
  219.         return value; // Allow use in nested calls
  220.     }

  221.     /**
  222.      *
  223.      * @param array
  224.      * @since 6.0
  225.      */
  226.     final void setIndices(final int[] array) {
  227.         indices = array;
  228.     }

  229.     /**
  230.      *
  231.      * @param index
  232.      * @param value
  233.      * @since 6.0
  234.      */
  235.     final void setMatch(final int index, final int value) {
  236.         match[index] = value;
  237.     }

  238.     /**
  239.      *
  240.      * @param array
  241.      * @since 6.0
  242.      */
  243.     final void setMatches(final int[] array) {
  244.         match = array;
  245.     }

  246.     /**
  247.      * @param matchLength the match_length to set
  248.      * @since 6.0
  249.      */
  250.     final int setMatchLength(final int matchLength) {
  251.         this.match_length = matchLength;
  252.         return matchLength;
  253.     }

  254.     /**
  255.      * Sets branch target for 'i'th case
  256.      */
  257.     public void setTarget(final int i, final InstructionHandle target) { // TODO could be package-protected?
  258.         notifyTarget(targets[i], target, this);
  259.         targets[i] = target;
  260.     }

  261.     /**
  262.      *
  263.      * @param array
  264.      * @since 6.0
  265.      */
  266.     final void setTargets(final InstructionHandle[] array) {
  267.         targets = array;
  268.     }

  269.     /**
  270.      * @return mnemonic for instruction
  271.      */
  272.     @Override
  273.     public String toString(final boolean verbose) {
  274.         final StringBuilder buf = new StringBuilder(super.toString(verbose));
  275.         if (verbose) {
  276.             for (int i = 0; i < match_length; i++) {
  277.                 String s = "null";
  278.                 if (targets[i] != null) {
  279.                     if (targets[i].getInstruction() == this) {
  280.                         s = "<points to itself>";
  281.                     } else {
  282.                         s = targets[i].getInstruction().toString();
  283.                     }
  284.                 }
  285.                 buf.append("(").append(match[i]).append(", ").append(s).append(" = {").append(indices[i]).append("})");
  286.             }
  287.         } else {
  288.             buf.append(" ...");
  289.         }
  290.         return buf.toString();
  291.     }

  292.     /**
  293.      * Since this is a variable length instruction, it may shift the following instructions which then need to update their
  294.      * position.
  295.      *
  296.      * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable
  297.      * length instructions 'setPositions' performs multiple passes over the instruction list to calculate the correct (byte)
  298.      * positions and offsets by calling this function.
  299.      *
  300.      * @param offset additional offset caused by preceding (variable length) instructions
  301.      * @param maxOffset the maximum offset that may be caused by these instructions
  302.      * @return additional offset caused by possible change of this instruction's length
  303.      */
  304.     @Override
  305.     protected int updatePosition(final int offset, final int maxOffset) {
  306.         setPosition(getPosition() + offset); // Additional offset caused by preceding SWITCHs, GOTOs, etc.
  307.         final short oldLength = (short) super.getLength();
  308.         /*
  309.          * Alignment on 4-byte-boundary, + 1, because of tag byte.
  310.          */
  311.         padding = (4 - (getPosition() + 1) % 4) % 4;
  312.         super.setLength((short) (fixed_length + padding)); // Update length
  313.         return super.getLength() - oldLength;
  314.     }

  315.     /**
  316.      * @param oldIh old target
  317.      * @param newIh new target
  318.      */
  319.     @Override
  320.     public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
  321.         boolean targeted = false;
  322.         if (super.getTarget() == oldIh) {
  323.             targeted = true;
  324.             setTarget(newIh);
  325.         }
  326.         for (int i = 0; i < targets.length; i++) {
  327.             if (targets[i] == oldIh) {
  328.                 targeted = true;
  329.                 setTarget(i, newIh);
  330.             }
  331.         }
  332.         if (!targeted) {
  333.             throw new ClassGenException("Not targeting " + oldIh);
  334.         }
  335.     }
  336. }