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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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.List;
  20. import java.util.LinkedList;
  21. /**
  22. * <code>field_info</code> structure.
  23. *
  24. * @see javassist.CtField#getFieldInfo()
  25. */
  26. public final class FieldInfo {
  27. ConstPool constPool;
  28. int accessFlags;
  29. int name;
  30. int descriptor;
  31. LinkedList attribute; // may be null.
  32. private FieldInfo(ConstPool cp) {
  33. constPool = cp;
  34. accessFlags = 0;
  35. attribute = null;
  36. }
  37. /**
  38. * Constructs a <code>field_info</code> structure.
  39. *
  40. * @param cp a constant pool table
  41. * @param fieldName field name
  42. * @param desc field descriptor
  43. *
  44. * @see Descriptor
  45. */
  46. public FieldInfo(ConstPool cp, String fieldName, String desc) {
  47. this(cp);
  48. name = cp.addUtf8Info(fieldName);
  49. descriptor = cp.addUtf8Info(desc);
  50. }
  51. FieldInfo(ConstPool cp, DataInputStream in) throws IOException {
  52. this(cp);
  53. read(in);
  54. }
  55. void prune(ConstPool cp) {
  56. attribute = null;
  57. name = cp.addUtf8Info(getName());
  58. descriptor = cp.addUtf8Info(getDescriptor());
  59. constPool = cp;
  60. }
  61. /**
  62. * Returns the constant pool table used
  63. * by this <code>field_info</code>.
  64. */
  65. public ConstPool getConstPool() {
  66. return constPool;
  67. }
  68. /**
  69. * Returns the field name.
  70. */
  71. public String getName() {
  72. return constPool.getUtf8Info(name);
  73. }
  74. /**
  75. * Sets the field name.
  76. */
  77. public void setName(String newName) {
  78. name = constPool.addUtf8Info(newName);
  79. }
  80. /**
  81. * Returns the access flags.
  82. *
  83. * @see AccessFlag
  84. */
  85. public int getAccessFlags() {
  86. return accessFlags;
  87. }
  88. /**
  89. * Sets the access flags.
  90. *
  91. * @see AccessFlag
  92. */
  93. public void setAccessFlags(int acc) {
  94. accessFlags = acc;
  95. }
  96. /**
  97. * Returns the field descriptor.
  98. *
  99. * @see Descriptor
  100. */
  101. public String getDescriptor() {
  102. return constPool.getUtf8Info(descriptor);
  103. }
  104. /**
  105. * Sets the field descriptor.
  106. *
  107. * @see Descriptor
  108. */
  109. public void setDescriptor(String desc) {
  110. if (!desc.equals(getDescriptor()))
  111. descriptor = constPool.addUtf8Info(desc);
  112. }
  113. /**
  114. * Returns all the attributes.
  115. * A new element can be added to the returned list
  116. * and an existing element can be removed from the list.
  117. *
  118. * @return a list of <code>AttributeInfo</code> objects.
  119. * @see AttributeInfo
  120. */
  121. public List getAttributes() {
  122. if (attribute == null)
  123. attribute = new LinkedList();
  124. return attribute;
  125. }
  126. /**
  127. * Returns the attribute with the specified name.
  128. *
  129. * @param name attribute name
  130. */
  131. public AttributeInfo getAttribute(String name) {
  132. return AttributeInfo.lookup(attribute, name);
  133. }
  134. /**
  135. * Appends an attribute. If there is already an attribute with
  136. * the same name, the new one substitutes for it.
  137. */
  138. public void addAttribute(AttributeInfo info) {
  139. if (attribute == null)
  140. attribute = new LinkedList();
  141. AttributeInfo.remove(attribute, info.getName());
  142. attribute.add(info);
  143. }
  144. private void read(DataInputStream in) throws IOException {
  145. accessFlags = in.readUnsignedShort();
  146. name = in.readUnsignedShort();
  147. descriptor = in.readUnsignedShort();
  148. int n = in.readUnsignedShort();
  149. attribute = new LinkedList();
  150. for (int i = 0; i < n; ++i)
  151. attribute.add(AttributeInfo.read(constPool, in));
  152. }
  153. void write(DataOutputStream out) throws IOException {
  154. out.writeShort(accessFlags);
  155. out.writeShort(name);
  156. out.writeShort(descriptor);
  157. if (attribute == null)
  158. out.writeShort(0);
  159. else {
  160. out.writeShort(attribute.size());
  161. AttributeInfo.writeAll(attribute, out);
  162. }
  163. }
  164. }