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

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