    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.ExceptionConst;
    021
    022/**
    023 * Super class for instructions dealing with array access such as IALOAD.
    024 *
    025 */
    026public abstract class ArrayInstruction extends Instruction implements ExceptionThrower,
    027        TypedInstruction {
    028
    029    /**
    030     * Empty constructor needed for Instruction.readInstruction.
    031     * Not to be used otherwise.
    032     */
    033    ArrayInstruction() {
    034    }
    035
    036
    037    /**
    038     * @param opcode of instruction
    039     */
    040    protected ArrayInstruction(final short opcode) {
    041        super(opcode, (short) 1);
    042    }
    043
    044
    045    @Override
    046    public Class<?>[] getExceptions() {
    047        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_ARRAY_EXCEPTION);
    048    }
    049
    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("Oops: unknown case in switch" + _opcode);
    083        }
    084    }
    085}