View Javadoc

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