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