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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.Map;
  21. import java.util.ArrayList;
  22. import java.util.ListIterator;
  23. import java.util.List;
  24. import java.util.Iterator;
  25. // Note: if you define a new subclass of AttributeInfo, then
  26. // update AttributeInfo.read(), .copy(), and (maybe) write().
  27. /**
  28. * <code>attribute_info</code> structure.
  29. */
  30. public class AttributeInfo {
  31. protected ConstPool constPool;
  32. int name;
  33. byte[] info;
  34. protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) {
  35. constPool = cp;
  36. name = attrname;
  37. info = attrinfo;
  38. }
  39. protected AttributeInfo(ConstPool cp, String attrname) {
  40. this(cp, attrname, (byte[])null);
  41. }
  42. /**
  43. * Constructs an <code>attribute_info</code> structure.
  44. *
  45. * @param cp constant pool table
  46. * @param attrname attribute name
  47. * @param attrinfo <code>info</code> field
  48. * of <code>attribute_info</code> structure.
  49. */
  50. public AttributeInfo(ConstPool cp, String attrname, byte[] attrinfo) {
  51. this(cp, cp.addUtf8Info(attrname), attrinfo);
  52. }
  53. protected AttributeInfo(ConstPool cp, int n, DataInputStream in)
  54. throws IOException
  55. {
  56. constPool = cp;
  57. name = n;
  58. int len = in.readInt();
  59. info = new byte[len];
  60. if (len > 0)
  61. in.readFully(info);
  62. }
  63. static AttributeInfo read(ConstPool cp, DataInputStream in)
  64. throws IOException
  65. {
  66. int name = in.readUnsignedShort();
  67. String nameStr = cp.getUtf8Info(name);
  68. if (nameStr.charAt(0) < 'L') {
  69. if (nameStr.equals(AnnotationDefaultAttribute.tag))
  70. return new AnnotationDefaultAttribute(cp, name, in);
  71. else if (nameStr.equals(BootstrapMethodsAttribute.tag))
  72. return new BootstrapMethodsAttribute(cp, name, in);
  73. else if (nameStr.equals(CodeAttribute.tag))
  74. return new CodeAttribute(cp, name, in);
  75. else if (nameStr.equals(ConstantAttribute.tag))
  76. return new ConstantAttribute(cp, name, in);
  77. else if (nameStr.equals(DeprecatedAttribute.tag))
  78. return new DeprecatedAttribute(cp, name, in);
  79. else if (nameStr.equals(EnclosingMethodAttribute.tag))
  80. return new EnclosingMethodAttribute(cp, name, in);
  81. else if (nameStr.equals(ExceptionsAttribute.tag))
  82. return new ExceptionsAttribute(cp, name, in);
  83. else if (nameStr.equals(InnerClassesAttribute.tag))
  84. return new InnerClassesAttribute(cp, name, in);
  85. }
  86. else {
  87. /* Note that the names of Annotations attributes begin with 'R'.
  88. */
  89. if (nameStr.equals(LineNumberAttribute.tag))
  90. return new LineNumberAttribute(cp, name, in);
  91. else if (nameStr.equals(LocalVariableAttribute.tag))
  92. return new LocalVariableAttribute(cp, name, in);
  93. else if (nameStr.equals(LocalVariableTypeAttribute.tag))
  94. return new LocalVariableTypeAttribute(cp, name, in);
  95. else if (nameStr.equals(AnnotationsAttribute.visibleTag)
  96. || nameStr.equals(AnnotationsAttribute.invisibleTag)) {
  97. // RuntimeVisibleAnnotations or RuntimeInvisibleAnnotations
  98. return new AnnotationsAttribute(cp, name, in);
  99. }
  100. else if (nameStr.equals(ParameterAnnotationsAttribute.visibleTag)
  101. || nameStr.equals(ParameterAnnotationsAttribute.invisibleTag))
  102. return new ParameterAnnotationsAttribute(cp, name, in);
  103. else if (nameStr.equals(SignatureAttribute.tag))
  104. return new SignatureAttribute(cp, name, in);
  105. else if (nameStr.equals(SourceFileAttribute.tag))
  106. return new SourceFileAttribute(cp, name, in);
  107. else if (nameStr.equals(SyntheticAttribute.tag))
  108. return new SyntheticAttribute(cp, name, in);
  109. else if (nameStr.equals(StackMap.tag))
  110. return new StackMap(cp, name, in);
  111. else if (nameStr.equals(StackMapTable.tag))
  112. return new StackMapTable(cp, name, in);
  113. }
  114. return new AttributeInfo(cp, name, in);
  115. }
  116. /**
  117. * Returns an attribute name.
  118. */
  119. public String getName() {
  120. return constPool.getUtf8Info(name);
  121. }
  122. /**
  123. * Returns a constant pool table.
  124. */
  125. public ConstPool getConstPool() { return constPool; }
  126. /**
  127. * Returns the length of this <code>attribute_info</code>
  128. * structure.
  129. * The returned value is <code>attribute_length + 6</code>.
  130. */
  131. public int length() {
  132. return info.length + 6;
  133. }
  134. /**
  135. * Returns the <code>info</code> field
  136. * of this <code>attribute_info</code> structure.
  137. *
  138. * <p>This method is not available if the object is an instance
  139. * of <code>CodeAttribute</code>.
  140. */
  141. public byte[] get() { return info; }
  142. /**
  143. * Sets the <code>info</code> field
  144. * of this <code>attribute_info</code> structure.
  145. *
  146. * <p>This method is not available if the object is an instance
  147. * of <code>CodeAttribute</code>.
  148. */
  149. public void set(byte[] newinfo) { info = newinfo; }
  150. /**
  151. * Makes a copy. Class names are replaced according to the
  152. * given <code>Map</code> object.
  153. *
  154. * @param newCp the constant pool table used by the new copy.
  155. * @param classnames pairs of replaced and substituted
  156. * class names.
  157. */
  158. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  159. int s = info.length;
  160. byte[] srcInfo = info;
  161. byte[] newInfo = new byte[s];
  162. for (int i = 0; i < s; ++i)
  163. newInfo[i] = srcInfo[i];
  164. return new AttributeInfo(newCp, getName(), newInfo);
  165. }
  166. void write(DataOutputStream out) throws IOException {
  167. out.writeShort(name);
  168. out.writeInt(info.length);
  169. if (info.length > 0)
  170. out.write(info);
  171. }
  172. static int getLength(ArrayList list) {
  173. int size = 0;
  174. int n = list.size();
  175. for (int i = 0; i < n; ++i) {
  176. AttributeInfo attr = (AttributeInfo)list.get(i);
  177. size += attr.length();
  178. }
  179. return size;
  180. }
  181. static AttributeInfo lookup(ArrayList list, String name) {
  182. if (list == null)
  183. return null;
  184. ListIterator iterator = list.listIterator();
  185. while (iterator.hasNext()) {
  186. AttributeInfo ai = (AttributeInfo)iterator.next();
  187. if (ai.getName().equals(name))
  188. return ai;
  189. }
  190. return null; // no such attribute
  191. }
  192. static synchronized void remove(ArrayList list, String name) {
  193. if (list == null)
  194. return;
  195. ListIterator iterator = list.listIterator();
  196. while (iterator.hasNext()) {
  197. AttributeInfo ai = (AttributeInfo)iterator.next();
  198. if (ai.getName().equals(name))
  199. iterator.remove();
  200. }
  201. }
  202. static void writeAll(ArrayList list, DataOutputStream out)
  203. throws IOException
  204. {
  205. if (list == null)
  206. return;
  207. int n = list.size();
  208. for (int i = 0; i < n; ++i) {
  209. AttributeInfo attr = (AttributeInfo)list.get(i);
  210. attr.write(out);
  211. }
  212. }
  213. static ArrayList copyAll(ArrayList list, ConstPool cp) {
  214. if (list == null)
  215. return null;
  216. ArrayList newList = new ArrayList();
  217. int n = list.size();
  218. for (int i = 0; i < n; ++i) {
  219. AttributeInfo attr = (AttributeInfo)list.get(i);
  220. newList.add(attr.copy(cp, null));
  221. }
  222. return newList;
  223. }
  224. /* The following two methods are used to implement
  225. * ClassFile.renameClass().
  226. * Only CodeAttribute, LocalVariableAttribute,
  227. * AnnotationsAttribute, and SignatureAttribute
  228. * override these methods.
  229. */
  230. void renameClass(String oldname, String newname) {}
  231. void renameClass(Map classnames) {}
  232. static void renameClass(List attributes, String oldname, String newname) {
  233. Iterator iterator = attributes.iterator();
  234. while (iterator.hasNext()) {
  235. AttributeInfo ai = (AttributeInfo)iterator.next();
  236. ai.renameClass(oldname, newname);
  237. }
  238. }
  239. static void renameClass(List attributes, Map classnames) {
  240. Iterator iterator = attributes.iterator();
  241. while (iterator.hasNext()) {
  242. AttributeInfo ai = (AttributeInfo)iterator.next();
  243. ai.renameClass(classnames);
  244. }
  245. }
  246. void getRefClasses(Map classnames) {}
  247. static void getRefClasses(List attributes, Map classnames) {
  248. Iterator iterator = attributes.iterator();
  249. while (iterator.hasNext()) {
  250. AttributeInfo ai = (AttributeInfo)iterator.next();
  251. ai.getRefClasses(classnames);
  252. }
  253. }
  254. }