View Javadoc

1   /*
2    * @(#) GETFIELD.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.reflection.ClassUtil;
22  
23  /***
24   * The GETFIELD operation as defined in <a
25   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc5.html">
26   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc5.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 GETFIELD extends VoidOperation
44  {
45  	/*** OP refers to the operand code */
46  	public static final int OP = 180;
47  
48  	/*** the index to load */
49  	private int index = -1;
50  
51  	/***
52  	 * constructs a new GETFIELD
53  	 * 
54  	 * @param dataInput the dataInput
55  	 * @throws IOException on IOfailure
56  	 */
57  	public GETFIELD(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  		try
73  		{
74  			ConstantFieldref constantFieldref = (ConstantFieldref) constantPool[this.index];
75  			Object objectRef = stack.pop();
76  			Field field = ClassUtil.resolveField(objectRef, constantFieldref
77  					.getConstantNameAndType().getName());
78  			field.setAccessible(true);
79  			stack.push(field.get(objectRef));
80  		} catch (Exception exception)
81  		{
82  			throw new InterpreterException(exception);
83  		}
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 GETFIELD.OP;
100 	}
101 }