001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.depend.constantpool;
019:
020: import java.io.DataInputStream;
021: import java.io.IOException;
022:
023: /**
024: * An entry in the constant pool. This class contains a representation of the
025: * constant pool entries. It is an abstract base class for all the different
026: * forms of constant pool entry.
027: *
028: * @see ConstantPool
029: */
030: public abstract class ConstantPoolEntry {
031:
032: /** Tag value for UTF8 entries. */
033: public static final int CONSTANT_UTF8 = 1;
034:
035: /** Tag value for Integer entries. */
036: public static final int CONSTANT_INTEGER = 3;
037:
038: /** Tag value for Float entries. */
039: public static final int CONSTANT_FLOAT = 4;
040:
041: /** Tag value for Long entries. */
042: public static final int CONSTANT_LONG = 5;
043:
044: /** Tag value for Double entries. */
045: public static final int CONSTANT_DOUBLE = 6;
046:
047: /** Tag value for Class entries. */
048: public static final int CONSTANT_CLASS = 7;
049:
050: /** Tag value for String entries. */
051: public static final int CONSTANT_STRING = 8;
052:
053: /** Tag value for Field Reference entries. */
054: public static final int CONSTANT_FIELDREF = 9;
055:
056: /** Tag value for Method Reference entries. */
057: public static final int CONSTANT_METHODREF = 10;
058:
059: /** Tag value for Interface Method Reference entries. */
060: public static final int CONSTANT_INTERFACEMETHODREF = 11;
061:
062: /** Tag value for Name and Type entries. */
063: public static final int CONSTANT_NAMEANDTYPE = 12;
064:
065: /**
066: * This entry's tag which identifies the type of this constant pool
067: * entry.
068: */
069: private int tag;
070:
071: /**
072: * The number of slots in the constant pool, occupied by this entry.
073: */
074: private int numEntries;
075:
076: /**
077: * A flag which indicates if this entry has been resolved or not.
078: */
079: private boolean resolved;
080:
081: /**
082: * Initialise the constant pool entry.
083: *
084: * @param tagValue the tag value which identifies which type of constant
085: * pool entry this is.
086: * @param entries the number of constant pool entry slots this entry
087: * occupies.
088: */
089: public ConstantPoolEntry(int tagValue, int entries) {
090: tag = tagValue;
091: numEntries = entries;
092: resolved = false;
093: }
094:
095: /**
096: * Read a constant pool entry from a stream. This is a factory method
097: * which reads a constant pool entry form a stream and returns the
098: * appropriate subclass for the entry.
099: *
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 cpStream)
108: throws IOException {
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(
160: "Invalid Constant Pool entry Type " + 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 cpStream)
200: throws IOException;
201:
202: /**
203: * Get the Entry's type tag.
204: *
205: * @return The Tag value of this entry
206: */
207: public int getTag() {
208: return tag;
209: }
210:
211: /**
212: * Get the number of Constant Pool Entry slots within the constant pool
213: * occupied by this entry.
214: *
215: * @return the number of slots used.
216: */
217: public final int getNumEntries() {
218: return numEntries;
219: }
220:
221: }
|