19 * limitations under the License. |
19 * limitations under the License. |
20 */ |
20 */ |
21 |
21 |
22 package com.sun.org.apache.bcel.internal.generic; |
22 package com.sun.org.apache.bcel.internal.generic; |
23 |
23 |
24 import java.io.*; |
24 import java.io.DataOutputStream; |
|
25 import java.io.IOException; |
|
26 |
|
27 import com.sun.org.apache.bcel.internal.ExceptionConst; |
25 import com.sun.org.apache.bcel.internal.util.ByteSequence; |
28 import com.sun.org.apache.bcel.internal.util.ByteSequence; |
26 |
29 |
27 /** |
30 /** |
28 * NEWARRAY - Create new array of basic type (int, short, ...) |
31 * NEWARRAY - Create new array of basic type (int, short, ...) |
29 * <PRE>Stack: ..., count -> ..., arrayref</PRE> |
32 * <PRE>Stack: ..., count -> ..., arrayref</PRE> |
30 * type must be one of T_INT, T_SHORT, ... |
33 * type must be one of T_INT, T_SHORT, ... |
31 * |
34 * |
32 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
35 * @version $Id: NEWARRAY.java 1747278 2016-06-07 17:28:43Z britter $ |
33 */ |
36 */ |
34 public class NEWARRAY extends Instruction |
37 public class NEWARRAY extends Instruction implements AllocationInstruction, ExceptionThrower, |
35 implements AllocationInstruction, ExceptionThrower, StackProducer { |
38 StackProducer { |
36 private byte type; |
|
37 |
39 |
38 /** |
40 private byte type; |
39 * Empty constructor needed for the Class.newInstance() statement in |
|
40 * Instruction.readInstruction(). Not to be used otherwise. |
|
41 */ |
|
42 NEWARRAY() {} |
|
43 |
41 |
44 public NEWARRAY(byte type) { |
|
45 super(com.sun.org.apache.bcel.internal.Constants.NEWARRAY, (short)2); |
|
46 this.type = type; |
|
47 } |
|
48 |
42 |
49 public NEWARRAY(BasicType type) { |
43 /** |
50 this(type.getType()); |
44 * Empty constructor needed for the Class.newInstance() statement in |
51 } |
45 * Instruction.readInstruction(). Not to be used otherwise. |
|
46 */ |
|
47 NEWARRAY() { |
|
48 } |
52 |
49 |
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 |
50 |
62 /** |
51 public NEWARRAY(final byte type) { |
63 * @return numeric code for basic element type |
52 super(com.sun.org.apache.bcel.internal.Const.NEWARRAY, (short) 2); |
64 */ |
53 this.type = type; |
65 public final byte getTypecode() { return type; } |
54 } |
66 |
55 |
67 /** |
|
68 * @return type of constructed array |
|
69 */ |
|
70 public final Type getType() { |
|
71 return new ArrayType(BasicType.getType(type), 1); |
|
72 } |
|
73 |
56 |
74 /** |
57 public NEWARRAY(final BasicType type) { |
75 * @return mnemonic for instruction |
58 this(type.getType()); |
76 */ |
59 } |
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 |
60 |
89 public Class[] getExceptions() { |
|
90 return new Class[] { com.sun.org.apache.bcel.internal.ExceptionConstants.NEGATIVE_ARRAY_SIZE_EXCEPTION }; |
|
91 } |
|
92 |
61 |
93 /** |
62 /** |
94 * Call corresponding visitor method(s). The order is: |
63 * Dump instruction as byte code to stream out. |
95 * Call visitor methods of implemented interfaces first, then |
64 * @param out Output stream |
96 * call methods according to the class hierarchy in descending order, |
65 */ |
97 * i.e., the most specific visitXXX() call comes last. |
66 @Override |
98 * |
67 public void dump( final DataOutputStream out ) throws IOException { |
99 * @param v Visitor object |
68 out.writeByte(super.getOpcode()); |
100 */ |
69 out.writeByte(type); |
101 public void accept(Visitor v) { |
70 } |
102 v.visitAllocationInstruction(this); |
71 |
103 v.visitExceptionThrower(this); |
72 |
104 v.visitStackProducer(this); |
73 /** |
105 v.visitNEWARRAY(this); |
74 * @return numeric code for basic element type |
106 } |
75 */ |
|
76 public final byte getTypecode() { |
|
77 return type; |
|
78 } |
|
79 |
|
80 |
|
81 /** |
|
82 * @return type of constructed array |
|
83 */ |
|
84 public final Type getType() { |
|
85 return new ArrayType(BasicType.getType(type), 1); |
|
86 } |
|
87 |
|
88 |
|
89 /** |
|
90 * @return mnemonic for instruction |
|
91 */ |
|
92 @Override |
|
93 public String toString( final boolean verbose ) { |
|
94 return super.toString(verbose) + " " + com.sun.org.apache.bcel.internal.Const.getTypeName(type); |
|
95 } |
|
96 |
|
97 |
|
98 /** |
|
99 * Read needed data (e.g. index) from file. |
|
100 */ |
|
101 @Override |
|
102 protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException { |
|
103 type = bytes.readByte(); |
|
104 super.setLength(2); |
|
105 } |
|
106 |
|
107 |
|
108 @Override |
|
109 public Class<?>[] getExceptions() { |
|
110 return new Class[] { |
|
111 ExceptionConst.NEGATIVE_ARRAY_SIZE_EXCEPTION |
|
112 }; |
|
113 } |
|
114 |
|
115 |
|
116 /** |
|
117 * Call corresponding visitor method(s). The order is: |
|
118 * Call visitor methods of implemented interfaces first, then |
|
119 * call methods according to the class hierarchy in descending order, |
|
120 * i.e., the most specific visitXXX() call comes last. |
|
121 * |
|
122 * @param v Visitor object |
|
123 */ |
|
124 @Override |
|
125 public void accept( final Visitor v ) { |
|
126 v.visitAllocationInstruction(this); |
|
127 v.visitExceptionThrower(this); |
|
128 v.visitStackProducer(this); |
|
129 v.visitNEWARRAY(this); |
|
130 } |
107 } |
131 } |