    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 org.apache.bcel.Const;
    022import org.apache.bcel.ExceptionConst;
    023
    024/**
    025 * Super class for the xRETURN family of instructions.
    026 */
    027public abstract class ReturnInstruction extends Instruction implements ExceptionThrower, TypedInstruction, StackConsumer {
    028
    029    /**
    030     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    031     */
    032    ReturnInstruction() {
    033    }
    034
    035    /**
    036     * Constructs a ReturnInstruction.
    037     *
    038     * @param opcode of instruction.
    039     */
    040    protected ReturnInstruction(final short opcode) {
    041        super(opcode, (short) 1);
    042    }
    043
    044    @Override
    045    public Class<?>[] getExceptions() {
    046        return new Class[] {ExceptionConst.ILLEGAL_MONITOR_STATE};
    047    }
    048
    049    public Type getType() {
    050        final short opcode = super.getOpcode();
    051        switch (opcode) {
    052        case Const.IRETURN:
    053            return Type.INT;
    054        case Const.LRETURN:
    055            return Type.LONG;
    056        case Const.FRETURN:
    057            return Type.FLOAT;
    058        case Const.DRETURN:
    059            return Type.DOUBLE;
    060        case Const.ARETURN:
    061            return Type.OBJECT;
    062        case Const.RETURN:
    063            return Type.VOID;
    064        default: // Never reached
    065            throw new ClassGenException("Unknown type " + opcode);
    066        }
    067    }
    068
    069    /**
    070     * @return type associated with the instruction.
    071     */
    072    @Override
    073    public Type getType(final ConstantPoolGen cp) {
    074        return getType();
    075    }
    076}