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

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