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