BranchInstruction.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.  * Abstract super class for branching instructions like GOTO, IFEQ, etc.. Branch instructions may have a variable
  23.  * length, namely GOTO, JSR, LOOKUPSWITCH and TABLESWITCH.
  24.  *
  25.  * @see InstructionList
  26.  */
  27. public abstract class BranchInstruction extends Instruction implements InstructionTargeter {

  28.     /**
  29.      * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen, LineNumberGen
  30.      */
  31.     static void notifyTarget(final InstructionHandle oldIh, final InstructionHandle newIh, final InstructionTargeter t) {
  32.         if (oldIh != null) {
  33.             oldIh.removeTargeter(t);
  34.         }
  35.         if (newIh != null) {
  36.             newIh.addTargeter(t);
  37.         }
  38.     }

  39.     /**
  40.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  41.      */
  42.     @Deprecated
  43.     protected int index; // Branch target relative to this instruction

  44.     /**
  45.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  46.      */
  47.     @Deprecated
  48.     protected InstructionHandle target; // Target object in instruction list

  49.     /**
  50.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  51.      */
  52.     @Deprecated
  53.     protected int position; // Byte code offset

  54.     /**
  55.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  56.      */
  57.     BranchInstruction() {
  58.     }

  59.     /**
  60.      * Common super constructor
  61.      *
  62.      * @param opcode Instruction opcode
  63.      * @param target instruction to branch to
  64.      */
  65.     protected BranchInstruction(final short opcode, final InstructionHandle target) {
  66.         super(opcode, (short) 3);
  67.         setTarget(target);
  68.     }

  69.     /**
  70.      * @return true, if ih is target of this instruction
  71.      */
  72.     @Override
  73.     public boolean containsTarget(final InstructionHandle ih) {
  74.         return target == ih;
  75.     }

  76.     /**
  77.      * Inform target that it's not targeted anymore.
  78.      */
  79.     @Override
  80.     void dispose() {
  81.         setTarget(null);
  82.         index = -1;
  83.         position = -1;
  84.     }

  85.     /**
  86.      * Dump instruction as byte code to stream out.
  87.      *
  88.      * @param out Output stream
  89.      */
  90.     @Override
  91.     public void dump(final DataOutputStream out) throws IOException {
  92.         out.writeByte(super.getOpcode());
  93.         index = getTargetOffset();
  94.         if (!isValidShort(index)) {
  95.             throw new ClassGenException("Branch target offset too large for short: " + index);
  96.         }
  97.         out.writeShort(index); // May be negative, i.e., point backwards
  98.     }

  99.     /**
  100.      * @return target offset in byte code
  101.      */
  102.     public final int getIndex() {
  103.         return index;
  104.     }

  105.     /**
  106.      * @return the position
  107.      * @since 6.0
  108.      */
  109.     protected int getPosition() {
  110.         return position;
  111.     }

  112.     /**
  113.      * @return target of branch instruction
  114.      */
  115.     public InstructionHandle getTarget() {
  116.         return target;
  117.     }

  118.     /**
  119.      * @return the offset to this instruction's target
  120.      */
  121.     protected int getTargetOffset() {
  122.         return getTargetOffset(target);
  123.     }

  124.     /**
  125.      * @param target branch target
  126.      * @return the offset to 'target' relative to this instruction
  127.      */
  128.     protected int getTargetOffset(final InstructionHandle target) {
  129.         if (target == null) {
  130.             throw new ClassGenException("Target of " + super.toString(true) + " is invalid null handle");
  131.         }
  132.         final int t = target.getPosition();
  133.         if (t < 0) {
  134.             throw new ClassGenException("Invalid branch target position offset for " + super.toString(true) + ":" + t + ":" + target);
  135.         }
  136.         return t - position;
  137.     }

  138.     /**
  139.      * Read needed data (e.g. index) from file. Conversion to a InstructionHandle is done in InstructionList(byte[]).
  140.      *
  141.      * @param bytes input stream
  142.      * @param wide wide prefix?
  143.      * @see InstructionList
  144.      */
  145.     @Override
  146.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  147.         super.setLength(3);
  148.         index = bytes.readShort();
  149.     }

  150.     /**
  151.      * @param index the index to set
  152.      * @since 6.0
  153.      */
  154.     protected void setIndex(final int index) {
  155.         this.index = index;
  156.     }

  157.     /**
  158.      * @param position the position to set
  159.      * @since 6.0
  160.      */
  161.     protected void setPosition(final int position) {
  162.         this.position = position;
  163.     }

  164.     /**
  165.      * Sets branch target
  166.      *
  167.      * @param target branch target
  168.      */
  169.     public void setTarget(final InstructionHandle target) {
  170.         notifyTarget(this.target, target, this);
  171.         this.target = target;
  172.     }

  173.     /**
  174.      * Long output format:
  175.      *
  176.      * &lt;position in byte code&gt; &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" "("&lt;length of instruction&gt;")"
  177.      * "&lt;"&lt;target instruction&gt;"&gt;" "@"&lt;branch target offset&gt;
  178.      *
  179.      * @param verbose long/short format switch
  180.      * @return mnemonic for instruction
  181.      */
  182.     @Override
  183.     public String toString(final boolean verbose) {
  184.         final String s = super.toString(verbose);
  185.         String t = "null";
  186.         if (target != null) {
  187.             if (verbose) {
  188.                 if (target.getInstruction() == this) {
  189.                     t = "<points to itself>";
  190.                 } else if (target.getInstruction() == null) {
  191.                     t = "<null instruction!!!?>";
  192.                 } else {
  193.                     // I'm more interested in the address of the target then
  194.                     // the instruction located there.
  195.                     // t = target.getInstruction().toString(false); // Avoid circles
  196.                     t = "" + target.getPosition();
  197.                 }
  198.             } else {
  199.                 index = target.getPosition();
  200.                 // index = getTargetOffset(); crashes if positions haven't been set
  201.                 // t = "" + (index + position);
  202.                 t = "" + index;
  203.             }
  204.         }
  205.         return s + " -> " + t;
  206.     }

  207.     /**
  208.      * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable
  209.      * length instructions 'setPositions' performs multiple passes over the instruction list to calculate the correct (byte)
  210.      * positions and offsets by calling this function.
  211.      *
  212.      * @param offset additional offset caused by preceding (variable length) instructions
  213.      * @param maxOffset the maximum offset that may be caused by these instructions
  214.      * @return additional offset caused by possible change of this instruction's length
  215.      */
  216.     protected int updatePosition(final int offset, final int maxOffset) {
  217.         position += offset;
  218.         return 0;
  219.     }

  220.     /**
  221.      * @param oldIh old target
  222.      * @param newIh new target
  223.      */
  224.     @Override
  225.     public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
  226.         if (target != oldIh) {
  227.             throw new ClassGenException("Not targeting " + oldIh + ", but " + target);
  228.         }
  229.         setTarget(newIh);
  230.     }

  231. }