選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MethodInfo.java 11KB

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