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.

AttributeInfo.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.Map;
  20. import java.util.LinkedList;
  21. import java.util.ListIterator;
  22. // Note: if you define a new subclass of AttributeInfo, then
  23. // update AttributeInfo.read().
  24. /**
  25. * <code>attribute_info</code> structure.
  26. */
  27. public class AttributeInfo {
  28. protected ConstPool constPool;
  29. int name;
  30. byte[] info;
  31. protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) {
  32. constPool = cp;
  33. name = attrname;
  34. info = attrinfo;
  35. }
  36. protected AttributeInfo(ConstPool cp, String attrname) {
  37. this(cp, attrname, (byte[])null);
  38. }
  39. /**
  40. * Constructs an <code>attribute_info</code> structure.
  41. *
  42. * @param cp constant pool table
  43. * @param attrname attribute name
  44. * @param attrinfo <code>info</code> field
  45. * of <code>attribute_info</code> structure.
  46. */
  47. public AttributeInfo(ConstPool cp, String attrname, byte[] attrinfo) {
  48. this(cp, cp.addUtf8Info(attrname), attrinfo);
  49. }
  50. protected AttributeInfo(ConstPool cp, int n, DataInputStream in)
  51. throws IOException
  52. {
  53. constPool = cp;
  54. name = n;
  55. int len = in.readInt();
  56. info = new byte[len];
  57. if (len > 0)
  58. in.readFully(info);
  59. }
  60. static AttributeInfo read(ConstPool cp, DataInputStream in)
  61. throws IOException
  62. {
  63. int name = in.readUnsignedShort();
  64. String nameStr = cp.getUtf8Info(name);
  65. if (nameStr.equals(CodeAttribute.tag))
  66. return new CodeAttribute(cp, name, in);
  67. else if (nameStr.equals(ExceptionsAttribute.tag))
  68. return new ExceptionsAttribute(cp, name, in);
  69. else if (nameStr.equals(ConstantAttribute.tag))
  70. return new ConstantAttribute(cp, name, in);
  71. else if (nameStr.equals(SourceFileAttribute.tag))
  72. return new SourceFileAttribute(cp, name, in);
  73. else if (nameStr.equals(LineNumberAttribute.tag))
  74. return new LineNumberAttribute(cp, name, in);
  75. else if (nameStr.equals(SyntheticAttribute.tag))
  76. return new SyntheticAttribute(cp, name, in);
  77. else if (nameStr.equals(InnerClassesAttribute.tag))
  78. return new InnerClassesAttribute(cp, name, in);
  79. else
  80. return new AttributeInfo(cp, name, in);
  81. }
  82. /**
  83. * Returns an attribute name.
  84. */
  85. public String getName() {
  86. return constPool.getUtf8Info(name);
  87. }
  88. /**
  89. * Returns a constant pool table.
  90. */
  91. public ConstPool getConstPool() { return constPool; }
  92. /**
  93. * Returns the length of this <code>attribute_info</code>
  94. * structure.
  95. * The returned value is <code>attribute_length + 6</code>.
  96. */
  97. public int length() {
  98. return info.length + 6;
  99. }
  100. /**
  101. * Returns the <code>info</code> field
  102. * of this <code>attribute_info</code> structure.
  103. *
  104. * <p>This method is not available if the object is an instance
  105. * of <code>CodeAttribute</code>.
  106. */
  107. public byte[] get() { return info; }
  108. /**
  109. * Sets the <code>info</code> field
  110. * of this <code>attribute_info</code> structure.
  111. *
  112. * <p>This method is not available if the object is an instance
  113. * of <code>CodeAttribute</code>.
  114. */
  115. public void set(byte[] newinfo) { info = newinfo; }
  116. /**
  117. * Makes a copy. Class names are replaced according to the
  118. * given <code>Map</code> object.
  119. *
  120. * @param newCp the constant pool table used by the new copy.
  121. * @param classnames pairs of replaced and substituted
  122. * class names.
  123. */
  124. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  125. int s = info.length;
  126. byte[] newInfo = new byte[s];
  127. for (int i = 0; i < s; ++i)
  128. newInfo[i] = info[i];
  129. return new AttributeInfo(newCp, getName(), newInfo);
  130. }
  131. void write(DataOutputStream out) throws IOException {
  132. out.writeShort(name);
  133. out.writeInt(info.length);
  134. if (info.length > 0)
  135. out.write(info);
  136. }
  137. static int getLength(LinkedList list) {
  138. int size = 0;
  139. int n = list.size();
  140. for (int i = 0; i < n; ++i) {
  141. AttributeInfo attr = (AttributeInfo)list.get(i);
  142. size += attr.length();
  143. }
  144. return size;
  145. }
  146. static AttributeInfo lookup(LinkedList list, String name) {
  147. if (list == null)
  148. return null;
  149. ListIterator iterator = list.listIterator();
  150. while (iterator.hasNext()) {
  151. AttributeInfo ai = (AttributeInfo)iterator.next();
  152. if (ai.getName().equals(name))
  153. return ai;
  154. }
  155. return null; // no such attribute
  156. }
  157. static AttributeInfo lookup(LinkedList list, Class type) {
  158. if (list == null)
  159. return null;
  160. ListIterator iterator = list.listIterator();
  161. while (iterator.hasNext()) {
  162. Object obj = iterator.next();
  163. if (type.isInstance(obj))
  164. return (AttributeInfo)obj;
  165. }
  166. return null; // no such attribute
  167. }
  168. static synchronized void remove(LinkedList list, String name) {
  169. if (list == null)
  170. return;
  171. ListIterator iterator = list.listIterator();
  172. while (iterator.hasNext()) {
  173. AttributeInfo ai = (AttributeInfo)iterator.next();
  174. if (ai.getName().equals(name))
  175. iterator.remove();
  176. }
  177. }
  178. static synchronized void remove(LinkedList list, Class type) {
  179. if (list == null)
  180. return;
  181. ListIterator iterator = list.listIterator();
  182. while (iterator.hasNext()) {
  183. Object obj = iterator.next();
  184. if (type.isInstance(obj))
  185. iterator.remove();
  186. }
  187. }
  188. static void writeAll(LinkedList list, DataOutputStream out)
  189. throws IOException
  190. {
  191. if (list == null)
  192. return;
  193. int n = list.size();
  194. for (int i = 0; i < n; ++i) {
  195. AttributeInfo attr = (AttributeInfo)list.get(i);
  196. attr.write(out);
  197. }
  198. }
  199. }