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