You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FieldInfo.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.util.Hashtable;
  20. import java.util.List;
  21. import java.util.LinkedList;
  22. /**
  23. * <code>field_info</code> structure.
  24. *
  25. * @see javassist.CtField#getFieldInfo()
  26. */
  27. public final class FieldInfo {
  28. ConstPool constPool;
  29. int accessFlags;
  30. int name;
  31. int descriptor;
  32. LinkedList attribute; // may be null.
  33. private FieldInfo(ConstPool cp) {
  34. constPool = cp;
  35. accessFlags = 0;
  36. attribute = null;
  37. }
  38. /**
  39. * Constructs a <code>field_info</code> structure.
  40. *
  41. * @param cp a constant pool table
  42. * @param fieldName field name
  43. * @param desc field descriptor
  44. *
  45. * @see Descriptor
  46. */
  47. public FieldInfo(ConstPool cp, String fieldName, String desc) {
  48. this(cp);
  49. name = cp.addUtf8Info(fieldName);
  50. descriptor = cp.addUtf8Info(desc);
  51. }
  52. FieldInfo(ConstPool cp, DataInputStream in) throws IOException {
  53. this(cp);
  54. read(in);
  55. }
  56. /**
  57. * Returns the constant pool table used
  58. * by this <code>field_info</code>.
  59. */
  60. public ConstPool getConstPool() { return constPool; }
  61. /**
  62. * Returns the field name.
  63. */
  64. public String getName() {
  65. return constPool.getUtf8Info(name);
  66. }
  67. /**
  68. * Sets the field name.
  69. */
  70. public void setName(String newName) {
  71. name = constPool.addUtf8Info(newName);
  72. }
  73. /**
  74. * Returns the access flags.
  75. *
  76. * @see AccessFlag
  77. */
  78. public int getAccessFlags() {
  79. return accessFlags;
  80. }
  81. /**
  82. * Sets the access flags.
  83. *
  84. * @see AccessFlag
  85. */
  86. public void setAccessFlags(int acc) {
  87. accessFlags = acc;
  88. }
  89. /**
  90. * Returns the field descriptor.
  91. *
  92. * @see Descriptor
  93. */
  94. public String getDescriptor() {
  95. return constPool.getUtf8Info(descriptor);
  96. }
  97. /**
  98. * Sets the field descriptor.
  99. *
  100. * @see Descriptor
  101. */
  102. public void setDescriptor(String desc) {
  103. if (!desc.equals(getDescriptor()))
  104. descriptor = constPool.addUtf8Info(desc);
  105. }
  106. /**
  107. * Returns all the attributes.
  108. *
  109. * @return a list of <code>AttributeInfo</code> objects.
  110. * @see AttributeInfo
  111. */
  112. public List getAttributes() {
  113. if (attribute == null)
  114. attribute = new LinkedList();
  115. return attribute;
  116. }
  117. /**
  118. * Returns the attribute with the specified name.
  119. *
  120. * @param name attribute name
  121. */
  122. public AttributeInfo getAttribute(String name) {
  123. return AttributeInfo.lookup(attribute, name);
  124. }
  125. /**
  126. * Appends an attribute. If there is already an attribute with
  127. * the same name, the new one substitutes for it.
  128. */
  129. public void addAttribute(AttributeInfo info) {
  130. if (attribute == null)
  131. attribute = new LinkedList();
  132. AttributeInfo.remove(attribute, info.getName());
  133. attribute.add(info);
  134. }
  135. private void read(DataInputStream in) throws IOException {
  136. accessFlags = in.readUnsignedShort();
  137. name = in.readUnsignedShort();
  138. descriptor = in.readUnsignedShort();
  139. int n = in.readUnsignedShort();
  140. attribute = new LinkedList();
  141. for (int i = 0; i < n; ++i)
  142. attribute.add(AttributeInfo.read(constPool, in));
  143. }
  144. void write(DataOutputStream out) throws IOException {
  145. out.writeShort(accessFlags);
  146. out.writeShort(name);
  147. out.writeShort(descriptor);
  148. if (attribute == null)
  149. out.writeShort(0);
  150. else {
  151. out.writeShort(attribute.size());
  152. AttributeInfo.writeAll(attribute, out);
  153. }
  154. }
  155. }