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

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