1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.interpreter.operations;
11
12 import java.io.DataInput;
13 import java.io.IOException;
14 import java.lang.reflect.Array;
15
16 import nl.tudelft.simulation.dsol.interpreter.LocalVariable;
17 import nl.tudelft.simulation.dsol.interpreter.OperandStack;
18 import nl.tudelft.simulation.dsol.interpreter.classfile.Constant;
19
20 /***
21 * The NEWARRAY operation as defined in <a
22 * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc10.html">
23 * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc10.html
24 * </a>.
25 * <p>
26 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
27 * University of Technology </a>, the Netherlands. <br>
28 * See for project information <a
29 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31 * License (GPL) </a>, no warranty <br>
32 *
33 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
34 * Jacobs </a><a
35 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
36 * Verbraeck </a>
37 * @version 1.3 Apr 6, 2004
38 * @since 1.4
39 */
40 public class NEWARRAY extends VoidOperation
41 {
42 /*** OP refers to the operand code */
43 public static final int OP = 188;
44
45 /*** the type to load */
46 private int atype = -1;
47
48 /***
49 * constructs a new NEWARRAY
50 *
51 * @param dataInput the dataInput
52 * @throws IOException on IOfailure
53 */
54 public NEWARRAY(final DataInput dataInput) throws IOException
55 {
56 super();
57 this.atype = dataInput.readUnsignedByte();
58 }
59
60 /***
61 * @see nl.tudelft.simulation.dsol.interpreter.operations.VoidOperation#execute(
62 * nl.tudelft.simulation.dsol.interpreter.OperandStack,
63 * nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
64 * nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
65 */
66 public void execute(final OperandStack stack,
67 final Constant[] constantPool, final LocalVariable[] localVariables)
68 {
69 int length = ((Integer) stack.pop()).intValue();
70 Class clazz = null;
71 switch (this.atype)
72 {
73 case 4 :
74 clazz = boolean.class;
75 break;
76 case 5 :
77 clazz = char.class;
78 break;
79 case 6 :
80 clazz = float.class;
81 break;
82 case 7 :
83 clazz = double.class;
84 break;
85 case 8 :
86 clazz = byte.class;
87 break;
88 case 9 :
89 clazz = short.class;
90 break;
91 case 10 :
92 clazz = int.class;
93 break;
94 case 11 :
95 clazz = long.class;
96 break;
97 default :
98 throw new RuntimeException("unknown atype in NEWARRAY");
99 }
100 Object array = Array.newInstance(clazz, length);
101 stack.push(array);
102 }
103
104 /***
105 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
106 */
107 public int getByteLength()
108 {
109 return OPCODE_BYTE_LENGTH + 1;
110 }
111
112 /***
113 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
114 */
115 public int getOpcode()
116 {
117 return NEWARRAY.OP;
118 }
119 }