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

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