1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.interpreter.classfile;
11
12 import java.io.DataInput;
13 import java.io.IOException;
14
15 /***
16 * A Constant <br>
17 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
18 * University of Technology </a>, the Netherlands. <br>
19 * See for project information <a
20 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
21 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
22 * License (GPL) </a>, no warranty <br>
23 *
24 * @version 1.0 Jan 9, 2004 <br>
25 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
26 * Jacobs </a>
27 */
28 public abstract class Constant
29 {
30 /*** the constantPool it is part of */
31 protected Constant[] constantPool = null;
32
33 /***
34 * constructs a new Constant
35 *
36 * @param constantPool the constantPool it is part of
37 */
38 public Constant(final Constant[] constantPool)
39 {
40 super();
41 this.constantPool = constantPool;
42 }
43
44 /***
45 * returns the tag of the constant
46 *
47 * @return int the constant tag
48 */
49 public abstract int getTag();
50
51 /***
52 * reads a constant from the stream
53 *
54 * @param dataInput the dataInput
55 * @param constantPool the constantPool
56 * @return Constant
57 * @throws IOException on exception
58 */
59 public static Constant readConstant(final Constant[] constantPool,
60 final DataInput dataInput) throws IOException
61 {
62 int tag = dataInput.readUnsignedByte();
63 switch (tag)
64 {
65 case 7 :
66 return new ConstantClass(constantPool, dataInput);
67 case 9 :
68 return new ConstantFieldref(constantPool, dataInput);
69 case 10 :
70 return new ConstantMethodref(constantPool, dataInput);
71 case 11 :
72 return new ConstantInterfaceMethodref(constantPool, dataInput);
73 case 8 :
74 return new ConstantString(constantPool, dataInput);
75 case 3 :
76 return new ConstantInteger(constantPool, dataInput);
77 case 4 :
78 return new ConstantFloat(constantPool, dataInput);
79 case 5 :
80 return new ConstantLong(constantPool, dataInput);
81 case 6 :
82 return new ConstantDouble(constantPool, dataInput);
83 case 12 :
84 return new ConstantNameAndType(constantPool, dataInput);
85 case 1 :
86 return new ConstantUTF8(constantPool, dataInput);
87 default :
88 throw new IOException("unknow tag constant");
89 }
90 }
91
92 /***
93 * parses the constantPool to string
94 *
95 * @param constantPool the pool
96 * @return String
97 */
98 public static String toString(final Constant[] constantPool)
99 {
100 String result = "";
101 for (int i = 0; i < constantPool.length; i++)
102 {
103 if (constantPool[i] != null)
104 {
105 result = result + i + ": " + constantPool[i].toString() + "\n";
106 } else
107 {
108 result = result + i + ": empty \n";
109 }
110 }
111 return result;
112 }
113 }