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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. String cachedName;
  31. String cachedType;
  32. int descriptor;
  33. LinkedList attribute; // may be null.
  34. private FieldInfo(ConstPool cp) {
  35. constPool = cp;
  36. accessFlags = 0;
  37. attribute = null;
  38. }
  39. /**
  40. * Constructs a <code>field_info</code> structure.
  41. *
  42. * @param cp a constant pool table
  43. * @param fieldName field name
  44. * @param desc field descriptor
  45. *
  46. * @see Descriptor
  47. */
  48. public FieldInfo(ConstPool cp, String fieldName, String desc) {
  49. this(cp);
  50. name = cp.addUtf8Info(fieldName);
  51. cachedName = fieldName;
  52. descriptor = cp.addUtf8Info(desc);
  53. }
  54. FieldInfo(ConstPool cp, DataInputStream in) throws IOException {
  55. this(cp);
  56. read(in);
  57. }
  58. /**
  59. * Returns a string representation of the object.
  60. */
  61. public String toString() {
  62. return getName() + " " + getDescriptor();
  63. }
  64. /**
  65. * Copies all constant pool items to a given new constant pool
  66. * and replaces the original items with the new ones.
  67. * This is used for garbage collecting the items of removed fields
  68. * and methods.
  69. *
  70. * @param cp the destination
  71. */
  72. void compact(ConstPool cp) {
  73. name = cp.addUtf8Info(getName());
  74. descriptor = cp.addUtf8Info(getDescriptor());
  75. attribute = AttributeInfo.copyAll(attribute, cp);
  76. constPool = cp;
  77. }
  78. void prune(ConstPool cp) {
  79. LinkedList newAttributes = new LinkedList();
  80. AttributeInfo invisibleAnnotations
  81. = getAttribute(AnnotationsAttribute.invisibleTag);
  82. if (invisibleAnnotations != null) {
  83. invisibleAnnotations = invisibleAnnotations.copy(cp, null);
  84. newAttributes.add(invisibleAnnotations);
  85. }
  86. AttributeInfo visibleAnnotations
  87. = getAttribute(AnnotationsAttribute.visibleTag);
  88. if (visibleAnnotations != null) {
  89. visibleAnnotations = visibleAnnotations.copy(cp, null);
  90. newAttributes.add(visibleAnnotations);
  91. }
  92. int index = getConstantValue();
  93. if (index != 0) {
  94. index = constPool.copy(index, cp, null);
  95. newAttributes.add(new ConstantAttribute(cp, index));
  96. }
  97. attribute = newAttributes;
  98. name = cp.addUtf8Info(getName());
  99. descriptor = cp.addUtf8Info(getDescriptor());
  100. constPool = cp;
  101. }
  102. /**
  103. * Returns the constant pool table used
  104. * by this <code>field_info</code>.
  105. */
  106. public ConstPool getConstPool() {
  107. return constPool;
  108. }
  109. /**
  110. * Returns the field name.
  111. */
  112. public String getName() {
  113. if (cachedName == null)
  114. cachedName = constPool.getUtf8Info(name);
  115. return cachedName;
  116. }
  117. /**
  118. * Sets the field name.
  119. */
  120. public void setName(String newName) {
  121. name = constPool.addUtf8Info(newName);
  122. cachedName = newName;
  123. }
  124. /**
  125. * Returns the access flags.
  126. *
  127. * @see AccessFlag
  128. */
  129. public int getAccessFlags() {
  130. return accessFlags;
  131. }
  132. /**
  133. * Sets the access flags.
  134. *
  135. * @see AccessFlag
  136. */
  137. public void setAccessFlags(int acc) {
  138. accessFlags = acc;
  139. }
  140. /**
  141. * Returns the field descriptor.
  142. *
  143. * @see Descriptor
  144. */
  145. public String getDescriptor() {
  146. return constPool.getUtf8Info(descriptor);
  147. }
  148. /**
  149. * Sets the field descriptor.
  150. *
  151. * @see Descriptor
  152. */
  153. public void setDescriptor(String desc) {
  154. if (!desc.equals(getDescriptor()))
  155. descriptor = constPool.addUtf8Info(desc);
  156. }
  157. /**
  158. * Finds a ConstantValue attribute and returns the index into
  159. * the <code>constant_pool</code> table.
  160. *
  161. * @return 0 if a ConstantValue attribute is not found.
  162. */
  163. public int getConstantValue() {
  164. if ((accessFlags & AccessFlag.STATIC) == 0)
  165. return 0;
  166. ConstantAttribute attr
  167. = (ConstantAttribute)getAttribute(ConstantAttribute.tag);
  168. if (attr == null)
  169. return 0;
  170. else
  171. return attr.getConstantValue();
  172. }
  173. /**
  174. * Returns all the attributes. The returned <code>List</code> object
  175. * is shared with this object. If you add a new attribute to the list,
  176. * the attribute is also added to the field represented by this
  177. * object. If you remove an attribute from the list, it is also removed
  178. * from the field.
  179. *
  180. * @return a list of <code>AttributeInfo</code> objects.
  181. * @see AttributeInfo
  182. */
  183. public List getAttributes() {
  184. if (attribute == null)
  185. attribute = new LinkedList();
  186. return attribute;
  187. }
  188. /**
  189. * Returns the attribute with the specified name.
  190. * It returns null if the specified attribute is not found.
  191. *
  192. * @param name attribute name
  193. * @see #getAttributes()
  194. */
  195. public AttributeInfo getAttribute(String name) {
  196. return AttributeInfo.lookup(attribute, name);
  197. }
  198. /**
  199. * Appends an attribute. If there is already an attribute with
  200. * the same name, the new one substitutes for it.
  201. *
  202. * @see #getAttributes()
  203. */
  204. public void addAttribute(AttributeInfo info) {
  205. if (attribute == null)
  206. attribute = new LinkedList();
  207. AttributeInfo.remove(attribute, info.getName());
  208. attribute.add(info);
  209. }
  210. private void read(DataInputStream in) throws IOException {
  211. accessFlags = in.readUnsignedShort();
  212. name = in.readUnsignedShort();
  213. descriptor = in.readUnsignedShort();
  214. int n = in.readUnsignedShort();
  215. attribute = new LinkedList();
  216. for (int i = 0; i < n; ++i)
  217. attribute.add(AttributeInfo.read(constPool, in));
  218. }
  219. void write(DataOutputStream out) throws IOException {
  220. out.writeShort(accessFlags);
  221. out.writeShort(name);
  222. out.writeShort(descriptor);
  223. if (attribute == null)
  224. out.writeShort(0);
  225. else {
  226. out.writeShort(attribute.size());
  227. AttributeInfo.writeAll(attribute, out);
  228. }
  229. }
  230. }