View Javadoc

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