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.

CtMethod.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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;
  16. import javassist.bytecode.*;
  17. /**
  18. * An instance of <code>CtMethod</code> represents a method.
  19. *
  20. * <p>See the super class <code>CtBehavior</code> since
  21. * a number of useful methods are in <code>CtBehavior</code>.
  22. * A number of useful factory methods are in <code>CtNewMethod</code>.
  23. *
  24. * @see CtClass#getDeclaredMethods()
  25. * @see CtNewMethod
  26. */
  27. public final class CtMethod extends CtBehavior {
  28. protected String cachedStringRep;
  29. CtMethod(MethodInfo minfo, CtClass declaring) {
  30. super(declaring, minfo);
  31. next = null;
  32. cachedStringRep = null;
  33. }
  34. /**
  35. * Creates a public abstract method. The created method must be
  36. * added to a class with <code>CtClass.addMethod()</code>.
  37. *
  38. * @param declaring the class to which the created method is added.
  39. * @param returnType the type of the returned value
  40. * @param mname the method name
  41. * @param parameters a list of the parameter types
  42. *
  43. * @see CtClass#addMethod(CtMethod)
  44. */
  45. public CtMethod(CtClass returnType, String mname,
  46. CtClass[] parameters, CtClass declaring) {
  47. this(null, declaring);
  48. ConstPool cp = declaring.getClassFile2().getConstPool();
  49. String desc = Descriptor.ofMethod(returnType, parameters);
  50. methodInfo = new MethodInfo(cp, mname, desc);
  51. setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
  52. }
  53. /**
  54. * Creates a copy of a <code>CtMethod</code> object.
  55. * The created method must be
  56. * added to a class with <code>CtClass.addMethod()</code>.
  57. *
  58. * <p>All occurrences of class names in the created method
  59. * are replaced with names specified by
  60. * <code>map</code> if <code>map</code> is not <code>null</code>.
  61. *
  62. * <p>For example, suppose that a method <code>at()</code> is as
  63. * follows:
  64. *
  65. * <ul><pre>public X at(int i) {
  66. * return (X)super.elementAt(i);
  67. * }</pre></ul>
  68. *
  69. * <p>(<code>X</code> is a class name.) If <code>map</code> substitutes
  70. * <code>String</code> for <code>X</code>, then the created method is:
  71. *
  72. * <ul><pre>public String at(int i) {
  73. * return (String)super.elementAt(i);
  74. * }</pre></ul>
  75. *
  76. * <p>By default, all the occurrences of the names of the class
  77. * declaring <code>at()</code> and the superclass are replaced
  78. * with the name of the class and the superclass that the
  79. * created method is added to.
  80. * This is done whichever <code>map</code> is null or not.
  81. * To prevent this replacement, call <code>ClassMap.fix()</code>.
  82. *
  83. * <p><b>Note:</b> if the <code>.class</code> notation (for example,
  84. * <code>String.class</code>) is included in an expression, the
  85. * Javac compiler may produce a helper method.
  86. * Since this constructor never
  87. * copies this helper method, the programmers have the responsiblity of
  88. * copying it. Otherwise, use <code>Class.forName()</code> in the
  89. * expression.
  90. *
  91. * @param src the source method.
  92. * @param declaring the class to which the created method is added.
  93. * @param map the hashtable associating original class names
  94. * with substituted names.
  95. * It can be <code>null</code>.
  96. *
  97. * @see CtClass#addMethod(CtMethod)
  98. * @see ClassMap#fix(String)
  99. */
  100. public CtMethod(CtMethod src, CtClass declaring, ClassMap map)
  101. throws CannotCompileException
  102. {
  103. this(null, declaring);
  104. copy(src, false, map);
  105. }
  106. /**
  107. * Compiles the given source code and creates a method.
  108. * This method simply delegates to <code>make()</code> in
  109. * <code>CtNewMethod</code>. See it for more details.
  110. * <code>CtNewMethod</code> has a number of useful factory methods.
  111. *
  112. * @param src the source text.
  113. * @param declaring the class to which the created method is added.
  114. * @see CtNewMethod#make(String, CtClass)
  115. */
  116. public static CtMethod make(String src, CtClass declaring)
  117. throws CannotCompileException
  118. {
  119. return CtNewMethod.make(src, declaring);
  120. }
  121. /**
  122. * Returns a hash code value for the method.
  123. * If two methods have the same name and signature, then
  124. * the hash codes for the two methods are equal.
  125. */
  126. public int hashCode() {
  127. return getStringRep().hashCode();
  128. }
  129. /* This method is also called by CtClassType.getMethods0().
  130. */
  131. final String getStringRep() {
  132. if (cachedStringRep == null)
  133. cachedStringRep = methodInfo.getName()
  134. + Descriptor.getParamDescriptor(methodInfo.getDescriptor());
  135. return cachedStringRep;
  136. }
  137. /**
  138. * Indicates whether <code>obj</code> has the same name and the
  139. * same signature as this method.
  140. */
  141. public boolean equals(Object obj) {
  142. return obj != null && obj instanceof CtMethod
  143. && ((CtMethod)obj).getStringRep().equals(getStringRep());
  144. }
  145. /**
  146. * Obtains the name of this method.
  147. */
  148. public String getName() {
  149. return methodInfo.getName();
  150. }
  151. /**
  152. * Changes the name of this method.
  153. */
  154. public void setName(String newname) {
  155. declaringClass.checkModify();
  156. methodInfo.setName(newname);
  157. }
  158. /**
  159. * Obtains the type of the returned value.
  160. */
  161. public CtClass getReturnType() throws NotFoundException {
  162. return getReturnType0();
  163. }
  164. /**
  165. * Returns true if the method body is empty, that is, <code>{}</code>.
  166. * It also returns true if the method is an abstract method.
  167. */
  168. public boolean isEmpty() {
  169. CodeAttribute ca = getMethodInfo2().getCodeAttribute();
  170. if (ca == null) // abstract or native
  171. return (getModifiers() & Modifier.ABSTRACT) != 0;
  172. CodeIterator it = ca.iterator();
  173. try {
  174. return it.hasNext() && it.byteAt(it.next()) == Opcode.RETURN
  175. && !it.hasNext();
  176. }
  177. catch (BadBytecode e) {}
  178. return false;
  179. }
  180. /**
  181. * Copies a method body from another method.
  182. * If this method is abstract, the abstract modifier is removed
  183. * after the method body is copied.
  184. *
  185. * <p>All occurrences of the class names in the copied method body
  186. * are replaced with the names specified by
  187. * <code>map</code> if <code>map</code> is not <code>null</code>.
  188. *
  189. * @param src the method that the body is copied from.
  190. * @param map the hashtable associating original class names
  191. * with substituted names.
  192. * It can be <code>null</code>.
  193. */
  194. public void setBody(CtMethod src, ClassMap map)
  195. throws CannotCompileException
  196. {
  197. setBody0(src.declaringClass, src.methodInfo,
  198. declaringClass, methodInfo, map);
  199. }
  200. /**
  201. * Replace a method body with a new method body wrapping the
  202. * given method.
  203. *
  204. * @param mbody the wrapped method
  205. * @param constParam the constant parameter given to
  206. * the wrapped method
  207. * (maybe <code>null</code>).
  208. *
  209. * @see CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
  210. */
  211. public void setWrappedBody(CtMethod mbody, ConstParameter constParam)
  212. throws CannotCompileException
  213. {
  214. declaringClass.checkModify();
  215. CtClass clazz = getDeclaringClass();
  216. CtClass[] params;
  217. CtClass retType;
  218. try {
  219. params = getParameterTypes();
  220. retType = getReturnType();
  221. }
  222. catch (NotFoundException e) {
  223. throw new CannotCompileException(e);
  224. }
  225. Bytecode code = CtNewWrappedMethod.makeBody(clazz,
  226. clazz.getClassFile2(),
  227. mbody,
  228. params, retType,
  229. constParam);
  230. CodeAttribute cattr = code.toCodeAttribute();
  231. methodInfo.setCodeAttribute(cattr);
  232. methodInfo.setAccessFlags(methodInfo.getAccessFlags()
  233. & ~AccessFlag.ABSTRACT);
  234. }
  235. // inner classes
  236. /**
  237. * Instances of this class represent a constant parameter.
  238. * They are used to specify the parameter given to the methods
  239. * created by <code>CtNewMethod.wrapped()</code>.
  240. *
  241. * @see CtMethod#setWrappedBody(CtMethod,CtMethod.ConstParameter)
  242. * @see CtNewMethod#wrapped(CtClass,String,CtClass[],CtClass[],CtMethod,CtMethod.ConstParameter,CtClass)
  243. * @see CtNewConstructor#make(CtClass[],CtClass[],int,CtMethod,CtMethod.ConstParameter,CtClass)
  244. */
  245. public static class ConstParameter {
  246. /**
  247. * Makes an integer constant.
  248. *
  249. * @param i the constant value.
  250. */
  251. public static ConstParameter integer(int i) {
  252. return new IntConstParameter(i);
  253. }
  254. /**
  255. * Makes a long integer constant.
  256. *
  257. * @param i the constant value.
  258. */
  259. public static ConstParameter integer(long i) {
  260. return new LongConstParameter(i);
  261. }
  262. /**
  263. * Makes an <code>String</code> constant.
  264. *
  265. * @param s the constant value.
  266. */
  267. public static ConstParameter string(String s) {
  268. return new StringConstParameter(s);
  269. }
  270. ConstParameter() {}
  271. /**
  272. * @return the size of the stack consumption.
  273. */
  274. int compile(Bytecode code) throws CannotCompileException {
  275. return 0;
  276. }
  277. String descriptor() {
  278. return defaultDescriptor();
  279. }
  280. /**
  281. * @see CtNewWrappedMethod
  282. */
  283. static String defaultDescriptor() {
  284. return "([Ljava/lang/Object;)Ljava/lang/Object;";
  285. }
  286. /**
  287. * Returns the descriptor for constructors.
  288. *
  289. * @see CtNewWrappedConstructor
  290. */
  291. String constDescriptor() {
  292. return defaultConstDescriptor();
  293. }
  294. /**
  295. * Returns the default descriptor for constructors.
  296. */
  297. static String defaultConstDescriptor() {
  298. return "([Ljava/lang/Object;)V";
  299. }
  300. }
  301. static class IntConstParameter extends ConstParameter {
  302. int param;
  303. IntConstParameter(int i) {
  304. param = i;
  305. }
  306. int compile(Bytecode code) throws CannotCompileException {
  307. code.addIconst(param);
  308. return 1;
  309. }
  310. String descriptor() {
  311. return "([Ljava/lang/Object;I)Ljava/lang/Object;";
  312. }
  313. String constDescriptor() {
  314. return "([Ljava/lang/Object;I)V";
  315. }
  316. }
  317. static class LongConstParameter extends ConstParameter {
  318. long param;
  319. LongConstParameter(long l) {
  320. param = l;
  321. }
  322. int compile(Bytecode code) throws CannotCompileException {
  323. code.addLconst(param);
  324. return 2;
  325. }
  326. String descriptor() {
  327. return "([Ljava/lang/Object;J)Ljava/lang/Object;";
  328. }
  329. String constDescriptor() {
  330. return "([Ljava/lang/Object;J)V";
  331. }
  332. }
  333. static class StringConstParameter extends ConstParameter {
  334. String param;
  335. StringConstParameter(String s) {
  336. param = s;
  337. }
  338. int compile(Bytecode code) throws CannotCompileException {
  339. code.addLdc(param);
  340. return 1;
  341. }
  342. String descriptor() {
  343. return "([Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;";
  344. }
  345. String constDescriptor() {
  346. return "([Ljava/lang/Object;Ljava/lang/String;)V";
  347. }
  348. }
  349. }