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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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(CodeAttribute.tag))
  67. return new CodeAttribute(cp, name, in);
  68. else if (nameStr.equals(ConstantAttribute.tag))
  69. return new ConstantAttribute(cp, name, in);
  70. else if (nameStr.equals(DeprecatedAttribute.tag))
  71. return new DeprecatedAttribute(cp, name, in);
  72. else if (nameStr.equals(EnclosingMethodAttribute.tag))
  73. return new EnclosingMethodAttribute(cp, name, in);
  74. else if (nameStr.equals(ExceptionsAttribute.tag))
  75. return new ExceptionsAttribute(cp, name, in);
  76. else if (nameStr.equals(InnerClassesAttribute.tag))
  77. return new InnerClassesAttribute(cp, name, in);
  78. }
  79. else {
  80. /* Note that the names of Annotations attributes begin with 'R'.
  81. */
  82. if (nameStr.equals(LineNumberAttribute.tag))
  83. return new LineNumberAttribute(cp, name, in);
  84. else if (nameStr.equals(LocalVariableAttribute.tag)
  85. || nameStr.equals(LocalVariableAttribute.typeTag))
  86. return new LocalVariableAttribute(cp, name, in);
  87. else if (nameStr.equals(AnnotationsAttribute.visibleTag)
  88. || nameStr.equals(AnnotationsAttribute.invisibleTag))
  89. return new AnnotationsAttribute(cp, name, in);
  90. else if (nameStr.equals(ParameterAnnotationsAttribute.visibleTag)
  91. || nameStr.equals(ParameterAnnotationsAttribute.invisibleTag))
  92. return new ParameterAnnotationsAttribute(cp, name, in);
  93. else if (nameStr.equals(SignatureAttribute.tag))
  94. return new SignatureAttribute(cp, name, in);
  95. else if (nameStr.equals(SourceFileAttribute.tag))
  96. return new SourceFileAttribute(cp, name, in);
  97. else if (nameStr.equals(SyntheticAttribute.tag))
  98. return new SyntheticAttribute(cp, name, in);
  99. }
  100. return new AttributeInfo(cp, name, in);
  101. }
  102. /**
  103. * Returns an attribute name.
  104. */
  105. public String getName() {
  106. return constPool.getUtf8Info(name);
  107. }
  108. /**
  109. * Returns a constant pool table.
  110. */
  111. public ConstPool getConstPool() { return constPool; }
  112. /**
  113. * Returns the length of this <code>attribute_info</code>
  114. * structure.
  115. * The returned value is <code>attribute_length + 6</code>.
  116. */
  117. public int length() {
  118. return info.length + 6;
  119. }
  120. /**
  121. * Returns the <code>info</code> field
  122. * of this <code>attribute_info</code> structure.
  123. *
  124. * <p>This method is not available if the object is an instance
  125. * of <code>CodeAttribute</code>.
  126. */
  127. public byte[] get() { return info; }
  128. /**
  129. * Sets the <code>info</code> field
  130. * of this <code>attribute_info</code> structure.
  131. *
  132. * <p>This method is not available if the object is an instance
  133. * of <code>CodeAttribute</code>.
  134. */
  135. public void set(byte[] newinfo) { info = newinfo; }
  136. /**
  137. * Makes a copy. Class names are replaced according to the
  138. * given <code>Map</code> object.
  139. *
  140. * @param newCp the constant pool table used by the new copy.
  141. * @param classnames pairs of replaced and substituted
  142. * class names.
  143. */
  144. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  145. int s = info.length;
  146. byte[] newInfo = new byte[s];
  147. for (int i = 0; i < s; ++i)
  148. newInfo[i] = info[i];
  149. return new AttributeInfo(newCp, getName(), newInfo);
  150. }
  151. void write(DataOutputStream out) throws IOException {
  152. out.writeShort(name);
  153. out.writeInt(info.length);
  154. if (info.length > 0)
  155. out.write(info);
  156. }
  157. static int getLength(LinkedList list) {
  158. int size = 0;
  159. int n = list.size();
  160. for (int i = 0; i < n; ++i) {
  161. AttributeInfo attr = (AttributeInfo)list.get(i);
  162. size += attr.length();
  163. }
  164. return size;
  165. }
  166. static AttributeInfo lookup(LinkedList list, String name) {
  167. if (list == null)
  168. return null;
  169. ListIterator iterator = list.listIterator();
  170. while (iterator.hasNext()) {
  171. AttributeInfo ai = (AttributeInfo)iterator.next();
  172. if (ai.getName().equals(name))
  173. return ai;
  174. }
  175. return null; // no such attribute
  176. }
  177. static synchronized void remove(LinkedList list, String name) {
  178. if (list == null)
  179. return;
  180. ListIterator iterator = list.listIterator();
  181. while (iterator.hasNext()) {
  182. AttributeInfo ai = (AttributeInfo)iterator.next();
  183. if (ai.getName().equals(name))
  184. iterator.remove();
  185. }
  186. }
  187. static void writeAll(LinkedList list, DataOutputStream out)
  188. throws IOException
  189. {
  190. if (list == null)
  191. return;
  192. int n = list.size();
  193. for (int i = 0; i < n; ++i) {
  194. AttributeInfo attr = (AttributeInfo)list.get(i);
  195. attr.write(out);
  196. }
  197. }
  198. static LinkedList copyAll(LinkedList list, ConstPool cp) {
  199. if (list == null)
  200. return null;
  201. LinkedList newList = new LinkedList();
  202. int n = list.size();
  203. for (int i = 0; i < n; ++i) {
  204. AttributeInfo attr = (AttributeInfo)list.get(i);
  205. newList.add(attr.copy(cp, null));
  206. }
  207. return newList;
  208. }
  209. }