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

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