    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
    023import org.apache.bcel.util.ByteSequence;
    024
    025/**
    026 * RET - Return from subroutine
    027 *
    028 * <PRE>
    029 * Stack: ... -&gt; ...
    030 * </PRE>
    031 *
    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, i.e., 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     * Dump 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     * Read needed data (e.g. 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     * Set 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}