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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.io.DataInputStream;
  17. import java.io.DataOutputStream;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.List;
  21. import java.util.LinkedList;
  22. import javassist.CannotCompileException;
  23. /**
  24. * <code>method_info</code> structure.
  25. *
  26. * @see javassist.CtMethod#getMethodInfo()
  27. * @see javassist.CtConstructor#getMethodInfo()
  28. */
  29. public final class MethodInfo {
  30. ConstPool constPool;
  31. int accessFlags;
  32. int name;
  33. int descriptor;
  34. LinkedList attribute; // may be null
  35. /**
  36. * The name of constructors: <code>&lt;init&gt</code>.
  37. */
  38. public static final String nameInit = "<init>";
  39. /**
  40. * The name of class initializer (static initializer):
  41. * <code>&lt;clinit&gt</code>.
  42. */
  43. public static final String nameClinit = "<clinit>";
  44. private MethodInfo(ConstPool cp) {
  45. constPool = cp;
  46. attribute = null;
  47. }
  48. /**
  49. * Constructs a <code>method_info</code> structure.
  50. *
  51. * @param cp a constant pool table
  52. * @param methodname method name
  53. * @param desc method descriptor
  54. *
  55. * @see Descriptor
  56. */
  57. public MethodInfo(ConstPool cp, String methodname, String desc) {
  58. this(cp);
  59. accessFlags = 0;
  60. name = cp.addUtf8Info(methodname);
  61. descriptor = constPool.addUtf8Info(desc);
  62. }
  63. MethodInfo(ConstPool cp, DataInputStream in) throws IOException {
  64. this(cp);
  65. read(in);
  66. }
  67. /**
  68. * Constructs a copy of <code>method_info</code> structure.
  69. * Class names appearing in the source <code>method_info</code>
  70. * are renamed according to <code>classnameMap</code>.
  71. *
  72. * <p>Note: only <code>Code</code> and <code>Exceptions</code>
  73. * attributes are copied from the source. The other attributes
  74. * are ignored.
  75. *
  76. * @param cp a constant pool table
  77. * @param methodname a method name
  78. * @param src a source <code>method_info</code>
  79. * @param classnameMap specifies pairs of replaced and substituted
  80. * name.
  81. * @see Descriptor
  82. */
  83. public MethodInfo(ConstPool cp, String methodname, MethodInfo src,
  84. Map classnameMap) throws BadBytecode
  85. {
  86. this(cp);
  87. read(src, methodname, classnameMap);
  88. }
  89. /**
  90. * Returns a method name.
  91. */
  92. public String getName() {
  93. return constPool.getUtf8Info(name);
  94. }
  95. /**
  96. * Sets a method name.
  97. */
  98. public void setName(String newName) {
  99. name = constPool.addUtf8Info(newName);
  100. }
  101. /**
  102. * Returns true if this is not a constructor or a class initializer
  103. * (static initializer).
  104. */
  105. public boolean isMethod() {
  106. String n = getName();
  107. return !n.equals(nameInit) && !n.equals(nameClinit);
  108. }
  109. /**
  110. * Returns a constant pool table used by this method.
  111. */
  112. public ConstPool getConstPool() { return constPool; }
  113. /**
  114. * Returns true if this is a constructor.
  115. */
  116. public boolean isConstructor() { return getName().equals(nameInit); }
  117. /**
  118. * Returns true if this is a class initializer (static initializer).
  119. */
  120. public boolean isStaticInitializer() {
  121. return getName().equals(nameClinit);
  122. }
  123. /**
  124. * Returns access flags.
  125. *
  126. * @see AccessFlag
  127. */
  128. public int getAccessFlags() {
  129. return accessFlags;
  130. }
  131. /**
  132. * Sets access flags.
  133. *
  134. * @see AccessFlag
  135. */
  136. public void setAccessFlags(int acc) {
  137. accessFlags = acc;
  138. }
  139. /**
  140. * Returns a method descriptor.
  141. *
  142. * @see Descriptor
  143. */
  144. public String getDescriptor() {
  145. return constPool.getUtf8Info(descriptor);
  146. }
  147. /**
  148. * Sets a method descriptor.
  149. *
  150. * @see Descriptor
  151. */
  152. public void setDescriptor(String desc) {
  153. if (!desc.equals(getDescriptor()))
  154. descriptor = constPool.addUtf8Info(desc);
  155. }
  156. /**
  157. * Returns all the attributes.
  158. *
  159. * @return a list of <code>AttributeInfo</code> objects.
  160. * @see AttributeInfo
  161. */
  162. public List getAttributes() {
  163. if (attribute == null)
  164. attribute = new LinkedList();
  165. return attribute;
  166. }
  167. /**
  168. * Returns the attribute with the specified name.
  169. * If it is not found, this method returns null.
  170. *
  171. * @param name attribute name
  172. * @return an <code>AttributeInfo</code> object or null.
  173. */
  174. public AttributeInfo getAttribute(String name) {
  175. return AttributeInfo.lookup(attribute, name);
  176. }
  177. /**
  178. * Appends an attribute. If there is already an attribute with
  179. * the same name, the new one substitutes for it.
  180. */
  181. public void addAttribute(AttributeInfo info) {
  182. if (attribute == null)
  183. attribute = new LinkedList();
  184. AttributeInfo.remove(attribute, info.getName());
  185. attribute.add(info);
  186. }
  187. /**
  188. * Returns an Exceptions attribute.
  189. *
  190. * @return an Exceptions attribute
  191. * or null if it is not specified.
  192. */
  193. public ExceptionsAttribute getExceptionsAttribute() {
  194. AttributeInfo info
  195. = AttributeInfo.lookup(attribute, ExceptionsAttribute.class);
  196. return (ExceptionsAttribute)info;
  197. }
  198. /**
  199. * Returns a Code attribute.
  200. *
  201. * @return a Code attribute
  202. * or null if it is not specified.
  203. */
  204. public CodeAttribute getCodeAttribute() {
  205. AttributeInfo info
  206. = AttributeInfo.lookup(attribute, CodeAttribute.class);
  207. return (CodeAttribute)info;
  208. }
  209. /**
  210. * Removes an Exception attribute.
  211. */
  212. public void removeExceptionsAttribute() {
  213. AttributeInfo.remove(attribute, ExceptionsAttribute.class);
  214. }
  215. /**
  216. * Adds an Exception attribute.
  217. *
  218. * <p>The added attribute must share the same constant pool table
  219. * as this <code>method_info</code> structure.
  220. */
  221. public void setExceptionsAttribute(ExceptionsAttribute cattr) {
  222. removeExceptionsAttribute();
  223. if (attribute == null)
  224. attribute = new LinkedList();
  225. attribute.add(cattr);
  226. }
  227. /**
  228. * Removes a Code attribute.
  229. */
  230. public void removeCodeAttribute() {
  231. AttributeInfo.remove(attribute, CodeAttribute.class);
  232. }
  233. /**
  234. * Adds a Code attribute.
  235. *
  236. * <p>The added attribute must share the same constant pool table
  237. * as this <code>method_info</code> structure.
  238. */
  239. public void setCodeAttribute(CodeAttribute cattr) {
  240. removeCodeAttribute();
  241. if (attribute == null)
  242. attribute = new LinkedList();
  243. attribute.add(cattr);
  244. }
  245. /**
  246. * Returns the line number of the source line corresponding to the
  247. * specified bytecode contained in this method.
  248. *
  249. * @param pos the position of the bytecode (&gt;= 0).
  250. * an index into the code array.
  251. * @return -1 if this information is not available.
  252. */
  253. public int getLineNumber(int pos) {
  254. CodeAttribute ca = getCodeAttribute();
  255. if (ca == null)
  256. return -1;
  257. LineNumberAttribute ainfo
  258. = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
  259. if (ainfo == null)
  260. return -1;
  261. return ainfo.toLineNumber(pos);
  262. }
  263. /**
  264. * Changes a super constructor called by this constructor.
  265. *
  266. * <p>This method modifies a call to <code>super()</code>,
  267. * which should be at the
  268. * head of a constructor body, so that a constructor in a different
  269. * super class is called. This method does not change actural
  270. * parameters. Hence the new super class must have a constructor
  271. * with the same signature as the original one.
  272. *
  273. * <p>This method should be called when the super class
  274. * of the class declaring this method is changed.
  275. *
  276. * <p>This method does not perform anything unless this
  277. * <code>MethodInfo</code> represents a constructor.
  278. *
  279. * @param superclass the new super class
  280. */
  281. public void setSuperclass(String superclass) throws BadBytecode {
  282. if (!isConstructor())
  283. return;
  284. CodeAttribute ca = getCodeAttribute();
  285. byte[] code = ca.getCode();
  286. CodeIterator iterator = ca.iterator();
  287. int pos = iterator.skipSuperConstructor();
  288. if (pos >= 0) { // not this()
  289. ConstPool cp = constPool;
  290. int mref = ByteArray.readU16bit(code, pos + 1);
  291. int nt = cp.getMethodrefNameAndType(mref);
  292. int sc = cp.addClassInfo(superclass);
  293. int mref2 = cp.addMethodrefInfo(sc, nt);
  294. ByteArray.write16bit(mref2, code, pos + 1);
  295. }
  296. }
  297. private void read(MethodInfo src, String methodname, Map classnames)
  298. throws BadBytecode
  299. {
  300. ConstPool destCp = constPool;
  301. accessFlags = src.accessFlags;
  302. name = destCp.addUtf8Info(methodname);
  303. ConstPool srcCp = src.constPool;
  304. String desc = srcCp.getUtf8Info(src.descriptor);
  305. String desc2 = Descriptor.rename(desc, classnames);
  306. descriptor = destCp.addUtf8Info(desc2);
  307. attribute = new LinkedList();
  308. ExceptionsAttribute eattr = src.getExceptionsAttribute();
  309. if (eattr != null)
  310. attribute.add(eattr.copy(destCp, classnames));
  311. CodeAttribute cattr = src.getCodeAttribute();
  312. if (cattr != null)
  313. attribute.add(cattr.copy(destCp, classnames));
  314. }
  315. private void read(DataInputStream in) throws IOException {
  316. accessFlags = in.readUnsignedShort();
  317. name = in.readUnsignedShort();
  318. descriptor = in.readUnsignedShort();
  319. int n = in.readUnsignedShort();
  320. attribute = new LinkedList();
  321. for (int i = 0; i < n; ++i)
  322. attribute.add(AttributeInfo.read(constPool, in));
  323. }
  324. void write(DataOutputStream out) throws IOException {
  325. out.writeShort(accessFlags);
  326. out.writeShort(name);
  327. out.writeShort(descriptor);
  328. if (attribute == null)
  329. out.writeShort(0);
  330. else {
  331. out.writeShort(attribute.size());
  332. AttributeInfo.writeAll(attribute, out);
  333. }
  334. }
  335. }