    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.bcel.generic;
    020
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.util.ByteSequence;
    025
    026/**
    027 * RET - Return from subroutine
    028 *
    029 * <PRE>
    030 * Stack: ... -&gt; ...
    031 * </PRE>
    032 */
    033public class RET extends Instruction implements IndexedInstruction, TypedInstruction {
    034
    035    private boolean wide;
    036    private int index; // index to local variable containg the return address
    037
    038    /**
    039     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    040     */
    041    RET() {
    042    }
    043
    044    public RET(final int index) {
    045        super(org.apache.bcel.Const.RET, (short) 2);
    046        setIndex(index); // May set wide as side effect
    047    }
    048
    049    /**
    050     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
    051     * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
    052     *
    053     * @param v Visitor object.
    054     */
    055    @Override
    056    public void accept(final Visitor v) {
    057        v.visitRET(this);
    058    }
    059
    060    /**
    061     * Dumps instruction as byte code to stream out.
    062     *
    063     * @param out Output stream.
    064     */
    065    @Override
    066    public void dump(final DataOutputStream out) throws IOException {
    067        if (wide) {
    068            out.writeByte(org.apache.bcel.Const.WIDE);
    069        }
    070        out.writeByte(super.getOpcode());
    071        if (wide) {
    072            out.writeShort(index);
    073        } else {
    074            out.writeByte(index);
    075        }
    076    }
    077
    078    /**
    079     * @return index of local variable containg the return address.
    080     */
    081    @Override
    082    public final int getIndex() {
    083        return index;
    084    }
    085
    086    /**
    087     * @return return address type.
    088     */
    089    @Override
    090    public Type getType(final ConstantPoolGen cp) {
    091        return ReturnaddressType.NO_TARGET;
    092    }
    093
    094    /**
    095     * Reads needed data (for example index) from file.
    096     */
    097    @Override
    098    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
    099        this.wide = wide;
    100        if (wide) {
    101            index = bytes.readUnsignedShort();
    102            super.setLength(4);
    103        } else {
    104            index = bytes.readUnsignedByte();
    105            super.setLength(2);
    106        }
    107    }
    108
    109    /**
    110     * Sets index of local variable containg the return address
    111     */
    112    @Override
    113    public final void setIndex(final int n) {
    114        if (n < 0) {
    115            throw new ClassGenException("Negative index value: " + n);
    116        }
    117        index = n;
    118        setWide();
    119    }
    120
    121    private void setWide() {
    122        wide = index > org.apache.bcel.Const.MAX_BYTE;
    123        if (wide) {
    124            super.setLength(4); // Including the wide byte
    125        } else {
    126            super.setLength(2);
    127        }
    128    }
    129
    130    /**
    131     * @return mnemonic for instruction.
    132     */
    133    @Override
    134    public String toString(final boolean verbose) {
    135        return super.toString(verbose) + " " + index;
    136    }
    137}