1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.generic; 23 24 import java.io.*; 25 import com.sun.org.apache.bcel.internal.util.ByteSequence; 26 27 /** 28 * NEWARRAY - Create new array of basic type (int, short, ...) 29 * <PRE>Stack: ..., count -> ..., arrayref</PRE> 30 * type must be one of T_INT, T_SHORT, ... 31 * 32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 33 */ 34 public class NEWARRAY extends Instruction 35 implements AllocationInstruction, ExceptionThrower, StackProducer { 36 private byte type; 37 38 /** 39 * Empty constructor needed for the Class.newInstance() statement in 40 * Instruction.readInstruction(). Not to be used otherwise. 41 */ 42 NEWARRAY() {} 43 44 public NEWARRAY(byte type) { 45 super(com.sun.org.apache.bcel.internal.Constants.NEWARRAY, (short)2); 46 this.type = type; 47 } 48 49 public NEWARRAY(BasicType type) { 50 this(type.getType()); 51 } 52 53 /** 54 * Dump instruction as byte code to stream out. 55 * @param out Output stream 56 */ 57 public void dump(DataOutputStream out) throws IOException { 58 out.writeByte(opcode); 59 out.writeByte(type); 60 } 61 62 /** 63 * @return numeric code for basic element type 64 */ 65 public final byte getTypecode() { return type; } 66 67 /** 68 * @return type of constructed array 69 */ 70 public final Type getType() { 71 return new ArrayType(BasicType.getType(type), 1); 72 } 73 74 /** 75 * @return mnemonic for instruction 76 */ 77 public String toString(boolean verbose) { 78 return super.toString(verbose) + " " + com.sun.org.apache.bcel.internal.Constants.TYPE_NAMES[type]; 79 } 80 /** 81 * Read needed data (e.g. index) from file. 82 */ 83 protected void initFromFile(ByteSequence bytes, boolean wide) throws IOException 84 { 85 type = bytes.readByte(); 86 length = 2; 87 } 88 89 public Class[] getExceptions() { 90 return new Class[] { com.sun.org.apache.bcel.internal.ExceptionConstants.NEGATIVE_ARRAY_SIZE_EXCEPTION }; 91 } 92 93 /** 94 * Call corresponding visitor method(s). The order is: 95 * Call visitor methods of implemented interfaces first, then 96 * call methods according to the class hierarchy in descending order, 97 * i.e., the most specific visitXXX() call comes last. 98 * 99 * @param v Visitor object 100 */ 101 public void accept(Visitor v) { 102 v.visitAllocationInstruction(this); 103 v.visitExceptionThrower(this); 104 v.visitStackProducer(this); 105 v.visitNEWARRAY(this); 106 } 107 }