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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.LinkedList;
  21. import java.util.ListIterator;
  22. // Note: if you define a new subclass of AttributeInfo, then
  23. // update AttributeInfo.read().
  24. /**
  25. * <code>attribute_info</code> structure.
  26. */
  27. public class AttributeInfo {
  28. protected ConstPool constPool;
  29. int name;
  30. byte[] info;
  31. protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) {
  32. constPool = cp;
  33. name = attrname;
  34. info = attrinfo;
  35. }
  36. protected AttributeInfo(ConstPool cp, String attrname) {
  37. this(cp, attrname, (byte[])null);
  38. }
  39. /**
  40. * Constructs an <code>attribute_info</code> structure.
  41. *
  42. * @param cp constant pool table
  43. * @param attrname attribute name
  44. * @param attrinfo <code>info</code> field
  45. * of <code>attribute_info</code> structure.
  46. */
  47. public AttributeInfo(ConstPool cp, String attrname, byte[] attrinfo) {
  48. this(cp, cp.addUtf8Info(attrname), attrinfo);
  49. }
  50. protected AttributeInfo(ConstPool cp, int n, DataInputStream in)
  51. throws IOException
  52. {
  53. constPool = cp;
  54. name = n;
  55. int len = in.readInt();
  56. info = new byte[len];
  57. if (len > 0)
  58. in.readFully(info);
  59. }
  60. static AttributeInfo read(ConstPool cp, DataInputStream in)
  61. throws IOException
  62. {
  63. int name = in.readUnsignedShort();
  64. String nameStr = cp.getUtf8Info(name);
  65. if (nameStr.charAt(0) < 'L') {
  66. if (nameStr.equals(AnnotationDefaultAttribute.tag))
  67. return new AnnotationDefaultAttribute(cp, name, in);
  68. else if (nameStr.equals(CodeAttribute.tag))
  69. return new CodeAttribute(cp, name, in);
  70. else if (nameStr.equals(ConstantAttribute.tag))
  71. return new ConstantAttribute(cp, name, in);
  72. else if (nameStr.equals(DeprecatedAttribute.tag))
  73. return new DeprecatedAttribute(cp, name, in);
  74. else if (nameStr.equals(EnclosingMethodAttribute.tag))
  75. return new EnclosingMethodAttribute(cp, name, in);
  76. else if (nameStr.equals(ExceptionsAttribute.tag))
  77. return new ExceptionsAttribute(cp, name, in);
  78. else if (nameStr.equals(InnerClassesAttribute.tag))
  79. return new InnerClassesAttribute(cp, name, in);
  80. }
  81. else {
  82. /* Note that the names of Annotations attributes begin with 'R'.
  83. */
  84. if (nameStr.equals(LineNumberAttribute.tag))
  85. return new LineNumberAttribute(cp, name, in);
  86. else if (nameStr.equals(LocalVariableAttribute.tag)
  87. || nameStr.equals(LocalVariableAttribute.typeTag))
  88. return new LocalVariableAttribute(cp, name, in);
  89. else if (nameStr.equals(AnnotationsAttribute.visibleTag)
  90. || nameStr.equals(AnnotationsAttribute.invisibleTag))
  91. return new AnnotationsAttribute(cp, name, in);
  92. else if (nameStr.equals(ParameterAnnotationsAttribute.visibleTag)
  93. || nameStr.equals(ParameterAnnotationsAttribute.invisibleTag))
  94. return new ParameterAnnotationsAttribute(cp, name, in);
  95. else if (nameStr.equals(SignatureAttribute.tag))
  96. return new SignatureAttribute(cp, name, in);
  97. else if (nameStr.equals(SourceFileAttribute.tag))
  98. return new SourceFileAttribute(cp, name, in);
  99. else if (nameStr.equals(SyntheticAttribute.tag))
  100. return new SyntheticAttribute(cp, name, in);
  101. }
  102. return new AttributeInfo(cp, name, in);
  103. }
  104. /**
  105. * Returns an attribute name.
  106. */
  107. public String getName() {
  108. return constPool.getUtf8Info(name);
  109. }
  110. /**
  111. * Returns a constant pool table.
  112. */
  113. public ConstPool getConstPool() { return constPool; }
  114. /**
  115. * Returns the length of this <code>attribute_info</code>
  116. * structure.
  117. * The returned value is <code>attribute_length + 6</code>.
  118. */
  119. public int length() {
  120. return info.length + 6;
  121. }
  122. /**
  123. * Returns the <code>info</code> field
  124. * of this <code>attribute_info</code> structure.
  125. *
  126. * <p>This method is not available if the object is an instance
  127. * of <code>CodeAttribute</code>.
  128. */
  129. public byte[] get() { return info; }
  130. /**
  131. * Sets the <code>info</code> field
  132. * of this <code>attribute_info</code> structure.
  133. *
  134. * <p>This method is not available if the object is an instance
  135. * of <code>CodeAttribute</code>.
  136. */
  137. public void set(byte[] newinfo) { info = newinfo; }
  138. /**
  139. * Makes a copy. Class names are replaced according to the
  140. * given <code>Map</code> object.
  141. *
  142. * @param newCp the constant pool table used by the new copy.
  143. * @param classnames pairs of replaced and substituted
  144. * class names.
  145. */
  146. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  147. int s = info.length;
  148. byte[] newInfo = new byte[s];
  149. for (int i = 0; i < s; ++i)
  150. newInfo[i] = info[i];
  151. return new AttributeInfo(newCp, getName(), newInfo);
  152. }
  153. void write(DataOutputStream out) throws IOException {
  154. out.writeShort(name);
  155. out.writeInt(info.length);
  156. if (info.length > 0)
  157. out.write(info);
  158. }
  159. static int getLength(LinkedList list) {
  160. int size = 0;
  161. int n = list.size();
  162. for (int i = 0; i < n; ++i) {
  163. AttributeInfo attr = (AttributeInfo)list.get(i);
  164. size += attr.length();
  165. }
  166. return size;
  167. }
  168. static AttributeInfo lookup(LinkedList list, String name) {
  169. if (list == null)
  170. return null;
  171. ListIterator iterator = list.listIterator();
  172. while (iterator.hasNext()) {
  173. AttributeInfo ai = (AttributeInfo)iterator.next();
  174. if (ai.getName().equals(name))
  175. return ai;
  176. }
  177. return null; // no such attribute
  178. }
  179. static synchronized void remove(LinkedList list, String name) {
  180. if (list == null)
  181. return;
  182. ListIterator iterator = list.listIterator();
  183. while (iterator.hasNext()) {
  184. AttributeInfo ai = (AttributeInfo)iterator.next();
  185. if (ai.getName().equals(name))
  186. iterator.remove();
  187. }
  188. }
  189. static void writeAll(LinkedList list, DataOutputStream out)
  190. throws IOException
  191. {
  192. if (list == null)
  193. return;
  194. int n = list.size();
  195. for (int i = 0; i < n; ++i) {
  196. AttributeInfo attr = (AttributeInfo)list.get(i);
  197. attr.write(out);
  198. }
  199. }
  200. static LinkedList copyAll(LinkedList list, ConstPool cp) {
  201. if (list == null)
  202. return null;
  203. LinkedList newList = new LinkedList();
  204. int n = list.size();
  205. for (int i = 0; i < n; ++i) {
  206. AttributeInfo attr = (AttributeInfo)list.get(i);
  207. newList.add(attr.copy(cp, null));
  208. }
  209. return newList;
  210. }
  211. }