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

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