KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > classfile > Constant


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle.classfile;
26
27 import java.io.DataInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Abstract super class of all type of constants in the constant pool of
32  * a class file.
33  *
34  * @author Franz-Josef Elmer
35  */

36 public abstract class Constant {
37   private static final int MAGIC = 0xcafebabe;
38   private static final int CONSTANT_CLASS = 7,
39                            CONSTANT_FIELDREF = 9,
40                            CONSTANT_METHODREF = 10,
41                            CONSTANT_INTERFACE_METHODREF = 11,
42                            CONSTANT_STRING = 8,
43                            CONSTANT_INTEGER = 3,
44                            CONSTANT_FLOAT = 4,
45                            CONSTANT_LONG = 5,
46                            CONSTANT_DOUBLE = 6,
47                            CONSTANT_NAME_AND_TYPE = 12,
48                            CONSTANT_UTF8 = 1;
49   
50   /**
51    * Extracts the constant pool from the specified data stream of a class file.
52    * @param stream Input stream of a class file starting at the first byte.
53    * @return extracted array of constants.
54    * @throws IOException in case of reading errors or invalid class file.
55    */

56   public static Constant[] extractConstantPool(DataInputStream JavaDoc stream)
57                                                     throws IOException JavaDoc {
58     Constant[] pool = null;
59     if (stream.readInt() == MAGIC) {
60       stream.readUnsignedShort();
61       stream.readUnsignedShort();
62       pool = new Constant[stream.readUnsignedShort()];
63       for (int i = 1; i < pool.length; ) {
64         boolean skipIndex = false;
65         Constant c = null;
66         int type = stream.readUnsignedByte();
67         switch (type) {
68         case CONSTANT_CLASS:
69           c = new ClassConstant(pool, stream.readUnsignedShort());
70           break;
71         case CONSTANT_FIELDREF:
72           c = new FieldRefConstant(pool, stream.readUnsignedShort(),
73                                    stream.readUnsignedShort());
74           break;
75         case CONSTANT_METHODREF:
76           c = new MethodRefConstant(pool, stream.readUnsignedShort(),
77                                     stream.readUnsignedShort());
78           break;
79         case CONSTANT_INTERFACE_METHODREF:
80           c = new InterfaceMethodRefConstant(pool,
81                                              stream.readUnsignedShort(),
82                                              stream.readUnsignedShort());
83           break;
84         case CONSTANT_STRING:
85           c = new StringConstant(pool, stream.readUnsignedShort());
86           break;
87         case CONSTANT_INTEGER:
88           c = new IntConstant(pool, stream.readInt());
89           break;
90         case CONSTANT_FLOAT:
91           c = new FloatConstant(pool, stream.readFloat());
92           break;
93         case CONSTANT_LONG:
94           c = new LongConstant(pool, stream.readLong());
95           skipIndex = true;
96           break;
97         case CONSTANT_DOUBLE:
98           c = new DoubleConstant(pool, stream.readDouble());
99           skipIndex = true;
100           break;
101         case CONSTANT_NAME_AND_TYPE:
102           c = new NameAndTypeConstant(pool, stream.readUnsignedShort(),
103                                       stream.readUnsignedShort());
104           break;
105         case CONSTANT_UTF8:
106           c = new UTF8Constant(pool, stream.readUTF());
107           break;
108         }
109         pool[i] = c;
110         i += skipIndex ? 2 : 1; // double and long constants occupy two entries
111
}
112       return pool;
113     }
114     throw new IOException JavaDoc("Not a class file: Magic number missing.");
115   }
116
117   private Constant[] _pool;
118
119   /**
120    * Creates an instance.
121    * @param pool The poole which will be needed to resolve references.
122    */

123   public Constant(Constant[] pool) {
124     _pool = pool;
125   }
126
127   /**
128    * Returns the specified constant from the pool.
129    * @param index Index of requested constant.
130    */

131   public Constant getConstant(int index) {
132     return _pool[index];
133   }
134 } //class
Popular Tags