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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * @param name attribute name
  283. * @return an <code>AttributeInfo</code> object or null.
  284. * @see #getAttributes()
  285. */
  286. public AttributeInfo getAttribute(String name) {
  287. return AttributeInfo.lookup(attribute, name);
  288. }
  289. /**
  290. * Appends an attribute. If there is already an attribute with the same
  291. * name, the new one substitutes for it.
  292. *
  293. * @see #getAttributes()
  294. */
  295. public void addAttribute(AttributeInfo info) {
  296. if (attribute == null)
  297. attribute = new ArrayList();
  298. AttributeInfo.remove(attribute, info.getName());
  299. attribute.add(info);
  300. }
  301. /**
  302. * Returns an Exceptions attribute.
  303. *
  304. * @return an Exceptions attribute or null if it is not specified.
  305. */
  306. public ExceptionsAttribute getExceptionsAttribute() {
  307. AttributeInfo info = AttributeInfo.lookup(attribute,
  308. ExceptionsAttribute.tag);
  309. return (ExceptionsAttribute)info;
  310. }
  311. /**
  312. * Returns a Code attribute.
  313. *
  314. * @return a Code attribute or null if it is not specified.
  315. */
  316. public CodeAttribute getCodeAttribute() {
  317. AttributeInfo info = AttributeInfo.lookup(attribute, CodeAttribute.tag);
  318. return (CodeAttribute)info;
  319. }
  320. /**
  321. * Removes an Exception attribute.
  322. */
  323. public void removeExceptionsAttribute() {
  324. AttributeInfo.remove(attribute, ExceptionsAttribute.tag);
  325. }
  326. /**
  327. * Adds an Exception attribute.
  328. *
  329. * <p>
  330. * The added attribute must share the same constant pool table as this
  331. * <code>method_info</code> structure.
  332. */
  333. public void setExceptionsAttribute(ExceptionsAttribute cattr) {
  334. removeExceptionsAttribute();
  335. if (attribute == null)
  336. attribute = new ArrayList();
  337. attribute.add(cattr);
  338. }
  339. /**
  340. * Removes a Code attribute.
  341. */
  342. public void removeCodeAttribute() {
  343. AttributeInfo.remove(attribute, CodeAttribute.tag);
  344. }
  345. /**
  346. * Adds a Code attribute.
  347. *
  348. * <p>
  349. * The added attribute must share the same constant pool table as this
  350. * <code>method_info</code> structure.
  351. */
  352. public void setCodeAttribute(CodeAttribute cattr) {
  353. removeCodeAttribute();
  354. if (attribute == null)
  355. attribute = new ArrayList();
  356. attribute.add(cattr);
  357. }
  358. /**
  359. * Rebuilds a stack map table if the class file is for Java 6
  360. * or later. Java 5 or older Java VMs do not recognize a stack
  361. * map table. If <code>doPreverify</code> is true, this method
  362. * also rebuilds a stack map for J2ME (CLDC).
  363. *
  364. * @param pool used for making type hierarchy.
  365. * @param cf rebuild if this class file is for Java 6 or later.
  366. * @see #rebuildStackMap(ClassPool)
  367. * @see #rebuildStackMapForME(ClassPool)
  368. * @see #doPreverify
  369. * @since 3.6
  370. */
  371. public void rebuildStackMapIf6(ClassPool pool, ClassFile cf)
  372. throws BadBytecode
  373. {
  374. if (cf.getMajorVersion() >= ClassFile.JAVA_6)
  375. rebuildStackMap(pool);
  376. if (doPreverify)
  377. rebuildStackMapForME(pool);
  378. }
  379. /**
  380. * Rebuilds a stack map table. 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 StackMapTable
  386. * @since 3.6
  387. */
  388. public void rebuildStackMap(ClassPool pool) throws BadBytecode {
  389. CodeAttribute ca = getCodeAttribute();
  390. if (ca != null) {
  391. StackMapTable smt = MapMaker.make(pool, this);
  392. ca.setAttribute(smt);
  393. }
  394. }
  395. /**
  396. * Rebuilds a stack map table for J2ME (CLDC). If no stack map table is included,
  397. * a new one is created. If this <code>MethodInfo</code> does not
  398. * include a code attribute, nothing happens.
  399. *
  400. * @param pool used for making type hierarchy.
  401. * @see StackMap
  402. * @since 3.12
  403. */
  404. public void rebuildStackMapForME(ClassPool pool) throws BadBytecode {
  405. CodeAttribute ca = getCodeAttribute();
  406. if (ca != null) {
  407. StackMap sm = MapMaker.make2(pool, this);
  408. ca.setAttribute(sm);
  409. }
  410. }
  411. /**
  412. * Returns the line number of the source line corresponding to the specified
  413. * bytecode contained in this method.
  414. *
  415. * @param pos
  416. * the position of the bytecode (&gt;= 0). an index into the code
  417. * array.
  418. * @return -1 if this information is not available.
  419. */
  420. public int getLineNumber(int pos) {
  421. CodeAttribute ca = getCodeAttribute();
  422. if (ca == null)
  423. return -1;
  424. LineNumberAttribute ainfo = (LineNumberAttribute)ca
  425. .getAttribute(LineNumberAttribute.tag);
  426. if (ainfo == null)
  427. return -1;
  428. return ainfo.toLineNumber(pos);
  429. }
  430. /**
  431. * Changes a super constructor called by this constructor.
  432. *
  433. * <p>
  434. * This method modifies a call to <code>super()</code>, which should be
  435. * at the head of a constructor body, so that a constructor in a different
  436. * super class is called. This method does not change actual parameters.
  437. * Hence the new super class must have a constructor with the same signature
  438. * as the original one.
  439. *
  440. * <p>
  441. * This method should be called when the super class of the class declaring
  442. * this method is changed.
  443. *
  444. * <p>
  445. * This method does not perform anything unless this <code>MethodInfo</code>
  446. * represents a constructor.
  447. *
  448. * @param superclass
  449. * the new super class
  450. */
  451. public void setSuperclass(String superclass) throws BadBytecode {
  452. if (!isConstructor())
  453. return;
  454. CodeAttribute ca = getCodeAttribute();
  455. byte[] code = ca.getCode();
  456. CodeIterator iterator = ca.iterator();
  457. int pos = iterator.skipSuperConstructor();
  458. if (pos >= 0) { // not this()
  459. ConstPool cp = constPool;
  460. int mref = ByteArray.readU16bit(code, pos + 1);
  461. int nt = cp.getMethodrefNameAndType(mref);
  462. int sc = cp.addClassInfo(superclass);
  463. int mref2 = cp.addMethodrefInfo(sc, nt);
  464. ByteArray.write16bit(mref2, code, pos + 1);
  465. }
  466. }
  467. private void read(MethodInfo src, String methodname, Map classnames)
  468. throws BadBytecode {
  469. ConstPool destCp = constPool;
  470. accessFlags = src.accessFlags;
  471. name = destCp.addUtf8Info(methodname);
  472. cachedName = methodname;
  473. ConstPool srcCp = src.constPool;
  474. String desc = srcCp.getUtf8Info(src.descriptor);
  475. String desc2 = Descriptor.rename(desc, classnames);
  476. descriptor = destCp.addUtf8Info(desc2);
  477. attribute = new ArrayList();
  478. ExceptionsAttribute eattr = src.getExceptionsAttribute();
  479. if (eattr != null)
  480. attribute.add(eattr.copy(destCp, classnames));
  481. CodeAttribute cattr = src.getCodeAttribute();
  482. if (cattr != null)
  483. attribute.add(cattr.copy(destCp, classnames));
  484. }
  485. private void read(DataInputStream in) throws IOException {
  486. accessFlags = in.readUnsignedShort();
  487. name = in.readUnsignedShort();
  488. descriptor = in.readUnsignedShort();
  489. int n = in.readUnsignedShort();
  490. attribute = new ArrayList();
  491. for (int i = 0; i < n; ++i)
  492. attribute.add(AttributeInfo.read(constPool, in));
  493. }
  494. void write(DataOutputStream out) throws IOException {
  495. out.writeShort(accessFlags);
  496. out.writeShort(name);
  497. out.writeShort(descriptor);
  498. if (attribute == null)
  499. out.writeShort(0);
  500. else {
  501. out.writeShort(attribute.size());
  502. AttributeInfo.writeAll(attribute, out);
  503. }
  504. }
  505. }