您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AttributeInfo.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(DeprecatedAttribute.tag))
  78. return new DeprecatedAttribute(cp, name, in);
  79. else if (nameStr.equals(InnerClassesAttribute.tag))
  80. return new InnerClassesAttribute(cp, name, in);
  81. else
  82. return new AttributeInfo(cp, name, in);
  83. }
  84. /**
  85. * Returns an attribute name.
  86. */
  87. public String getName() {
  88. return constPool.getUtf8Info(name);
  89. }
  90. /**
  91. * Returns a constant pool table.
  92. */
  93. public ConstPool getConstPool() { return constPool; }
  94. /**
  95. * Returns the length of this <code>attribute_info</code>
  96. * structure.
  97. * The returned value is <code>attribute_length + 6</code>.
  98. */
  99. public int length() {
  100. return info.length + 6;
  101. }
  102. /**
  103. * Returns the <code>info</code> field
  104. * of this <code>attribute_info</code> structure.
  105. *
  106. * <p>This method is not available if the object is an instance
  107. * of <code>CodeAttribute</code>.
  108. */
  109. public byte[] get() { return info; }
  110. /**
  111. * Sets the <code>info</code> field
  112. * of this <code>attribute_info</code> structure.
  113. *
  114. * <p>This method is not available if the object is an instance
  115. * of <code>CodeAttribute</code>.
  116. */
  117. public void set(byte[] newinfo) { info = newinfo; }
  118. /**
  119. * Makes a copy. Class names are replaced according to the
  120. * given <code>Map</code> object.
  121. *
  122. * @param newCp the constant pool table used by the new copy.
  123. * @param classnames pairs of replaced and substituted
  124. * class names.
  125. */
  126. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  127. int s = info.length;
  128. byte[] newInfo = new byte[s];
  129. for (int i = 0; i < s; ++i)
  130. newInfo[i] = info[i];
  131. return new AttributeInfo(newCp, getName(), newInfo);
  132. }
  133. void write(DataOutputStream out) throws IOException {
  134. out.writeShort(name);
  135. out.writeInt(info.length);
  136. if (info.length > 0)
  137. out.write(info);
  138. }
  139. static int getLength(LinkedList list) {
  140. int size = 0;
  141. int n = list.size();
  142. for (int i = 0; i < n; ++i) {
  143. AttributeInfo attr = (AttributeInfo)list.get(i);
  144. size += attr.length();
  145. }
  146. return size;
  147. }
  148. static AttributeInfo lookup(LinkedList list, String name) {
  149. if (list == null)
  150. return null;
  151. ListIterator iterator = list.listIterator();
  152. while (iterator.hasNext()) {
  153. AttributeInfo ai = (AttributeInfo)iterator.next();
  154. if (ai.getName().equals(name))
  155. return ai;
  156. }
  157. return null; // no such attribute
  158. }
  159. static AttributeInfo lookup(LinkedList list, Class type) {
  160. if (list == null)
  161. return null;
  162. ListIterator iterator = list.listIterator();
  163. while (iterator.hasNext()) {
  164. Object obj = iterator.next();
  165. if (type.isInstance(obj))
  166. return (AttributeInfo)obj;
  167. }
  168. return null; // no such attribute
  169. }
  170. static synchronized void remove(LinkedList list, String name) {
  171. if (list == null)
  172. return;
  173. ListIterator iterator = list.listIterator();
  174. while (iterator.hasNext()) {
  175. AttributeInfo ai = (AttributeInfo)iterator.next();
  176. if (ai.getName().equals(name))
  177. iterator.remove();
  178. }
  179. }
  180. static synchronized void remove(LinkedList list, Class type) {
  181. if (list == null)
  182. return;
  183. ListIterator iterator = list.listIterator();
  184. while (iterator.hasNext()) {
  185. Object obj = iterator.next();
  186. if (type.isInstance(obj))
  187. iterator.remove();
  188. }
  189. }
  190. static void writeAll(LinkedList list, DataOutputStream out)
  191. throws IOException
  192. {
  193. if (list == null)
  194. return;
  195. int n = list.size();
  196. for (int i = 0; i < n; ++i) {
  197. AttributeInfo attr = (AttributeInfo)list.get(i);
  198. attr.write(out);
  199. }
  200. }
  201. }