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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import javassist.ClassPool;
  24. import javassist.bytecode.stackmap.MapMaker;
  25. /**
  26. * <code>method_info</code> structure.
  27. *
  28. * <p>The bytecode sequence of the method is represented
  29. * by a <code>CodeAttribute</code> object.
  30. *
  31. * @see #getCodeAttribute()
  32. * @see CodeAttribute
  33. * @see javassist.CtMethod#getMethodInfo()
  34. * @see javassist.CtConstructor#getMethodInfo()
  35. */
  36. public class MethodInfo {
  37. ConstPool constPool;
  38. int accessFlags;
  39. int name;
  40. String cachedName;
  41. int descriptor;
  42. ArrayList attribute; // may be null
  43. /**
  44. * If this value is true, Javassist maintains a <code>StackMap</code> attribute
  45. * generated by the <code>preverify</code> tool of J2ME (CLDC). The initial
  46. * value of this field is <code>false</code>.
  47. */
  48. public static boolean doPreverify = false;
  49. /**
  50. * The name of constructors: <code>&lt;init&gt;</code>.
  51. */
  52. public static final String nameInit = "<init>";
  53. /**
  54. * The name of class initializer (static initializer):
  55. * <code>&lt;clinit&gt;</code>.
  56. */
  57. public static final String nameClinit = "<clinit>";
  58. private MethodInfo(ConstPool cp) {
  59. constPool = cp;
  60. attribute = null;
  61. }
  62. /**
  63. * Constructs a <code>method_info</code> structure. The initial value of
  64. * <code>access_flags</code> is zero.
  65. *
  66. * @param cp
  67. * a constant pool table
  68. * @param methodname
  69. * method name
  70. * @param desc
  71. * method descriptor
  72. * @see Descriptor
  73. */
  74. public MethodInfo(ConstPool cp, String methodname, String desc) {
  75. this(cp);
  76. accessFlags = 0;
  77. name = cp.addUtf8Info(methodname);
  78. cachedName = methodname;
  79. descriptor = constPool.addUtf8Info(desc);
  80. }
  81. MethodInfo(ConstPool cp, DataInputStream in) throws IOException {
  82. this(cp);
  83. read(in);
  84. }
  85. /**
  86. * Constructs a copy of <code>method_info</code> structure. Class names
  87. * appearing in the source <code>method_info</code> are renamed according
  88. * to <code>classnameMap</code>.
  89. *
  90. * <p>
  91. * Note: only <code>Code</code> and <code>Exceptions</code> attributes
  92. * are copied from the source. The other attributes are ignored.
  93. *
  94. * @param cp
  95. * a constant pool table
  96. * @param methodname
  97. * a method name
  98. * @param src
  99. * a source <code>method_info</code>
  100. * @param classnameMap
  101. * specifies pairs of replaced and substituted name.
  102. * @see Descriptor
  103. */
  104. public MethodInfo(ConstPool cp, String methodname, MethodInfo src,
  105. Map classnameMap) throws BadBytecode {
  106. this(cp);
  107. read(src, methodname, classnameMap);
  108. }
  109. /**
  110. * Returns a string representation of the object.
  111. */
  112. public String toString() {
  113. return getName() + " " + getDescriptor();
  114. }
  115. /**
  116. * Copies all constant pool items to a given new constant pool
  117. * and replaces the original items with the new ones.
  118. * This is used for garbage collecting the items of removed fields
  119. * and methods.
  120. *
  121. * @param cp the destination
  122. */
  123. void compact(ConstPool cp) {
  124. name = cp.addUtf8Info(getName());
  125. descriptor = cp.addUtf8Info(getDescriptor());
  126. attribute = AttributeInfo.copyAll(attribute, cp);
  127. constPool = cp;
  128. }
  129. void prune(ConstPool cp) {
  130. ArrayList newAttributes = new ArrayList();
  131. AttributeInfo invisibleAnnotations
  132. = getAttribute(AnnotationsAttribute.invisibleTag);
  133. if (invisibleAnnotations != null) {
  134. invisibleAnnotations = invisibleAnnotations.copy(cp, null);
  135. newAttributes.add(invisibleAnnotations);
  136. }
  137. AttributeInfo visibleAnnotations
  138. = getAttribute(AnnotationsAttribute.visibleTag);
  139. if (visibleAnnotations != null) {
  140. visibleAnnotations = visibleAnnotations.copy(cp, null);
  141. newAttributes.add(visibleAnnotations);
  142. }
  143. AttributeInfo parameterInvisibleAnnotations
  144. = getAttribute(ParameterAnnotationsAttribute.invisibleTag);
  145. if (parameterInvisibleAnnotations != null) {
  146. parameterInvisibleAnnotations = parameterInvisibleAnnotations.copy(cp, null);
  147. newAttributes.add(parameterInvisibleAnnotations);
  148. }
  149. AttributeInfo parameterVisibleAnnotations
  150. = getAttribute(ParameterAnnotationsAttribute.visibleTag);
  151. if (parameterVisibleAnnotations != null) {
  152. parameterVisibleAnnotations = parameterVisibleAnnotations.copy(cp, null);
  153. newAttributes.add(parameterVisibleAnnotations);
  154. }
  155. AnnotationDefaultAttribute defaultAttribute
  156. = (AnnotationDefaultAttribute) getAttribute(AnnotationDefaultAttribute.tag);
  157. if (defaultAttribute != null)
  158. newAttributes.add(defaultAttribute);
  159. ExceptionsAttribute ea = getExceptionsAttribute();
  160. if (ea != null)
  161. newAttributes.add(ea);
  162. AttributeInfo signature
  163. = getAttribute(SignatureAttribute.tag);
  164. if (signature != null) {
  165. signature = signature.copy(cp, null);
  166. newAttributes.add(signature);
  167. }
  168. attribute = newAttributes;
  169. name = cp.addUtf8Info(getName());
  170. descriptor = cp.addUtf8Info(getDescriptor());
  171. constPool = cp;
  172. }
  173. /**
  174. * Returns a method name.
  175. */
  176. public String getName() {
  177. if (cachedName == null)
  178. cachedName = constPool.getUtf8Info(name);
  179. return cachedName;
  180. }
  181. /**
  182. * Sets a method name.
  183. */
  184. public void setName(String newName) {
  185. name = constPool.addUtf8Info(newName);
  186. cachedName = newName;
  187. }
  188. /**
  189. * Returns true if this is not a constructor or a class initializer (static
  190. * initializer).
  191. */
  192. public boolean isMethod() {
  193. String n = getName();
  194. return !n.equals(nameInit) && !n.equals(nameClinit);
  195. }
  196. /**
  197. * Returns a constant pool table used by this method.
  198. */
  199. public ConstPool getConstPool() {
  200. return constPool;
  201. }
  202. /**
  203. * Returns true if this is a constructor.
  204. */
  205. public boolean isConstructor() {
  206. return getName().equals(nameInit);
  207. }
  208. /**
  209. * Returns true if this is a class initializer (static initializer).
  210. */
  211. public boolean isStaticInitializer() {
  212. return getName().equals(nameClinit);
  213. }
  214. /**
  215. * Returns access flags.
  216. *
  217. * @see AccessFlag
  218. */
  219. public int getAccessFlags() {
  220. return accessFlags;
  221. }
  222. /**
  223. * Sets access flags.
  224. *
  225. * @see AccessFlag
  226. */
  227. public void setAccessFlags(int acc) {
  228. accessFlags = acc;
  229. }
  230. /**
  231. * Returns a method descriptor.
  232. *
  233. * @see Descriptor
  234. */
  235. public String getDescriptor() {
  236. return constPool.getUtf8Info(descriptor);
  237. }
  238. /**
  239. * Sets a method descriptor.
  240. *
  241. * @see Descriptor
  242. */
  243. public void setDescriptor(String desc) {
  244. if (!desc.equals(getDescriptor()))
  245. descriptor = constPool.addUtf8Info(desc);
  246. }
  247. /**
  248. * Returns all the attributes. The returned <code>List</code> object
  249. * is shared with this object. If you add a new attribute to the list,
  250. * the attribute is also added to the method represented by this
  251. * object. If you remove an attribute from the list, it is also removed
  252. * from the method.
  253. *
  254. * @return a list of <code>AttributeInfo</code> objects.
  255. * @see AttributeInfo
  256. */
  257. public List getAttributes() {
  258. if (attribute == null)
  259. attribute = new ArrayList();
  260. return attribute;
  261. }
  262. /**
  263. * Returns the attribute with the specified name. If it is not found, this
  264. * method returns null.
  265. *
  266. * @param name attribute name
  267. * @return an <code>AttributeInfo</code> object or null.
  268. * @see #getAttributes()
  269. */
  270. public AttributeInfo getAttribute(String name) {
  271. return AttributeInfo.lookup(attribute, name);
  272. }
  273. /**
  274. * Appends an attribute. If there is already an attribute with the same
  275. * name, the new one substitutes for it.
  276. *
  277. * @see #getAttributes()
  278. */
  279. public void addAttribute(AttributeInfo info) {
  280. if (attribute == null)
  281. attribute = new ArrayList();
  282. AttributeInfo.remove(attribute, info.getName());
  283. attribute.add(info);
  284. }
  285. /**
  286. * Returns an Exceptions attribute.
  287. *
  288. * @return an Exceptions attribute or null if it is not specified.
  289. */
  290. public ExceptionsAttribute getExceptionsAttribute() {
  291. AttributeInfo info = AttributeInfo.lookup(attribute,
  292. ExceptionsAttribute.tag);
  293. return (ExceptionsAttribute)info;
  294. }
  295. /**
  296. * Returns a Code attribute.
  297. *
  298. * @return a Code attribute or null if it is not specified.
  299. */
  300. public CodeAttribute getCodeAttribute() {
  301. AttributeInfo info = AttributeInfo.lookup(attribute, CodeAttribute.tag);
  302. return (CodeAttribute)info;
  303. }
  304. /**
  305. * Removes an Exception attribute.
  306. */
  307. public void removeExceptionsAttribute() {
  308. AttributeInfo.remove(attribute, ExceptionsAttribute.tag);
  309. }
  310. /**
  311. * Adds an Exception attribute.
  312. *
  313. * <p>
  314. * The added attribute must share the same constant pool table as this
  315. * <code>method_info</code> structure.
  316. */
  317. public void setExceptionsAttribute(ExceptionsAttribute cattr) {
  318. removeExceptionsAttribute();
  319. if (attribute == null)
  320. attribute = new ArrayList();
  321. attribute.add(cattr);
  322. }
  323. /**
  324. * Removes a Code attribute.
  325. */
  326. public void removeCodeAttribute() {
  327. AttributeInfo.remove(attribute, CodeAttribute.tag);
  328. }
  329. /**
  330. * Adds a Code attribute.
  331. *
  332. * <p>
  333. * The added attribute must share the same constant pool table as this
  334. * <code>method_info</code> structure.
  335. */
  336. public void setCodeAttribute(CodeAttribute cattr) {
  337. removeCodeAttribute();
  338. if (attribute == null)
  339. attribute = new ArrayList();
  340. attribute.add(cattr);
  341. }
  342. /**
  343. * Rebuilds a stack map table if the class file is for Java 6
  344. * or later. Java 5 or older Java VMs do not recognize a stack
  345. * map table. If <code>doPreverify</code> is true, this method
  346. * also rebuilds a stack map for J2ME (CLDC).
  347. *
  348. * @param pool used for making type hierarchy.
  349. * @param cf rebuild if this class file is for Java 6 or later.
  350. * @see #rebuildStackMap(ClassPool)
  351. * @see #rebuildStackMapForME(ClassPool)
  352. * @see #doPreverify
  353. * @since 3.6
  354. */
  355. public void rebuildStackMapIf6(ClassPool pool, ClassFile cf)
  356. throws BadBytecode
  357. {
  358. if (cf.getMajorVersion() >= ClassFile.JAVA_6)
  359. rebuildStackMap(pool);
  360. if (doPreverify)
  361. rebuildStackMapForME(pool);
  362. }
  363. /**
  364. * Rebuilds a stack map table. If no stack map table is included,
  365. * a new one is created. If this <code>MethodInfo</code> does not
  366. * include a code attribute, nothing happens.
  367. *
  368. * @param pool used for making type hierarchy.
  369. * @see StackMapTable
  370. * @since 3.6
  371. */
  372. public void rebuildStackMap(ClassPool pool) throws BadBytecode {
  373. CodeAttribute ca = getCodeAttribute();
  374. if (ca != null) {
  375. StackMapTable smt = MapMaker.make(pool, this);
  376. ca.setAttribute(smt);
  377. }
  378. }
  379. /**
  380. * Rebuilds a stack map table for J2ME (CLDC). If no stack map table is included,
  381. * a new one is created. If this <code>MethodInfo</code> does not
  382. * include a code attribute, nothing happens.
  383. *
  384. * @param pool used for making type hierarchy.
  385. * @see StackMap
  386. * @since 3.12
  387. */
  388. public void rebuildStackMapForME(ClassPool pool) throws BadBytecode {
  389. CodeAttribute ca = getCodeAttribute();
  390. if (ca != null) {
  391. StackMap sm = MapMaker.make2(pool, this);
  392. ca.setAttribute(sm);
  393. }
  394. }
  395. /**
  396. * Returns the line number of the source line corresponding to the specified
  397. * bytecode contained in this method.
  398. *
  399. * @param pos
  400. * the position of the bytecode (&gt;= 0). an index into the code
  401. * array.
  402. * @return -1 if this information is not available.
  403. */
  404. public int getLineNumber(int pos) {
  405. CodeAttribute ca = getCodeAttribute();
  406. if (ca == null)
  407. return -1;
  408. LineNumberAttribute ainfo = (LineNumberAttribute)ca
  409. .getAttribute(LineNumberAttribute.tag);
  410. if (ainfo == null)
  411. return -1;
  412. return ainfo.toLineNumber(pos);
  413. }
  414. /**
  415. * Changes a super constructor called by this constructor.
  416. *
  417. * <p>
  418. * This method modifies a call to <code>super()</code>, which should be
  419. * at the head of a constructor body, so that a constructor in a different
  420. * super class is called. This method does not change actual parameters.
  421. * Hence the new super class must have a constructor with the same signature
  422. * as the original one.
  423. *
  424. * <p>
  425. * This method should be called when the super class of the class declaring
  426. * this method is changed.
  427. *
  428. * <p>
  429. * This method does not perform anything unless this <code>MethodInfo</code>
  430. * represents a constructor.
  431. *
  432. * @param superclass
  433. * the new super class
  434. */
  435. public void setSuperclass(String superclass) throws BadBytecode {
  436. if (!isConstructor())
  437. return;
  438. CodeAttribute ca = getCodeAttribute();
  439. byte[] code = ca.getCode();
  440. CodeIterator iterator = ca.iterator();
  441. int pos = iterator.skipSuperConstructor();
  442. if (pos >= 0) { // not this()
  443. ConstPool cp = constPool;
  444. int mref = ByteArray.readU16bit(code, pos + 1);
  445. int nt = cp.getMethodrefNameAndType(mref);
  446. int sc = cp.addClassInfo(superclass);
  447. int mref2 = cp.addMethodrefInfo(sc, nt);
  448. ByteArray.write16bit(mref2, code, pos + 1);
  449. }
  450. }
  451. private void read(MethodInfo src, String methodname, Map classnames)
  452. throws BadBytecode {
  453. ConstPool destCp = constPool;
  454. accessFlags = src.accessFlags;
  455. name = destCp.addUtf8Info(methodname);
  456. cachedName = methodname;
  457. ConstPool srcCp = src.constPool;
  458. String desc = srcCp.getUtf8Info(src.descriptor);
  459. String desc2 = Descriptor.rename(desc, classnames);
  460. descriptor = destCp.addUtf8Info(desc2);
  461. attribute = new ArrayList();
  462. ExceptionsAttribute eattr = src.getExceptionsAttribute();
  463. if (eattr != null)
  464. attribute.add(eattr.copy(destCp, classnames));
  465. CodeAttribute cattr = src.getCodeAttribute();
  466. if (cattr != null)
  467. attribute.add(cattr.copy(destCp, classnames));
  468. }
  469. private void read(DataInputStream in) throws IOException {
  470. accessFlags = in.readUnsignedShort();
  471. name = in.readUnsignedShort();
  472. descriptor = in.readUnsignedShort();
  473. int n = in.readUnsignedShort();
  474. attribute = new ArrayList();
  475. for (int i = 0; i < n; ++i)
  476. attribute.add(AttributeInfo.read(constPool, in));
  477. }
  478. void write(DataOutputStream out) throws IOException {
  479. out.writeShort(accessFlags);
  480. out.writeShort(name);
  481. out.writeShort(descriptor);
  482. if (attribute == null)
  483. out.writeShort(0);
  484. else {
  485. out.writeShort(attribute.size());
  486. AttributeInfo.writeAll(attribute, out);
  487. }
  488. }
  489. }