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.InterpreterException;
17 import nl.tudelft.simulation.dsol.interpreter.LocalVariable;
18 import nl.tudelft.simulation.dsol.interpreter.OperandStack;
19 import nl.tudelft.simulation.dsol.interpreter.classfile.Constant;
20 import nl.tudelft.simulation.dsol.interpreter.classfile.ConstantClass;
21 import nl.tudelft.simulation.language.primitives.Primitive;
22
23 /***
24 * The ANEWARRAY operation as defined in <a
25 * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html">
26 * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc.html
27 * </a>.
28 * <p>
29 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
30 * University of Technology </a>, the Netherlands. <br>
31 * See for project information <a
32 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
33 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
34 * License (GPL) </a>, no warranty <br>
35 *
36 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
37 * Jacobs </a><a
38 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
39 * Verbraeck </a>
40 * @version 1.3 Apr 6, 2004
41 * @since 1.4
42 */
43 public class ANEWARRAY extends VoidOperation
44 {
45 /*** OP refers to the operand code */
46 public static final int OP = 189;
47
48 /*** the index to load */
49 private int index = -1;
50
51 /***
52 * constructs a new ANEWARRAY
53 *
54 * @param dataInput the dataInput
55 * @throws IOException on IOfailure
56 */
57 public ANEWARRAY(final DataInput dataInput) throws IOException
58 {
59 super();
60 this.index = dataInput.readUnsignedShort();
61 }
62
63 /***
64 * @see nl.tudelft.simulation.dsol.interpreter.operations.VoidOperation
65 * #execute(nl.tudelft.simulation.dsol.interpreter.OperandStack,
66 * nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
67 * nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
68 */
69 public void execute(final OperandStack stack,
70 final Constant[] constantPool, final LocalVariable[] localVariables)
71 {
72 int length = Primitive.toInteger(stack.pop()).intValue();
73 Class clazz = null;
74 try
75 {
76 clazz = ((ConstantClass) constantPool[this.index]).getValue()
77 .getClassValue();
78 } catch (ClassNotFoundException exception)
79 {
80 throw new InterpreterException(exception);
81 }
82 Object array = Array.newInstance(clazz, length);
83 stack.push(array);
84 }
85
86 /***
87 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
88 */
89 public int getByteLength()
90 {
91 return OPCODE_BYTE_LENGTH + 2;
92 }
93
94 /***
95 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
96 */
97 public int getOpcode()
98 {
99 return ANEWARRAY.OP;
100 }
101 }