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.

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