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    public MULTIANEWARRAY(final int index, final short dimensions) {
046        super(org.apache.bcel.Const.MULTIANEWARRAY, index);
047        if (dimensions < 1) {
048            throw new ClassGenException("Invalid dimensions value: " + dimensions);
049        }
050        this.dimensions = dimensions;
051        super.setLength(4);
052    }
053
054    /**
055     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
056     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
057     *
058     * @param v Visitor object
059     */
060    @Override
061    public void accept(final Visitor v) {
062        v.visitLoadClass(this);
063        v.visitAllocationInstruction(this);
064        v.visitExceptionThrower(this);
065        v.visitTypedInstruction(this);
066        v.visitCPInstruction(this);
067        v.visitMULTIANEWARRAY(this);
068    }
069
070    /**
071     * Also works for instructions whose stack effect depends on the constant pool entry they reference.
072     *
073     * @return Number of words consumed from stack by this instruction
074     */
075    @Override
076    public int consumeStack(final ConstantPoolGen cpg) {
077        return dimensions;
078    }
079
080    /**
081     * Dump instruction as byte code to stream out.
082     *
083     * @param out Output stream
084     */
085    @Override
086    public void dump(final DataOutputStream out) throws IOException {
087        out.writeByte(super.getOpcode());
088        out.writeShort(super.getIndex());
089        out.writeByte(dimensions);
090    }
091
092    /**
093     * @return number of dimensions to be created
094     */
095    public final short getDimensions() {
096        return dimensions;
097    }
098
099    @Override
100    public Class<?>[] getExceptions() {
101        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_CLASS_AND_INTERFACE_RESOLUTION, ExceptionConst.ILLEGAL_ACCESS_ERROR,
102            ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION);
103    }
104
105    @Override
106    public ObjectType getLoadClassType(final ConstantPoolGen cpg) {
107        Type t = getType(cpg);
108        if (t instanceof ArrayType) {
109            t = ((ArrayType) t).getBasicType();
110        }
111        return t instanceof ObjectType ? (ObjectType) t : null;
112    }
113
114    /**
115     * Reads needed data (i.e., no. dimension) from file.
116     */
117    @Override
118    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
119        super.initFromFile(bytes, wide);
120        dimensions = bytes.readByte();
121        super.setLength(4);
122    }
123
124    /**
125     * @return mnemonic for instruction
126     */
127    @Override
128    public String toString(final boolean verbose) {
129        return super.toString(verbose) + " " + super.getIndex() + " " + dimensions;
130    }
131
132    /**
133     * @return mnemonic for instruction with symbolic references resolved
134     */
135    @Override
136    public String toString(final ConstantPool cp) {
137        return super.toString(cp) + " " + dimensions;
138    }
139}