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