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