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 java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.ExceptionConst;
025import org.apache.bcel.classfile.ConstantPool;
026import org.apache.bcel.util.ByteSequence;
027
028/**
029 * MULTIANEWARRAY - Create new mutidimensional array of references
030 *
031 * <pre>
032 * Stack: ..., count1, [count2, ...] -&gt; ..., arrayref
033 * </pre>
034 */
035public class MULTIANEWARRAY extends CPInstruction implements LoadClass, AllocationInstruction, ExceptionThrower {
036
037    private short dimensions;
038
039    /**
040     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
041     */
042    MULTIANEWARRAY() {
043    }
044
045    /**
046     * Constructs a MULTIANEWARRAY instruction.
047     *
048     * @param index index into constant pool.
049     * @param dimensions number of dimensions.
050     */
051    public MULTIANEWARRAY(final int index, final short dimensions) {
052        super(org.apache.bcel.Const.MULTIANEWARRAY, index);
053        if (dimensions < 1 || dimensions > org.apache.bcel.Const.MAX_BYTE) {
054            throw new ClassGenException("Invalid dimensions value: " + dimensions);
055        }
056        this.dimensions = dimensions;
057        super.setLength(4);
058    }
059
060    /**
061     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
062     * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
063     *
064     * @param v Visitor object.
065     */
066    @Override
067    public void accept(final Visitor v) {
068        v.visitLoadClass(this);
069        v.visitAllocationInstruction(this);
070        v.visitExceptionThrower(this);
071        v.visitTypedInstruction(this);
072        v.visitCPInstruction(this);
073        v.visitMULTIANEWARRAY(this);
074    }
075
076    /**
077     * Also works for instructions whose stack effect depends on the constant pool entry they reference.
078     *
079     * @return Number of words consumed from stack by this instruction.
080     */
081    @Override
082    public int consumeStack(final ConstantPoolGen cpg) {
083        return dimensions;
084    }
085
086    /**
087     * Dumps instruction as byte code to stream out.
088     *
089     * @param out Output stream.
090     */
091    @Override
092    public void dump(final DataOutputStream out) throws IOException {
093        out.writeByte(super.getOpcode());
094        out.writeShort(super.getIndex());
095        out.writeByte(dimensions);
096    }
097
098    /**
099     * Gets the number of dimensions.
100     *
101     * @return number of dimensions to be created.
102     */
103    public final short getDimensions() {
104        return dimensions;
105    }
106
107    @Override
108    public Class<?>[] getExceptions() {
109        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_CLASS_AND_INTERFACE_RESOLUTION, ExceptionConst.ILLEGAL_ACCESS_ERROR,
110            ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION);
111    }
112
113    @Override
114    public ObjectType getLoadClassType(final ConstantPoolGen cpg) {
115        Type t = getType(cpg);
116        if (t instanceof ArrayType) {
117            t = ((ArrayType) t).getBasicType();
118        }
119        return t instanceof ObjectType ? (ObjectType) t : null;
120    }
121
122    /**
123     * Reads needed data (that is, no. dimension) from file.
124     */
125    @Override
126    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
127        super.initFromFile(bytes, wide);
128        dimensions = (short) bytes.readUnsignedByte();
129        super.setLength(4);
130    }
131
132    /**
133     * @return mnemonic for instruction.
134     */
135    @Override
136    public String toString(final boolean verbose) {
137        return super.toString(verbose) + " " + super.getIndex() + " " + dimensions;
138    }
139
140    /**
141     * @return mnemonic for instruction with symbolic references resolved.
142     */
143    @Override
144    public String toString(final ConstantPool cp) {
145        return super.toString(cp) + " " + dimensions;
146    }
147}