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

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