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