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. * A FieldRef CP Info
  22. *
  23. */
  24. public class FieldRefCPInfo extends ConstantPoolEntry {
  25. /** Name of the field's class */
  26. private String fieldClassName;
  27. /** name of the field in that class */
  28. private String fieldName;
  29. /** The type of the field */
  30. private String fieldType;
  31. /** Index into the constant pool for the class */
  32. private int classIndex;
  33. /** Index into the constant pool for the name and type entry */
  34. private int nameAndTypeIndex;
  35. /** Constructor. */
  36. public FieldRefCPInfo() {
  37. super(CONSTANT_FIELDREF, 1);
  38. }
  39. /**
  40. * read a constant pool entry from a class stream.
  41. *
  42. * @param cpStream the DataInputStream which contains the constant pool
  43. * entry to be read.
  44. * @exception IOException if there is a problem reading the entry from
  45. * the stream.
  46. */
  47. public void read(DataInputStream cpStream) throws IOException {
  48. classIndex = cpStream.readUnsignedShort();
  49. nameAndTypeIndex = cpStream.readUnsignedShort();
  50. }
  51. /**
  52. * Resolve this constant pool entry with respect to its dependents in
  53. * the constant pool.
  54. *
  55. * @param constantPool the constant pool of which this entry is a member
  56. * and against which this entry is to be resolved.
  57. */
  58. public void resolve(ConstantPool constantPool) {
  59. ClassCPInfo fieldClass
  60. = (ClassCPInfo) constantPool.getEntry(classIndex);
  61. fieldClass.resolve(constantPool);
  62. fieldClassName = fieldClass.getClassName();
  63. NameAndTypeCPInfo nt
  64. = (NameAndTypeCPInfo) constantPool.getEntry(nameAndTypeIndex);
  65. nt.resolve(constantPool);
  66. fieldName = nt.getName();
  67. fieldType = nt.getType();
  68. super.resolve(constantPool);
  69. }
  70. /**
  71. * Print a readable version of the constant pool entry.
  72. *
  73. * @return the string representation of this constant pool entry.
  74. */
  75. public String toString() {
  76. String value;
  77. if (isResolved()) {
  78. value = "Field : Class = " + fieldClassName + ", name = "
  79. + fieldName + ", type = " + fieldType;
  80. } else {
  81. value = "Field : Class index = " + classIndex
  82. + ", name and type index = " + nameAndTypeIndex;
  83. }
  84. return value;
  85. }
  86. /**
  87. * Gets the name of the class defining the field
  88. *
  89. * @return the name of the class defining the field
  90. */
  91. public String getFieldClassName() {
  92. return fieldClassName;
  93. }
  94. /**
  95. * Get the name of the field
  96. *
  97. * @return the field's name
  98. */
  99. public String getFieldName() {
  100. return fieldName;
  101. }
  102. /**
  103. * Get the type of the field
  104. *
  105. * @return the field's type in string format
  106. */
  107. public String getFieldType() {
  108. return fieldType;
  109. }
  110. }