View Javadoc

1   /*
2    * @(#) NEW.java Jan 8, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.interpreter.operations;
11  
12  import java.io.DataInput;
13  import java.io.IOException;
14  
15  import nl.tudelft.simulation.dsol.interpreter.InterpreterException;
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  import nl.tudelft.simulation.dsol.interpreter.classfile.ConstantClass;
20  
21  /***
22   * The NEW operation as defined in <a
23   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc10.html">
24   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc10.html
25   * </a>.
26   * <p>
27   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
28   * University of Technology </a>, the Netherlands. <br>
29   * See for project information <a
30   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
31   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
32   * License (GPL) </a>, no warranty <br>
33   * 
34   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
35   *         Jacobs </a><a
36   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
37   *         Verbraeck </a>
38   * @version 1.3 Apr 6, 2004
39   * @since 1.4
40   */
41  public class NEW extends VoidOperation
42  {
43  	/*** OP refers to the operand code */
44  	public static final int OP = 187;
45  
46  	/*** index refers to the constantpool index */
47  	private int index = -1;
48  
49  	/***
50  	 * constructs a new NEW
51  	 * 
52  	 * @param dataInput the dataInput
53  	 * @throws IOException on IOfailure
54  	 */
55  	public NEW(final DataInput dataInput) throws IOException
56  	{
57  		super();
58  		this.index = dataInput.readUnsignedShort();
59  	}
60  
61  	/***
62  	 * @see nl.tudelft.simulation.dsol.interpreter.operations.VoidOperation#execute(
63  	 *      nl.tudelft.simulation.dsol.interpreter.OperandStack,
64  	 *      nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
65  	 *      nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
66  	 */
67  	public void execute(final OperandStack stack,
68  			final Constant[] constantPool, final LocalVariable[] localVariables)
69  	{
70  		try
71  		{
72  			Class instanceClass = ((ConstantClass) constantPool[this.index])
73  					.getValue().getClassValue();
74  			stack.push(new UninitializedInstance(instanceClass));
75  		} catch (Exception exception)
76  		{
77  			throw new InterpreterException(exception);
78  		}
79  	}
80  
81  	/***
82  	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
83  	 */
84  	public int getByteLength()
85  	{
86  		return OPCODE_BYTE_LENGTH + 2;
87  	}
88  
89  	/***
90  	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
91  	 */
92  	public int getOpcode()
93  	{
94  		return NEW.OP;
95  	}
96  
97  	/***
98  	 * holder class for "to-be-constructed" instances
99  	 */
100 	public static class UninitializedInstance
101 	{
102 		/*** the value */
103 		private Class instanceClass = null;
104 
105 		/***
106 		 * constructs a new UninitializedInstance
107 		 * 
108 		 * @param instanceClass the class of which an instance must be made
109 		 */
110 		public UninitializedInstance(final Class instanceClass)
111 		{
112 			this.instanceClass = instanceClass;
113 		}
114 
115 		/***
116 		 * @return return the instanceClass
117 		 */
118 		public Class getInstanceClass()
119 		{
120 			return this.instanceClass;
121 		}
122 	}
123 }