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

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