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.

MethodInfo.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.List;
  21. import java.util.LinkedList;
  22. /**
  23. * <code>method_info</code> structure.
  24. *
  25. * @see javassist.CtMethod#getMethodInfo()
  26. * @see javassist.CtConstructor#getMethodInfo()
  27. */
  28. public final class MethodInfo {
  29. ConstPool constPool;
  30. int accessFlags;
  31. int name;
  32. int descriptor;
  33. LinkedList attribute; // may be null
  34. /**
  35. * The name of constructors: <code>&lt;init&gt</code>.
  36. */
  37. public static final String nameInit = "<init>";
  38. /**
  39. * The name of class initializer (static initializer):
  40. * <code>&lt;clinit&gt</code>.
  41. */
  42. public static final String nameClinit = "<clinit>";
  43. private MethodInfo(ConstPool cp) {
  44. constPool = cp;
  45. attribute = null;
  46. }
  47. /**
  48. * Constructs a <code>method_info</code> structure.
  49. * The initial value of <code>access_flags</code> is zero.
  50. *
  51. * @param cp a constant pool table
  52. * @param methodname method name
  53. * @param desc method descriptor
  54. *
  55. * @see Descriptor
  56. */
  57. public MethodInfo(ConstPool cp, String methodname, String desc) {
  58. this(cp);
  59. accessFlags = 0;
  60. name = cp.addUtf8Info(methodname);
  61. descriptor = constPool.addUtf8Info(desc);
  62. }
  63. MethodInfo(ConstPool cp, DataInputStream in) throws IOException {
  64. this(cp);
  65. read(in);
  66. }
  67. /**
  68. * Constructs a copy of <code>method_info</code> structure.
  69. * Class names appearing in the source <code>method_info</code>
  70. * are renamed according to <code>classnameMap</code>.
  71. *
  72. * <p>Note: only <code>Code</code> and <code>Exceptions</code>
  73. * attributes are copied from the source. The other attributes
  74. * are ignored.
  75. *
  76. * @param cp a constant pool table
  77. * @param methodname a method name
  78. * @param src a source <code>method_info</code>
  79. * @param classnameMap specifies pairs of replaced and substituted
  80. * name.
  81. * @see Descriptor
  82. */
  83. public MethodInfo(ConstPool cp, String methodname, MethodInfo src,
  84. Map classnameMap) throws BadBytecode {
  85. this(cp);
  86. read(src, methodname, classnameMap);
  87. }
  88. /**
  89. * Returns a method name.
  90. */
  91. public String getName() {
  92. return constPool.getUtf8Info(name);
  93. }
  94. /**
  95. * Sets a method name.
  96. */
  97. public void setName(String newName) {
  98. name = constPool.addUtf8Info(newName);
  99. }
  100. /**
  101. * Returns true if this is not a constructor or a class initializer
  102. * (static initializer).
  103. */
  104. public boolean isMethod() {
  105. String n = getName();
  106. return !n.equals(nameInit) && !n.equals(nameClinit);
  107. }
  108. /**
  109. * Returns a constant pool table used by this method.
  110. */
  111. public ConstPool getConstPool() {
  112. return constPool;
  113. }
  114. /**
  115. * Returns true if this is a constructor.
  116. */
  117. public boolean isConstructor() {
  118. return getName().equals(nameInit);
  119. }
  120. /**
  121. * Returns true if this is a class initializer (static initializer).
  122. */
  123. public boolean isStaticInitializer() {
  124. return getName().equals(nameClinit);
  125. }
  126. /**
  127. * Returns access flags.
  128. *
  129. * @see AccessFlag
  130. */
  131. public int getAccessFlags() {
  132. return accessFlags;
  133. }
  134. /**
  135. * Sets access flags.
  136. *
  137. * @see AccessFlag
  138. */
  139. public void setAccessFlags(int acc) {
  140. accessFlags = acc;
  141. }
  142. /**
  143. * Returns a method descriptor.
  144. *
  145. * @see Descriptor
  146. */
  147. public String getDescriptor() {
  148. return constPool.getUtf8Info(descriptor);
  149. }
  150. /**
  151. * Sets a method descriptor.
  152. *
  153. * @see Descriptor
  154. */
  155. public void setDescriptor(String desc) {
  156. if (!desc.equals(getDescriptor()))
  157. descriptor = constPool.addUtf8Info(desc);
  158. }
  159. /**
  160. * Returns all the attributes.
  161. *
  162. * @return a list of <code>AttributeInfo</code> objects.
  163. * @see AttributeInfo
  164. */
  165. public List getAttributes() {
  166. if (attribute == null)
  167. attribute = new LinkedList();
  168. return attribute;
  169. }
  170. /**
  171. * Returns the attribute with the specified name.
  172. * If it is not found, this method returns null.
  173. *
  174. * @param name attribute name
  175. * @return an <code>AttributeInfo</code> object or null.
  176. */
  177. public AttributeInfo getAttribute(String name) {
  178. return AttributeInfo.lookup(attribute, name);
  179. }
  180. /**
  181. * Appends an attribute. If there is already an attribute with
  182. * the same name, the new one substitutes for it.
  183. */
  184. public void addAttribute(AttributeInfo info) {
  185. if (attribute == null)
  186. attribute = new LinkedList();
  187. AttributeInfo.remove(attribute, info.getName());
  188. attribute.add(info);
  189. }
  190. /**
  191. * Returns an Exceptions attribute.
  192. *
  193. * @return an Exceptions attribute
  194. * or null if it is not specified.
  195. */
  196. public ExceptionsAttribute getExceptionsAttribute() {
  197. AttributeInfo info
  198. = AttributeInfo.lookup(attribute, ExceptionsAttribute.tag);
  199. return (ExceptionsAttribute)info;
  200. }
  201. /**
  202. * Returns a Code attribute.
  203. *
  204. * @return a Code attribute
  205. * or null if it is not specified.
  206. */
  207. public CodeAttribute getCodeAttribute() {
  208. AttributeInfo info
  209. = AttributeInfo.lookup(attribute, CodeAttribute.tag);
  210. return (CodeAttribute)info;
  211. }
  212. /**
  213. * Removes an Exception attribute.
  214. */
  215. public void removeExceptionsAttribute() {
  216. AttributeInfo.remove(attribute, ExceptionsAttribute.tag);
  217. }
  218. /**
  219. * Adds an Exception attribute.
  220. *
  221. * <p>The added attribute must share the same constant pool table
  222. * as this <code>method_info</code> structure.
  223. */
  224. public void setExceptionsAttribute(ExceptionsAttribute cattr) {
  225. removeExceptionsAttribute();
  226. if (attribute == null)
  227. attribute = new LinkedList();
  228. attribute.add(cattr);
  229. }
  230. /**
  231. * Removes a Code attribute.
  232. */
  233. public void removeCodeAttribute() {
  234. AttributeInfo.remove(attribute, CodeAttribute.tag);
  235. }
  236. /**
  237. * Adds a Code attribute.
  238. *
  239. * <p>The added attribute must share the same constant pool table
  240. * as this <code>method_info</code> structure.
  241. */
  242. public void setCodeAttribute(CodeAttribute cattr) {
  243. removeCodeAttribute();
  244. if (attribute == null)
  245. attribute = new LinkedList();
  246. attribute.add(cattr);
  247. }
  248. /**
  249. * Returns the line number of the source line corresponding to the
  250. * specified bytecode contained in this method.
  251. *
  252. * @param pos the position of the bytecode (&gt;= 0).
  253. * an index into the code array.
  254. * @return -1 if this information is not available.
  255. */
  256. public int getLineNumber(int pos) {
  257. CodeAttribute ca = getCodeAttribute();
  258. if (ca == null)
  259. return -1;
  260. LineNumberAttribute ainfo =
  261. (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
  262. if (ainfo == null)
  263. return -1;
  264. return ainfo.toLineNumber(pos);
  265. }
  266. /**
  267. * Changes a super constructor called by this constructor.
  268. *
  269. * <p>This method modifies a call to <code>super()</code>,
  270. * which should be at the
  271. * head of a constructor body, so that a constructor in a different
  272. * super class is called. This method does not change actural
  273. * parameters. Hence the new super class must have a constructor
  274. * with the same signature as the original one.
  275. *
  276. * <p>This method should be called when the super class
  277. * of the class declaring this method is changed.
  278. *
  279. * <p>This method does not perform anything unless this
  280. * <code>MethodInfo</code> represents a constructor.
  281. *
  282. * @param superclass the new super class
  283. */
  284. public void setSuperclass(String superclass) throws BadBytecode {
  285. if (!isConstructor())
  286. return;
  287. CodeAttribute ca = getCodeAttribute();
  288. byte[] code = ca.getCode();
  289. CodeIterator iterator = ca.iterator();
  290. int pos = iterator.skipSuperConstructor();
  291. if (pos >= 0) { // not this()
  292. ConstPool cp = constPool;
  293. int mref = ByteArray.readU16bit(code, pos + 1);
  294. int nt = cp.getMethodrefNameAndType(mref);
  295. int sc = cp.addClassInfo(superclass);
  296. int mref2 = cp.addMethodrefInfo(sc, nt);
  297. ByteArray.write16bit(mref2, code, pos + 1);
  298. }
  299. }
  300. private void read(MethodInfo src, String methodname, Map classnames)
  301. throws BadBytecode
  302. {
  303. ConstPool destCp = constPool;
  304. accessFlags = src.accessFlags;
  305. name = destCp.addUtf8Info(methodname);
  306. ConstPool srcCp = src.constPool;
  307. String desc = srcCp.getUtf8Info(src.descriptor);
  308. String desc2 = Descriptor.rename(desc, classnames);
  309. descriptor = destCp.addUtf8Info(desc2);
  310. attribute = new LinkedList();
  311. ExceptionsAttribute eattr = src.getExceptionsAttribute();
  312. if (eattr != null)
  313. attribute.add(eattr.copy(destCp, classnames));
  314. CodeAttribute cattr = src.getCodeAttribute();
  315. if (cattr != null)
  316. attribute.add(cattr.copy(destCp, classnames));
  317. }
  318. private void read(DataInputStream in) throws IOException {
  319. accessFlags = in.readUnsignedShort();
  320. name = in.readUnsignedShort();
  321. descriptor = in.readUnsignedShort();
  322. int n = in.readUnsignedShort();
  323. attribute = new LinkedList();
  324. for (int i = 0; i < n; ++i)
  325. attribute.add(AttributeInfo.read(constPool, in));
  326. }
  327. void write(DataOutputStream out) throws IOException {
  328. out.writeShort(accessFlags);
  329. out.writeShort(name);
  330. out.writeShort(descriptor);
  331. if (attribute == null)
  332. out.writeShort(0);
  333. else {
  334. out.writeShort(attribute.size());
  335. AttributeInfo.writeAll(attribute, out);
  336. }
  337. }
  338. }