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

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