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 org.apache.bcel.Const;
021
022/**
023 * Super class for the x2y family of instructions.
024 *
025 */
026public abstract class ConversionInstruction extends Instruction implements TypedInstruction, StackProducer, StackConsumer {
027
028    /**
029     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
030     */
031    ConversionInstruction() {
032    }
033
034    /**
035     * @param opcode opcode of instruction
036     */
037    protected ConversionInstruction(final short opcode) {
038        super(opcode, (short) 1);
039    }
040
041    /**
042     * @return type associated with the instruction
043     */
044    @Override
045    public Type getType(final ConstantPoolGen cp) {
046        final short opcode = super.getOpcode();
047        switch (opcode) {
048        case Const.D2I:
049        case Const.F2I:
050        case Const.L2I:
051            return Type.INT;
052        case Const.D2F:
053        case Const.I2F:
054        case Const.L2F:
055            return Type.FLOAT;
056        case Const.D2L:
057        case Const.F2L:
058        case Const.I2L:
059            return Type.LONG;
060        case Const.F2D:
061        case Const.I2D:
062        case Const.L2D:
063            return Type.DOUBLE;
064        case Const.I2B:
065            return Type.BYTE;
066        case Const.I2C:
067            return Type.CHAR;
068        case Const.I2S:
069            return Type.SHORT;
070        default: // Never reached
071            throw new ClassGenException("Unknown type " + opcode);
072        }
073    }
074}