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

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