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 * 029 * <PRE> 030 * Stack: ..., count -> ..., arrayref 031 * </PRE> 032 * 033 * type must be one of T_INT, T_SHORT, ... 034 * 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 * Read needed data (e.g. 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}