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