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.ExceptionConst;
022
023/**
024 * Superclass for instructions dealing with array access such as IALOAD.
025 */
026public abstract class ArrayInstruction extends Instruction implements ExceptionThrower, TypedInstruction {
027
028    /**
029     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
030     */
031    ArrayInstruction() {
032    }
033
034    /**
035     * Constructs an ArrayInstruction.
036     *
037     * @param opcode of instruction.
038     */
039    protected ArrayInstruction(final short opcode) {
040        super(opcode, (short) 1);
041    }
042
043    @Override
044    public Class<?>[] getExceptions() {
045        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_ARRAY_EXCEPTION);
046    }
047
048    /**
049     * Gets the type associated with the instruction.
050     *
051     * @return type associated with the instruction.
052     */
053    @Override
054    public Type getType(final ConstantPoolGen cp) {
055        final short opcode = super.getOpcode();
056        switch (opcode) {
057        case org.apache.bcel.Const.IALOAD:
058        case org.apache.bcel.Const.IASTORE:
059            return Type.INT;
060        case org.apache.bcel.Const.CALOAD:
061        case org.apache.bcel.Const.CASTORE:
062            return Type.CHAR;
063        case org.apache.bcel.Const.BALOAD:
064        case org.apache.bcel.Const.BASTORE:
065            return Type.BYTE;
066        case org.apache.bcel.Const.SALOAD:
067        case org.apache.bcel.Const.SASTORE:
068            return Type.SHORT;
069        case org.apache.bcel.Const.LALOAD:
070        case org.apache.bcel.Const.LASTORE:
071            return Type.LONG;
072        case org.apache.bcel.Const.DALOAD:
073        case org.apache.bcel.Const.DASTORE:
074            return Type.DOUBLE;
075        case org.apache.bcel.Const.FALOAD:
076        case org.apache.bcel.Const.FASTORE:
077            return Type.FLOAT;
078        case org.apache.bcel.Const.AALOAD:
079        case org.apache.bcel.Const.AASTORE:
080            return Type.OBJECT;
081        default:
082            throw new ClassGenException("Unknown case in switch" + opcode);
083        }
084    }
085}