GOTO.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. /**
  21.  * GOTO - Branch always (to relative offset, not absolute address)
  22.  */
  23. public class GOTO extends GotoInstruction implements VariableLengthInstruction {

  24.     /**
  25.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  26.      */
  27.     GOTO() {
  28.     }

  29.     public GOTO(final InstructionHandle target) {
  30.         super(org.apache.bcel.Const.GOTO, target);
  31.     }

  32.     /**
  33.      * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
  34.      * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
  35.      *
  36.      * @param v Visitor object
  37.      */
  38.     @Override
  39.     public void accept(final Visitor v) {
  40.         v.visitVariableLengthInstruction(this);
  41.         v.visitUnconditionalBranch(this);
  42.         v.visitBranchInstruction(this);
  43.         v.visitGotoInstruction(this);
  44.         v.visitGOTO(this);
  45.     }

  46.     /**
  47.      * Dump instruction as byte code to stream out.
  48.      *
  49.      * @param out Output stream
  50.      */
  51.     @Override
  52.     public void dump(final DataOutputStream out) throws IOException {
  53.         super.setIndex(getTargetOffset());
  54.         final short opcode = getOpcode();
  55.         if (opcode == org.apache.bcel.Const.GOTO) {
  56.             super.dump(out);
  57.         } else { // GOTO_W
  58.             super.setIndex(getTargetOffset());
  59.             out.writeByte(opcode);
  60.             out.writeInt(super.getIndex());
  61.         }
  62.     }

  63.     /**
  64.      * Called in pass 2 of InstructionList.setPositions() in order to update the branch target, that may shift due to
  65.      * variable length instructions.
  66.      *
  67.      * @param offset additional offset caused by preceding (variable length) instructions
  68.      * @param maxOffset the maximum offset that may be caused by these instructions
  69.      * @return additional offset caused by possible change of this instruction's length
  70.      */
  71.     @Override
  72.     protected int updatePosition(final int offset, final int maxOffset) {
  73.         final int i = getTargetOffset(); // Depending on old position value
  74.         setPosition(getPosition() + offset); // Position may be shifted by preceding expansions
  75.         if (Math.abs(i) >= Short.MAX_VALUE - maxOffset) { // to large for short (estimate)
  76.             super.setOpcode(org.apache.bcel.Const.GOTO_W);
  77.             final short oldLength = (short) super.getLength();
  78.             super.setLength(5);
  79.             return super.getLength() - oldLength;
  80.         }
  81.         return 0;
  82.     }
  83. }