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

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