001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.generic;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022/**
023 * GOTO - Branch always (to relative offset, not absolute address)
024 */
025public class GOTO extends GotoInstruction implements VariableLengthInstruction {
026
027    /**
028     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
029     */
030    GOTO() {
031    }
032
033    public GOTO(final InstructionHandle target) {
034        super(org.apache.bcel.Const.GOTO, target);
035    }
036
037    /**
038     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
039     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
040     *
041     * @param v Visitor object
042     */
043    @Override
044    public void accept(final Visitor v) {
045        v.visitVariableLengthInstruction(this);
046        v.visitUnconditionalBranch(this);
047        v.visitBranchInstruction(this);
048        v.visitGotoInstruction(this);
049        v.visitGOTO(this);
050    }
051
052    /**
053     * Dump instruction as byte code to stream out.
054     *
055     * @param out Output stream
056     */
057    @Override
058    public void dump(final DataOutputStream out) throws IOException {
059        super.setIndex(getTargetOffset());
060        final short opcode = getOpcode();
061        if (opcode == org.apache.bcel.Const.GOTO) {
062            super.dump(out);
063        } else { // GOTO_W
064            super.setIndex(getTargetOffset());
065            out.writeByte(opcode);
066            out.writeInt(super.getIndex());
067        }
068    }
069
070    /**
071     * Called in pass 2 of InstructionList.setPositions() in order to update the branch target, that may shift due to
072     * variable length instructions.
073     *
074     * @param offset additional offset caused by preceding (variable length) instructions
075     * @param maxOffset the maximum offset that may be caused by these instructions
076     * @return additional offset caused by possible change of this instruction's length
077     */
078    @Override
079    protected int updatePosition(final int offset, final int maxOffset) {
080        final int i = getTargetOffset(); // Depending on old position value
081        setPosition(getPosition() + offset); // Position may be shifted by preceding expansions
082        if (Math.abs(i) >= Short.MAX_VALUE - maxOffset) { // to large for short (estimate)
083            super.setOpcode(org.apache.bcel.Const.GOTO_W);
084            final short oldLength = (short) super.getLength();
085            super.setLength(5);
086            return super.getLength() - oldLength;
087        }
088        return 0;
089    }
090}