KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > depend > constantpool > ConstantPoolEntry


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.depend.constantpool;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * An entry in the constant pool. This class contains a representation of the
25  * constant pool entries. It is an abstract base class for all the different
26  * forms of constant pool entry.
27  *
28  * @see ConstantPool
29  */

30 public abstract class ConstantPoolEntry {
31
32     /** Tag value for UTF8 entries. */
33     public static final int CONSTANT_UTF8 = 1;
34
35     /** Tag value for Integer entries. */
36     public static final int CONSTANT_INTEGER = 3;
37
38     /** Tag value for Float entries. */
39     public static final int CONSTANT_FLOAT = 4;
40
41     /** Tag value for Long entries. */
42     public static final int CONSTANT_LONG = 5;
43
44     /** Tag value for Double entries. */
45     public static final int CONSTANT_DOUBLE = 6;
46
47     /** Tag value for Class entries. */
48     public static final int CONSTANT_CLASS = 7;
49
50     /** Tag value for String entries. */
51     public static final int CONSTANT_STRING = 8;
52
53     /** Tag value for Field Reference entries. */
54     public static final int CONSTANT_FIELDREF = 9;
55
56     /** Tag value for Method Reference entries. */
57     public static final int CONSTANT_METHODREF = 10;
58
59     /** Tag value for Interface Method Reference entries. */
60     public static final int CONSTANT_INTERFACEMETHODREF = 11;
61
62     /** Tag value for Name and Type entries. */
63     public static final int CONSTANT_NAMEANDTYPE = 12;
64
65     /**
66      * This entry's tag which identifies the type of this constant pool
67      * entry.
68      */

69     private int tag;
70
71     /**
72      * The number of slots in the constant pool, occupied by this entry.
73      */

74     private int numEntries;
75
76     /**
77      * A flag which indicates if this entry has been resolved or not.
78      */

79     private boolean resolved;
80
81     /**
82      * Initialise the constant pool entry.
83      *
84      * @param tagValue the tag value which identifies which type of constant
85      * pool entry this is.
86      * @param entries the number of constant pool entry slots this entry
87      * occupies.
88      */

89     public ConstantPoolEntry(int tagValue, int entries) {
90         tag = tagValue;
91         numEntries = entries;
92         resolved = false;
93     }
94
95     /**
96      * Read a constant pool entry from a stream. This is a factory method
97      * which reads a constant pool entry form a stream and returns the
98      * appropriate subclass for the entry.
99      *
100      * @param cpStream the stream from which the constant pool entry is to
101      * be read.
102      * @return the appropriate ConstantPoolEntry subclass representing the
103      * constant pool entry from the stream.
104      * @exception IOException if the constant pool entry cannot be read
105      * from the stream
106      */

107     public static ConstantPoolEntry readEntry(DataInputStream JavaDoc cpStream)
108          throws IOException JavaDoc {
109         ConstantPoolEntry cpInfo = null;
110         int cpTag = cpStream.readUnsignedByte();
111
112         switch (cpTag) {
113
114             case CONSTANT_UTF8:
115                 cpInfo = new Utf8CPInfo();
116
117                 break;
118             case CONSTANT_INTEGER:
119                 cpInfo = new IntegerCPInfo();
120
121                 break;
122             case CONSTANT_FLOAT:
123                 cpInfo = new FloatCPInfo();
124
125                 break;
126             case CONSTANT_LONG:
127                 cpInfo = new LongCPInfo();
128
129                 break;
130             case CONSTANT_DOUBLE:
131                 cpInfo = new DoubleCPInfo();
132
133                 break;
134             case CONSTANT_CLASS:
135                 cpInfo = new ClassCPInfo();
136
137                 break;
138             case CONSTANT_STRING:
139                 cpInfo = new StringCPInfo();
140
141                 break;
142             case CONSTANT_FIELDREF:
143                 cpInfo = new FieldRefCPInfo();
144
145                 break;
146             case CONSTANT_METHODREF:
147                 cpInfo = new MethodRefCPInfo();
148
149                 break;
150             case CONSTANT_INTERFACEMETHODREF:
151                 cpInfo = new InterfaceMethodRefCPInfo();
152
153                 break;
154             case CONSTANT_NAMEANDTYPE:
155                 cpInfo = new NameAndTypeCPInfo();
156
157                 break;
158             default:
159                 throw new ClassFormatError JavaDoc("Invalid Constant Pool entry Type "
160                      + cpTag);
161         }
162
163         cpInfo.read(cpStream);
164
165         return cpInfo;
166     }
167
168     /**
169      * Indicates whether this entry has been resolved. In general a constant
170      * pool entry can reference another constant pool entry by its index
171      * value. Resolution involves replacing this index value with the
172      * constant pool entry at that index.
173      *
174      * @return true if this entry has been resolved.
175      */

176     public boolean isResolved() {
177         return resolved;
178     }
179
180     /**
181      * Resolve this constant pool entry with respect to its dependents in
182      * the constant pool.
183      *
184      * @param constantPool the constant pool of which this entry is a member
185      * and against which this entry is to be resolved.
186      */

187     public void resolve(ConstantPool constantPool) {
188         resolved = true;
189     }
190
191     /**
192      * read a constant pool entry from a class stream.
193      *
194      * @param cpStream the DataInputStream which contains the constant pool
195      * entry to be read.
196      * @exception IOException if there is a problem reading the entry from
197      * the stream.
198      */

199     public abstract void read(DataInputStream JavaDoc cpStream) throws IOException JavaDoc;
200
201     /**
202      * Get the Entry's type tag.
203      *
204      * @return The Tag value of this entry
205      */

206     public int getTag() {
207         return tag;
208     }
209
210     /**
211      * Get the number of Constant Pool Entry slots within the constant pool
212      * occupied by this entry.
213      *
214      * @return the number of slots used.
215      */

216     public final int getNumEntries() {
217         return numEntries;
218     }
219
220 }
221
222
Popular Tags