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

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