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 *
017 */
018package org.apache.bcel.generic;
019
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023/**
024 * GOTO - Branch always (to relative offset, not absolute address)
025 *
026 */
027public class GOTO extends GotoInstruction implements VariableLengthInstruction {
028
029    /**
030     * Empty constructor needed for Instruction.readInstruction.
031     * Not to be used otherwise.
032     */
033    GOTO() {
034    }
035
036
037    public GOTO(final InstructionHandle target) {
038        super(org.apache.bcel.Const.GOTO, target);
039    }
040
041
042    /**
043     * Dump instruction as byte code to stream out.
044     * @param out Output stream
045     */
046    @Override
047    public void dump( final DataOutputStream out ) throws IOException {
048        super.setIndex(getTargetOffset());
049        final short _opcode = getOpcode();
050        if (_opcode == org.apache.bcel.Const.GOTO) {
051            super.dump(out);
052        } else { // GOTO_W
053            super.setIndex(getTargetOffset());
054            out.writeByte(_opcode);
055            out.writeInt(super.getIndex());
056        }
057    }
058
059
060    /**
061     * Called in pass 2 of InstructionList.setPositions() in order to update
062     * the branch target, that may shift due to variable length instructions.
063     *
064     * @param offset additional offset caused by preceding (variable length) instructions
065     * @param max_offset the maximum offset that may be caused by these instructions
066     * @return additional offset caused by possible change of this instruction's length
067     */
068    @Override
069    protected int updatePosition( final int offset, final int max_offset ) {
070        final int i = getTargetOffset(); // Depending on old position value
071        setPosition(getPosition() + offset); // Position may be shifted by preceding expansions
072        if (Math.abs(i) >= (Short.MAX_VALUE - max_offset)) { // to large for short (estimate)
073            super.setOpcode(org.apache.bcel.Const.GOTO_W);
074            final short old_length = (short) super.getLength();
075            super.setLength(5);
076            return super.getLength() - old_length;
077        }
078        return 0;
079    }
080
081
082    /**
083     * Call corresponding visitor method(s). The order is:
084     * Call visitor methods of implemented interfaces first, then
085     * call methods according to the class hierarchy in descending order,
086     * i.e., the most specific visitXXX() call comes last.
087     *
088     * @param v Visitor object
089     */
090    @Override
091    public void accept( final Visitor v ) {
092        v.visitVariableLengthInstruction(this);
093        v.visitUnconditionalBranch(this);
094        v.visitBranchInstruction(this);
095        v.visitGotoInstruction(this);
096        v.visitGOTO(this);
097    }
098}