View Javadoc
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, ...] -&gt; ..., arrayref</PRE>
30   *
31   */
32  public class MULTIANEWARRAY extends CPInstruction implements LoadClass, AllocationInstruction,
33          ExceptionThrower {
34  
35      private short dimensions;
36  
37  
38      /**
39       * Empty constructor needed for Instruction.readInstruction.
40       * Not to be used otherwise.
41       */
42      MULTIANEWARRAY() {
43      }
44  
45  
46      public MULTIANEWARRAY(final int index, final short dimensions) {
47          super(org.apache.bcel.Const.MULTIANEWARRAY, index);
48          if (dimensions < 1) {
49              throw new ClassGenException("Invalid dimensions value: " + dimensions);
50          }
51          this.dimensions = dimensions;
52          super.setLength(4);
53      }
54  
55  
56      /**
57       * Dump instruction as byte code to stream out.
58       * @param out Output stream
59       */
60      @Override
61      public void dump( final DataOutputStream out ) throws IOException {
62          out.writeByte(super.getOpcode());
63          out.writeShort(super.getIndex());
64          out.writeByte(dimensions);
65      }
66  
67  
68      /**
69       * Read needed data (i.e., no. dimension) from file.
70       */
71      @Override
72      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
73          super.initFromFile(bytes, wide);
74          dimensions = bytes.readByte();
75          super.setLength(4);
76      }
77  
78  
79      /**
80       * @return number of dimensions to be created
81       */
82      public final short getDimensions() {
83          return dimensions;
84      }
85  
86  
87      /**
88       * @return mnemonic for instruction
89       */
90      @Override
91      public String toString( final boolean verbose ) {
92          return super.toString(verbose) + " " + super.getIndex() + " " + dimensions;
93      }
94  
95  
96      /**
97       * @return mnemonic for instruction with symbolic references resolved
98       */
99      @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="../../../../org/apache/bcel/generic/ObjectType.html#ObjectType">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 }