1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 package org.apache.bcel.generic; 19 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 23 import org.apache.bcel.ExceptionConst; 24 import org.apache.bcel.classfile.ConstantPool; 25 import org.apache.bcel.util.ByteSequence; 26 27 /** 28 * MULTIANEWARRAY - Create new mutidimensional array of references 29 * <PRE>Stack: ..., count1, [count2, ...] -> ..., arrayref</PRE> 30 * 31 * @version $Id$ 32 */ 33 public class MULTIANEWARRAY extends CPInstruction implements LoadClass, AllocationInstruction, 34 ExceptionThrower { 35 36 private short dimensions; 37 38 39 /** 40 * Empty constructor needed for Instruction.readInstruction. 41 * Not to be used otherwise. 42 */ MULTIANEWARRAY()43 MULTIANEWARRAY() { 44 } 45 46 MULTIANEWARRAY(final int index, final short dimensions)47 public MULTIANEWARRAY(final int index, final short dimensions) { 48 super(org.apache.bcel.Const.MULTIANEWARRAY, index); 49 if (dimensions < 1) { 50 throw new ClassGenException("Invalid dimensions value: " + dimensions); 51 } 52 this.dimensions = dimensions; 53 super.setLength(4); 54 } 55 56 57 /** 58 * Dump instruction as byte code to stream out. 59 * @param out Output stream 60 */ 61 @Override dump( final DataOutputStream out )62 public void dump( final DataOutputStream out ) throws IOException { 63 out.writeByte(super.getOpcode()); 64 out.writeShort(super.getIndex()); 65 out.writeByte(dimensions); 66 } 67 68 69 /** 70 * Read needed data (i.e., no. dimension) from file. 71 */ 72 @Override initFromFile( final ByteSequence bytes, final boolean wide )73 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { 74 super.initFromFile(bytes, wide); 75 dimensions = bytes.readByte(); 76 super.setLength(4); 77 } 78 79 80 /** 81 * @return number of dimensions to be created 82 */ getDimensions()83 public final short getDimensions() { 84 return dimensions; 85 } 86 87 88 /** 89 * @return mnemonic for instruction 90 */ 91 @Override toString( final boolean verbose )92 public String toString( final boolean verbose ) { 93 return super.toString(verbose) + " " + super.getIndex() + " " + dimensions; 94 } 95 96 97 /** 98 * @return mnemonic for instruction with symbolic references resolved 99 */ 100 @Override toString( final ConstantPool cp )101 public String toString( final ConstantPool cp ) { 102 return super.toString(cp) + " " + dimensions; 103 } 104 105 106 /** 107 * Also works for instructions whose stack effect depends on the 108 * constant pool entry they reference. 109 * @return Number of words consumed from stack by this instruction 110 */ 111 @Override consumeStack( final ConstantPoolGen cpg )112 public int consumeStack( final ConstantPoolGen cpg ) { 113 return dimensions; 114 } 115 116 117 @Override getExceptions()118 public Class<?>[] getExceptions() { 119 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_CLASS_AND_INTERFACE_RESOLUTION, 120 ExceptionConst.ILLEGAL_ACCESS_ERROR, 121 ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION); 122 } 123 124 125 @Override getLoadClassType( final ConstantPoolGen cpg )126 public ObjectType getLoadClassType( final ConstantPoolGen cpg ) { 127 Type t = getType(cpg); 128 if (t instanceof ArrayType) { 129 t = ((ArrayType) t).getBasicType(); 130 } 131 return (t instanceof ObjectType) ? (ObjectType) t : null; 132 } 133 134 135 /** 136 * Call corresponding visitor method(s). The order is: 137 * Call visitor methods of implemented interfaces first, then 138 * call methods according to the class hierarchy in descending order, 139 * i.e., the most specific visitXXX() call comes last. 140 * 141 * @param v Visitor object 142 */ 143 @Override accept( final Visitor v )144 public void accept( final Visitor v ) { 145 v.visitLoadClass(this); 146 v.visitAllocationInstruction(this); 147 v.visitExceptionThrower(this); 148 v.visitTypedInstruction(this); 149 v.visitCPInstruction(this); 150 v.visitMULTIANEWARRAY(this); 151 } 152 } 153