View Javadoc

1   /*
2    * @(#) PUTSTATIC.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 PUTSTATIC operation as defined in <a
25   * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc11.html">
26   * http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc11.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  
44  public class PUTSTATIC extends VoidOperation
45  {
46  	/*** OP refers to the operand code */
47  	public static final int OP = 179;
48  
49  	/*** the index to load */
50  	private int index = -1;
51  
52  	/***
53  	 * constructs a new PUTSTATIC
54  	 * 
55  	 * @param dataInput the dataInput
56  	 * @throws IOException on IOfailure
57  	 */
58  	public PUTSTATIC(final DataInput dataInput) throws IOException
59  	{
60  		super();
61  		this.index = dataInput.readUnsignedShort();
62  	}
63  
64  	/***
65  	 * @see nl.tudelft.simulation.dsol.interpreter.operations.VoidOperation#execute(
66  	 *      nl.tudelft.simulation.dsol.interpreter.OperandStack,
67  	 *      nl.tudelft.simulation.dsol.interpreter.classfile.Constant[],
68  	 *      nl.tudelft.simulation.dsol.interpreter.LocalVariable[])
69  	 */
70  	public void execute(final OperandStack stack,
71  			final Constant[] constantPool, final LocalVariable[] localVariables)
72  	{
73  		try
74  		{
75  			ConstantFieldref constantFieldref = (ConstantFieldref) constantPool[this.index];
76  			Class referenceClass = constantFieldref.getConstantClass()
77  					.getValue().getClassValue();
78  			Field field = ClassUtil.resolveField(referenceClass,
79  					constantFieldref.getConstantNameAndType().getName());
80  			field.setAccessible(true);
81  			Object value = stack.pop();
82  			Object target = null;
83  			if (!field.getType().isPrimitive())
84  			{
85  				field.set(target, value);
86  				return;
87  			}
88  			if (field.getType().equals(boolean.class)
89  					|| field.getType().equals(byte.class)
90  					|| field.getType().equals(char.class)
91  					|| field.getType().equals(short.class)
92  					|| field.getType().equals(int.class))
93  			{
94  				field.setInt(target, ((Integer) value).intValue());
95  				return;
96  			}
97  			if (field.getType().equals(float.class))
98  			{
99  				field.setFloat(target, ((Float) value).floatValue());
100 				return;
101 			}
102 			if (field.getType().equals(long.class))
103 			{
104 				field.setLong(target, ((Long) value).longValue());
105 				return;
106 			}
107 			if (field.getType().equals(double.class))
108 			{
109 				field.setDouble(target, ((Double) value).doubleValue());
110 				return;
111 			}
112 		} catch (Exception exception)
113 		{
114 			throw new InterpreterException(exception);
115 		}
116 	}
117 
118 	/***
119 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getByteLength()
120 	 */
121 	public int getByteLength()
122 	{
123 		return OPCODE_BYTE_LENGTH + 2;
124 	}
125 
126 	/***
127 	 * @see nl.tudelft.simulation.dsol.interpreter.Operation#getOpcode()
128 	 */
129 	public int getOpcode()
130 	{
131 		return PUTSTATIC.OP;
132 	}
133 }