KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > CPInfo


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.structures;
9
10 import org.gjt.jclasslib.structures.constants.*;
11
12 import java.io.DataInput JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * Base class for all constant pool entries in the <tt>constants</tt> package.
17  *
18  * @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>, <a HREF="mailto:vitor.carreira@gmail.com">Vitor Carreira</a>
19  * @version $Revision: 1.6 $ $Date: 2004/12/28 13:04:32 $
20  */

21 public abstract class CPInfo extends AbstractStructure {
22
23     public static final byte CONSTANT_CLASS = 7;
24     public static final byte CONSTANT_FIELDREF = 9;
25     public static final byte CONSTANT_METHODREF = 10;
26     public static final byte CONSTANT_INTERFACE_METHODREF = 11;
27     public static final byte CONSTANT_STRING = 8;
28     public static final byte CONSTANT_INTEGER = 3;
29     public static final byte CONSTANT_FLOAT = 4;
30     public static final byte CONSTANT_LONG = 5;
31     public static final byte CONSTANT_DOUBLE = 6;
32     public static final byte CONSTANT_NAME_AND_TYPE = 12;
33     public static final byte CONSTANT_UTF8 = 1;
34
35     public static final String JavaDoc CONSTANT_CLASS_VERBOSE = "CONSTANT_Class_info";
36     public static final String JavaDoc CONSTANT_FIELDREF_VERBOSE = "CONSTANT_Fieldref_info";
37     public static final String JavaDoc CONSTANT_METHODREF_VERBOSE = "CONSTANT_Methodref_info";
38     public static final String JavaDoc CONSTANT_INTERFACE_METHODREF_VERBOSE = "CONSTANT_InterfaceMethodref_info";
39     public static final String JavaDoc CONSTANT_STRING_VERBOSE = "CONSTANT_String_info";
40     public static final String JavaDoc CONSTANT_INTEGER_VERBOSE = "CONSTANT_Integer_info";
41     public static final String JavaDoc CONSTANT_FLOAT_VERBOSE = "CONSTANT_Float_info";
42     public static final String JavaDoc CONSTANT_LONG_VERBOSE = "CONSTANT_Long_info";
43     public static final String JavaDoc CONSTANT_DOUBLE_VERBOSE = "CONSTANT_Double_info";
44     public static final String JavaDoc CONSTANT_NAME_AND_TYPE_VERBOSE = "CONSTANT_NameAndType_info";
45     public static final String JavaDoc CONSTANT_UTF8_VERBOSE = "CONSTANT_Utf8_info";
46
47     /**
48      * Factory method for creating <tt>CPInfo</tt> structures. <p>
49      * A <tt>CPInfo</tt> of the appropriate subtype from the <tt>constants</tt> package
50      * is created. <p>
51      *
52      * @param in the <tt>DataInput</tt> from which to read the <tt>CPInfo</tt> structure
53      * @param classFile the parent class file of the structure to be created
54      * @return the new <tt>CPInfo</tt> structure
55      * @throws InvalidByteCodeException if the byte code is invalid
56      * @throws IOException if an exception occurs with the <tt>DataInput</tt>
57      */

58     public static CPInfo create(DataInput JavaDoc in, ClassFile classFile)
59             throws InvalidByteCodeException, IOException JavaDoc {
60
61         CPInfo cpInfo;
62
63         byte tag = in.readByte();
64
65         switch (tag) {
66             case CONSTANT_CLASS:
67                 cpInfo = new ConstantClassInfo();
68                 break;
69             case CONSTANT_FIELDREF:
70                 cpInfo = new ConstantFieldrefInfo();
71                 break;
72             case CONSTANT_METHODREF:
73                 cpInfo = new ConstantMethodrefInfo();
74                 break;
75             case CONSTANT_INTERFACE_METHODREF:
76                 cpInfo = new ConstantInterfaceMethodrefInfo();
77                 break;
78             case CONSTANT_STRING:
79                 cpInfo = new ConstantStringInfo();
80                 break;
81             case CONSTANT_INTEGER:
82                 cpInfo = new ConstantIntegerInfo();
83                 break;
84             case CONSTANT_FLOAT:
85                 cpInfo = new ConstantFloatInfo();
86                 break;
87             case CONSTANT_LONG:
88                 cpInfo = new ConstantLongInfo();
89                 break;
90             case CONSTANT_DOUBLE:
91                 cpInfo = new ConstantDoubleInfo();
92                 break;
93             case CONSTANT_NAME_AND_TYPE:
94                 cpInfo = new ConstantNameAndTypeInfo();
95                 break;
96             case CONSTANT_UTF8:
97                 cpInfo = new ConstantUtf8Info();
98                 break;
99             default:
100                 throw new InvalidByteCodeException("invalid constant pool entry with unknown tag " + tag);
101         }
102         cpInfo.setClassFile(classFile);
103         cpInfo.read(in);
104
105         return cpInfo;
106     }
107
108
109     /**
110      * Get the value of the <tt>tag</tt> field of the <tt>cp_info</tt> structure.
111      *
112      * @return the tag
113      */

114     public abstract byte getTag();
115
116     /**
117      * Get the verbose description of the <tt>tag</tt> field of the
118      * <tt>cp_info</tt> structure.
119      *
120      * @return the verbose description
121      */

122     public abstract String JavaDoc getTagVerbose();
123
124     /**
125      * Get the verbose description of the content of the constant pool entry.
126      *
127      * @return the verbose description
128      * @throws InvalidByteCodeException if the byte code is invalid
129      */

130     public String JavaDoc getVerbose() throws InvalidByteCodeException {
131         return "";
132     }
133
134     /**
135      * Skip a <tt>CPInfo</tt> structure in a <tt>DataInput</tt>. <p>
136      *
137      * @param in the <tt>DataInput</tt> from which to read the <tt>CPInfo</tt> structure
138      * @return the number of bytes skipped
139      * @throws InvalidByteCodeException if the byte code is invalid
140      * @throws IOException if an exception occurs with the <tt>DataInput</tt>
141      */

142     public static int skip(DataInput JavaDoc in)
143             throws InvalidByteCodeException, IOException JavaDoc {
144
145         int offset = 0;
146
147         byte tag = in.readByte();
148
149         switch (tag) {
150             case CONSTANT_CLASS:
151                 in.skipBytes(ConstantClassInfo.SIZE);
152                 break;
153             case CONSTANT_FIELDREF:
154             case CONSTANT_METHODREF:
155             case CONSTANT_INTERFACE_METHODREF:
156                 in.skipBytes(ConstantReference.SIZE);
157                 break;
158             case CONSTANT_STRING:
159                 in.skipBytes(ConstantStringInfo.SIZE);
160                 break;
161             case CONSTANT_INTEGER:
162             case CONSTANT_FLOAT:
163                 in.skipBytes(ConstantNumeric.SIZE);
164                 break;
165             case CONSTANT_LONG:
166             case CONSTANT_DOUBLE:
167                 in.skipBytes(ConstantLargeNumeric.SIZE);
168                 offset = 1;
169                 break;
170             case CONSTANT_NAME_AND_TYPE:
171                 in.skipBytes(ConstantNameAndTypeInfo.SIZE);
172                 break;
173             case CONSTANT_UTF8:
174                 // Length of the constant is determined by the length of the byte array
175
in.skipBytes(in.readUnsignedShort());
176                 break;
177             default:
178                 throw new InvalidByteCodeException("invalid constant pool entry with unknown tag " + tag);
179         }
180
181         return offset;
182     }
183
184     public boolean equals(Object JavaDoc object) {
185         return object instanceof CPInfo;
186     }
187
188     public int hashCode() {
189         return 0;
190     }
191
192     protected String JavaDoc printAccessFlagsVerbose(int accessFlags) {
193         if (accessFlags != 0)
194             throw new RuntimeException JavaDoc("Access flags should be zero: " + Integer.toHexString(accessFlags));
195         return "";
196     }
197 }
198
Popular Tags