1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.depend.constantpool;
  18. import java.io.DataInputStream;
  19. import java.io.IOException;
  20. /**
  21. * An entry in the constant pool. This class contains a representation of the
  22. * constant pool entries. It is an abstract base class for all the different
  23. * forms of constant pool entry.
  24. *
  25. * @see ConstantPool
  26. */
  27. public abstract class ConstantPoolEntry {
  28. /** Tag value for UTF8 entries. */
  29. public static final int CONSTANT_UTF8 = 1;
  30. /** Tag value for Integer entries. */
  31. public static final int CONSTANT_INTEGER = 3;
  32. /** Tag value for Float entries. */
  33. public static final int CONSTANT_FLOAT = 4;
  34. /** Tag value for Long entries. */
  35. public static final int CONSTANT_LONG = 5;
  36. /** Tag value for Double entries. */
  37. public static final int CONSTANT_DOUBLE = 6;
  38. /** Tag value for Class entries. */
  39. public static final int CONSTANT_CLASS = 7;
  40. /** Tag value for String entries. */
  41. public static final int CONSTANT_STRING = 8;
  42. /** Tag value for Field Reference entries. */
  43. public static final int CONSTANT_FIELDREF = 9;
  44. /** Tag value for Method Reference entries. */
  45. public static final int CONSTANT_METHODREF = 10;
  46. /** Tag value for Interface Method Reference entries. */
  47. public static final int CONSTANT_INTERFACEMETHODREF = 11;
  48. /** Tag value for Name and Type entries. */
  49. public static final int CONSTANT_NAMEANDTYPE = 12;
  50. /**
  51. * This entry's tag which identifies the type of this constant pool
  52. * entry.
  53. */
  54. private int tag;
  55. /**
  56. * The number of slots in the constant pool, occupied by this entry.
  57. */
  58. private int numEntries;
  59. /**
  60. * A flag which indicates if this entry has been resolved or not.
  61. */
  62. private boolean resolved;
  63. /**
  64. * Initialise the constant pool entry.
  65. *
  66. * @param tagValue the tag value which identifies which type of constant
  67. * pool entry this is.
  68. * @param entries the number of constant pool entry slots this entry
  69. * occupies.
  70. */
  71. public ConstantPoolEntry(int tagValue, int entries) {
  72. tag = tagValue;
  73. numEntries = entries;
  74. resolved = false;
  75. }
  76. /**
  77. * Read a constant pool entry from a stream. This is a factory method
  78. * which reads a constant pool entry form a stream and returns the
  79. * appropriate subclass for the entry.
  80. *
  81. * @param cpStream the stream from which the constant pool entry is to
  82. * be read.
  83. * @return the appropriate ConstantPoolEntry subclass representing the
  84. * constant pool entry from the stream.
  85. * @exception IOException if the constant pool entry cannot be read
  86. * from the stream
  87. */
  88. public static ConstantPoolEntry readEntry(DataInputStream cpStream)
  89. throws IOException {
  90. ConstantPoolEntry cpInfo = null;
  91. int cpTag = cpStream.readUnsignedByte();
  92. switch (cpTag) {
  93. case CONSTANT_UTF8:
  94. cpInfo = new Utf8CPInfo();
  95. break;
  96. case CONSTANT_INTEGER:
  97. cpInfo = new IntegerCPInfo();
  98. break;
  99. case CONSTANT_FLOAT:
  100. cpInfo = new FloatCPInfo();
  101. break;
  102. case CONSTANT_LONG:
  103. cpInfo = new LongCPInfo();
  104. break;
  105. case CONSTANT_DOUBLE:
  106. cpInfo = new DoubleCPInfo();
  107. break;
  108. case CONSTANT_CLASS:
  109. cpInfo = new ClassCPInfo();
  110. break;
  111. case CONSTANT_STRING:
  112. cpInfo = new StringCPInfo();
  113. break;
  114. case CONSTANT_FIELDREF:
  115. cpInfo = new FieldRefCPInfo();
  116. break;
  117. case CONSTANT_METHODREF:
  118. cpInfo = new MethodRefCPInfo();
  119. break;
  120. case CONSTANT_INTERFACEMETHODREF:
  121. cpInfo = new InterfaceMethodRefCPInfo();
  122. break;
  123. case CONSTANT_NAMEANDTYPE:
  124. cpInfo = new NameAndTypeCPInfo();
  125. break;
  126. default:
  127. throw new ClassFormatError("Invalid Constant Pool entry Type "
  128. + cpTag);
  129. }
  130. cpInfo.read(cpStream);
  131. return cpInfo;
  132. }
  133. /**
  134. * Indicates whether this entry has been resolved. In general a constant
  135. * pool entry can reference another constant pool entry by its index
  136. * value. Resolution involves replacing this index value with the
  137. * constant pool entry at that index.
  138. *
  139. * @return true if this entry has been resolved.
  140. */
  141. public boolean isResolved() {
  142. return resolved;
  143. }
  144. /**
  145. * Resolve this constant pool entry with respect to its dependents in
  146. * the constant pool.
  147. *
  148. * @param constantPool the constant pool of which this entry is a member
  149. * and against which this entry is to be resolved.
  150. */
  151. public void resolve(ConstantPool constantPool) {
  152. resolved = true;
  153. }
  154. /**
  155. * read a constant pool entry from a class stream.
  156. *
  157. * @param cpStream the DataInputStream which contains the constant pool
  158. * entry to be read.
  159. * @exception IOException if there is a problem reading the entry from
  160. * the stream.
  161. */
  162. public abstract void read(DataInputStream cpStream) throws IOException;
  163. /**
  164. * Get the Entry's type tag.
  165. *
  166. * @return The Tag value of this entry
  167. */
  168. public int getTag() {
  169. return tag;
  170. }
  171. /**
  172. * Get the number of Constant Pool Entry slots within the constant pool
  173. * occupied by this entry.
  174. *
  175. * @return the number of slots used.
  176. */
  177. public final int getNumEntries() {
  178. return numEntries;
  179. }
  180. }