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.classfile.ConstantPool; 025import org.apache.bcel.util.ByteSequence; 026 027/** 028 * MULTIANEWARRAY - Create new mutidimensional array of references 029 * 030 * <PRE> 031 * Stack: ..., count1, [count2, ...] -> ..., arrayref 032 * </PRE> 033 * 034 */ 035public class MULTIANEWARRAY extends CPInstruction implements LoadClass, AllocationInstruction, ExceptionThrower { 036 037 private short dimensions; 038 039 /** 040 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. 041 */ 042 MULTIANEWARRAY() { 043 } 044 045 public MULTIANEWARRAY(final int index, final short dimensions) { 046 super(org.apache.bcel.Const.MULTIANEWARRAY, index); 047 if (dimensions < 1) { 048 throw new ClassGenException("Invalid dimensions value: " + dimensions); 049 } 050 this.dimensions = dimensions; 051 super.setLength(4); 052 } 053 054 /** 055 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call 056 * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last. 057 * 058 * @param v Visitor object 059 */ 060 @Override 061 public void accept(final Visitor v) { 062 v.visitLoadClass(this); 063 v.visitAllocationInstruction(this); 064 v.visitExceptionThrower(this); 065 v.visitTypedInstruction(this); 066 v.visitCPInstruction(this); 067 v.visitMULTIANEWARRAY(this); 068 } 069 070 /** 071 * Also works for instructions whose stack effect depends on the constant pool entry they reference. 072 * 073 * @return Number of words consumed from stack by this instruction 074 */ 075 @Override 076 public int consumeStack(final ConstantPoolGen cpg) { 077 return dimensions; 078 } 079 080 /** 081 * Dump instruction as byte code to stream out. 082 * 083 * @param out Output stream 084 */ 085 @Override 086 public void dump(final DataOutputStream out) throws IOException { 087 out.writeByte(super.getOpcode()); 088 out.writeShort(super.getIndex()); 089 out.writeByte(dimensions); 090 } 091 092 /** 093 * @return number of dimensions to be created 094 */ 095 public final short getDimensions() { 096 return dimensions; 097 } 098 099 @Override 100 public Class<?>[] getExceptions() { 101 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_CLASS_AND_INTERFACE_RESOLUTION, ExceptionConst.ILLEGAL_ACCESS_ERROR, 102 ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION); 103 } 104 105 @Override 106 public ObjectType getLoadClassType(final ConstantPoolGen cpg) { 107 Type t = getType(cpg); 108 if (t instanceof ArrayType) { 109 t = ((ArrayType) t).getBasicType(); 110 } 111 return t instanceof ObjectType ? (ObjectType) t : null; 112 } 113 114 /** 115 * Read needed data (i.e., no. dimension) from file. 116 */ 117 @Override 118 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 119 super.initFromFile(bytes, wide); 120 dimensions = bytes.readByte(); 121 super.setLength(4); 122 } 123 124 /** 125 * @return mnemonic for instruction 126 */ 127 @Override 128 public String toString(final boolean verbose) { 129 return super.toString(verbose) + " " + super.getIndex() + " " + dimensions; 130 } 131 132 /** 133 * @return mnemonic for instruction with symbolic references resolved 134 */ 135 @Override 136 public String toString(final ConstantPool cp) { 137 return super.toString(cp) + " " + dimensions; 138 } 139}