    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.util.ByteSequence;
    026
    027/**
    028 * NEWARRAY - Create new array of basic type (int, short, ...)
    029 *
    030 * <PRE>
    031 * Stack: ..., count -&gt; ..., arrayref
    032 * </PRE>
    033 *
    034 * type must be one of T_INT, T_SHORT, ...
    035 */
    036public class NEWARRAY extends Instruction implements AllocationInstruction, ExceptionThrower, StackProducer {
    037
    038    private byte type;
    039
    040    /**
    041     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    042     */
    043    NEWARRAY() {
    044    }
    045
    046    public NEWARRAY(final BasicType type) {
    047        this(type.getType());
    048    }
    049
    050    public NEWARRAY(final byte type) {
    051        super(org.apache.bcel.Const.NEWARRAY, (short) 2);
    052        this.type = type;
    053    }
    054
    055    /**
    056     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
    057     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
    058     *
    059     * @param v Visitor object
    060     */
    061    @Override
    062    public void accept(final Visitor v) {
    063        v.visitAllocationInstruction(this);
    064        v.visitExceptionThrower(this);
    065        v.visitStackProducer(this);
    066        v.visitNEWARRAY(this);
    067    }
    068
    069    /**
    070     * Dump instruction as byte code to stream out.
    071     *
    072     * @param out Output stream
    073     */
    074    @Override
    075    public void dump(final DataOutputStream out) throws IOException {
    076        out.writeByte(super.getOpcode());
    077        out.writeByte(type);
    078    }
    079
    080    @Override
    081    public Class<?>[] getExceptions() {
    082        return new Class[] {ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION};
    083    }
    084
    085    /**
    086     * @return type of constructed array
    087     */
    088    public final Type getType() {
    089        return new ArrayType(BasicType.getType(type), 1);
    090    }
    091
    092    /**
    093     * @return numeric code for basic element type
    094     */
    095    public final byte getTypecode() {
    096        return type;
    097    }
    098
    099    /**
    100     * Reads needed data (for example index) from file.
    101     */
    102    @Override
    103    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
    104        type = bytes.readByte();
    105        super.setLength(2);
    106    }
    107
    108    /**
    109     * @return mnemonic for instruction
    110     */
    111    @Override
    112    public String toString(final boolean verbose) {
    113        return super.toString(verbose) + " " + org.apache.bcel.Const.getTypeName(type);
    114    }
    115}