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.

CtNewMethod.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. import javassist.compiler.Javac;
  18. import javassist.compiler.CompileError;
  19. import javassist.CtMethod.ConstParameter;
  20. /**
  21. * A collection of static methods for creating a <code>CtMethod</code>.
  22. * An instance of this class does not make any sense.
  23. *
  24. * @see CtClass#addMethod(CtMethod)
  25. */
  26. public class CtNewMethod {
  27. /**
  28. * Compiles the given source code and creates a method.
  29. * The source code must include not only the method body
  30. * but the whole declaration, for example,
  31. *
  32. * <ul><pre>"public Object id(Object obj) { return obj; }"</pre></ul>
  33. *
  34. * @param src the source text.
  35. * @param declaring the class to which the created method is added.
  36. */
  37. public static CtMethod make(String src, CtClass declaring)
  38. throws CannotCompileException
  39. {
  40. return make(src, declaring, null, null);
  41. }
  42. /**
  43. * Compiles the given source code and creates a method.
  44. * The source code must include not only the method body
  45. * but the whole declaration, for example,
  46. *
  47. * <ul><pre>"public Object id(Object obj) { return obj; }"</pre></ul>
  48. *
  49. * <p>If the source code includes <code>$proceed()</code>, then
  50. * it is compiled into a method call on the specified object.
  51. *
  52. * @param src the source text.
  53. * @param declaring the class to which the created method is added.
  54. * @param delegateObj the source text specifying the object
  55. * that is called on by <code>$proceed()</code>.
  56. * @param delegateMethod the name of the method
  57. * that is called by <code>$proceed()</code>.
  58. */
  59. public static CtMethod make(String src, CtClass declaring,
  60. String delegateObj, String delegateMethod)
  61. throws CannotCompileException
  62. {
  63. Javac compiler = new Javac(declaring);
  64. try {
  65. if (delegateMethod != null)
  66. compiler.recordProceed(delegateObj, delegateMethod);
  67. CtMember obj = compiler.compile(src);
  68. if (obj instanceof CtMethod)
  69. return (CtMethod)obj;
  70. }
  71. catch (CompileError e) {
  72. throw new CannotCompileException(e);
  73. }
  74. throw new CannotCompileException("not a method");
  75. }
  76. /**
  77. * Creates a public (non-static) method. The created method cannot
  78. * be changed to a static method later.
  79. *
  80. * @param returnType the type of the returned value.
  81. * @param mname the method name.
  82. * @param parameters a list of the parameter types.
  83. * @param exceptions a list of the exception types.
  84. * @param body the source text of the method body.
  85. * It must be a block surrounded by <code>{}</code>.
  86. * If it is <code>null</code>, the created method
  87. * does nothing except returning zero or null.
  88. * @param declaring the class to which the created method is added.
  89. */
  90. public static CtMethod make(CtClass returnType,
  91. String mname, CtClass[] parameters,
  92. CtClass[] exceptions,
  93. String body, CtClass declaring)
  94. throws CannotCompileException
  95. {
  96. return make(Modifier.PUBLIC, returnType, mname, parameters, exceptions,
  97. body, declaring);
  98. }
  99. /**
  100. * Creates a method.
  101. *
  102. * @param modifiers access modifiers.
  103. * @param returnType the type of the returned value.
  104. * @param mname the method name.
  105. * @param parameters a list of the parameter types.
  106. * @param exceptions a list of the exception types.
  107. * @param body the source text of the method body.
  108. * It must be a block surrounded by <code>{}</code>.
  109. * If it is <code>null</code>, the created method
  110. * does nothing except returning zero or null.
  111. * @param declaring the class to which the created method is added.
  112. *
  113. * @see Modifier
  114. */
  115. public static CtMethod make(int modifiers, CtClass returnType,
  116. String mname, CtClass[] parameters,
  117. CtClass[] exceptions,
  118. String body, CtClass declaring)
  119. throws CannotCompileException
  120. {
  121. try {
  122. CtMethod cm
  123. = new CtMethod(returnType, mname, parameters, declaring);
  124. cm.setModifiers(modifiers);
  125. cm.setExceptionTypes(exceptions);
  126. cm.setBody(body);
  127. return cm;
  128. }
  129. catch (NotFoundException e) {
  130. throw new CannotCompileException(e);
  131. }
  132. }
  133. /**
  134. * Creates a copy of a method. This method is provided for creating
  135. * a new method based on an existing method.
  136. *
  137. * @param src the source method.
  138. * @param declaring the class to which the created method is added.
  139. * @param map the hashtable associating original class names
  140. * with substituted names.
  141. * It can be <code>null</code>.
  142. *
  143. * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap)
  144. */
  145. public static CtMethod copy(CtMethod src, CtClass declaring,
  146. ClassMap map) throws CannotCompileException {
  147. return new CtMethod(src, declaring, map);
  148. }
  149. /**
  150. * Creates a copy of a method with a new name.
  151. * This method is provided for creating
  152. * a new method based on an existing method.
  153. *
  154. * @param src the source method.
  155. * @param name the name of the created method.
  156. * @param declaring the class to which the created method is added.
  157. * @param map the hashtable associating original class names
  158. * with substituted names.
  159. * It can be <code>null</code>.
  160. *
  161. * @see CtMethod#CtMethod(CtMethod,CtClass,ClassMap)
  162. */
  163. public static CtMethod copy(CtMethod src, String name, CtClass declaring,
  164. ClassMap map) throws CannotCompileException {
  165. CtMethod cm = new CtMethod(src, declaring, map);
  166. cm.setName(name);
  167. return cm;
  168. }
  169. /**
  170. * Creates a public abstract method.
  171. *
  172. * @param returnType the type of the returned value
  173. * @param mname the method name
  174. * @param parameters a list of the parameter types
  175. * @param exceptions a list of the exception types
  176. * @param declaring the class to which the created method is added.
  177. *
  178. * @see CtMethod#CtMethod(CtClass,String,CtClass[],CtClass)
  179. */
  180. public static CtMethod abstractMethod(CtClass returnType,
  181. String mname,
  182. CtClass[] parameters,
  183. CtClass[] exceptions,
  184. CtClass declaring)
  185. throws NotFoundException
  186. {
  187. CtMethod cm = new CtMethod(returnType, mname, parameters, declaring);
  188. cm.setExceptionTypes(exceptions);
  189. return cm;
  190. }
  191. /**
  192. * Creates a public getter method. The getter method returns the value
  193. * of the specified field in the class to which this method is added.
  194. * The created method is initially not static even if the field is
  195. * static. Change the modifiers if the method should be static.
  196. *
  197. * @param methodName the name of the getter
  198. * @param field the field accessed.
  199. */
  200. public static CtMethod getter(String methodName, CtField field)
  201. throws CannotCompileException
  202. {
  203. FieldInfo finfo = field.getFieldInfo2();
  204. String fieldType = finfo.getDescriptor();
  205. String desc = "()" + fieldType;
  206. ConstPool cp = finfo.getConstPool();
  207. MethodInfo minfo = new MethodInfo(cp, methodName, desc);
  208. minfo.setAccessFlags(AccessFlag.PUBLIC);
  209. Bytecode code = new Bytecode(cp, 2, 1);
  210. try {
  211. String fieldName = finfo.getName();
  212. if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
  213. code.addAload(0);
  214. code.addGetfield(Bytecode.THIS, fieldName, fieldType);
  215. }
  216. else
  217. code.addGetstatic(Bytecode.THIS, fieldName, fieldType);
  218. code.addReturn(field.getType());
  219. }
  220. catch (NotFoundException e) {
  221. throw new CannotCompileException(e);
  222. }
  223. minfo.setCodeAttribute(code.toCodeAttribute());
  224. return new CtMethod(minfo, field.getDeclaringClass());
  225. }
  226. /**
  227. * Creates a public setter method. The setter method assigns the
  228. * value of the first parameter to the specified field
  229. * in the class to which this method is added.
  230. * The created method is not static even if the field is
  231. * static. You may not change it to be static
  232. * by <code>setModifiers()</code> in <code>CtBehavior</code>.
  233. *
  234. * @param methodName the name of the setter
  235. * @param field the field accessed.
  236. */
  237. public static CtMethod setter(String methodName, CtField field)
  238. throws CannotCompileException
  239. {
  240. FieldInfo finfo = field.getFieldInfo2();
  241. String fieldType = finfo.getDescriptor();
  242. String desc = "(" + fieldType + ")V";
  243. ConstPool cp = finfo.getConstPool();
  244. MethodInfo minfo = new MethodInfo(cp, methodName, desc);
  245. minfo.setAccessFlags(AccessFlag.PUBLIC);
  246. Bytecode code = new Bytecode(cp, 3, 3);
  247. try {
  248. String fieldName = finfo.getName();
  249. if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
  250. code.addAload(0);
  251. code.addLoad(1, field.getType());
  252. code.addPutfield(Bytecode.THIS, fieldName, fieldType);
  253. }
  254. else {
  255. code.addLoad(1, field.getType());
  256. code.addPutstatic(Bytecode.THIS, fieldName, fieldType);
  257. }
  258. code.addReturn(null);
  259. }
  260. catch (NotFoundException e) {
  261. throw new CannotCompileException(e);
  262. }
  263. minfo.setCodeAttribute(code.toCodeAttribute());
  264. return new CtMethod(minfo, field.getDeclaringClass());
  265. }
  266. /**
  267. * Creates a method forwarding to a delegate in
  268. * a super class. The created method calls a method specified
  269. * by <code>delegate</code> with all the parameters passed to the
  270. * created method. If the delegate method returns a value,
  271. * the created method returns that value to the caller.
  272. * The delegate method must be declared in a super class.
  273. *
  274. * <p>The following method is an example of the created method.
  275. *
  276. * <ul><pre>int f(int p, int q) {
  277. * return super.f(p, q);
  278. * }</pre></ul>
  279. *
  280. * <p>The name of the created method can be changed by
  281. * <code>setName()</code>.
  282. *
  283. * @param delegate the method that the created method forwards to.
  284. * @param declaring the class to which the created method is
  285. * added.
  286. */
  287. public static CtMethod delegator(CtMethod delegate, CtClass declaring)
  288. throws CannotCompileException
  289. {
  290. try {
  291. return delegator0(delegate, declaring);
  292. }
  293. catch (NotFoundException e) {
  294. throw new CannotCompileException(e);
  295. }
  296. }
  297. private static CtMethod delegator0(CtMethod delegate, CtClass declaring)
  298. throws CannotCompileException, NotFoundException
  299. {
  300. MethodInfo deleInfo = delegate.getMethodInfo2();
  301. String methodName = deleInfo.getName();
  302. String desc = deleInfo.getDescriptor();
  303. ConstPool cp = declaring.getClassFile2().getConstPool();
  304. MethodInfo minfo = new MethodInfo(cp, methodName, desc);
  305. minfo.setAccessFlags(deleInfo.getAccessFlags());
  306. ExceptionsAttribute eattr = deleInfo.getExceptionsAttribute();
  307. if (eattr != null)
  308. minfo.setExceptionsAttribute(
  309. (ExceptionsAttribute)eattr.copy(cp, null));
  310. Bytecode code = new Bytecode(cp, 0, 0);
  311. boolean isStatic = Modifier.isStatic(delegate.getModifiers());
  312. CtClass deleClass = delegate.getDeclaringClass();
  313. CtClass[] params = delegate.getParameterTypes();
  314. int s;
  315. if (isStatic) {
  316. s = code.addLoadParameters(params, 0);
  317. code.addInvokestatic(deleClass, methodName, desc);
  318. }
  319. else {
  320. code.addLoad(0, deleClass);
  321. s = code.addLoadParameters(params, 1);
  322. code.addInvokespecial(deleClass, methodName, desc);
  323. }
  324. code.addReturn(delegate.getReturnType());
  325. code.setMaxLocals(++s);
  326. code.setMaxStack(s < 2 ? 2 : s); // for a 2-word return value
  327. minfo.setCodeAttribute(code.toCodeAttribute());
  328. return new CtMethod(minfo, declaring);
  329. }
  330. /**
  331. * Creates a wrapped method. The wrapped method receives parameters
  332. * in the form of an array of <code>Object</code>.
  333. *
  334. * <p>The body of the created method is a copy of the body of a method
  335. * specified by <code>body</code>. However, it is wrapped in
  336. * parameter-conversion code.
  337. *
  338. * <p>The method specified by <code>body</code> must have this singature:
  339. *
  340. * <ul><code>Object method(Object[] params, &lt;type&gt; cvalue)
  341. * </code></ul>
  342. *
  343. * <p>The type of the <code>cvalue</code> depends on
  344. * <code>constParam</code>.
  345. * If <code>constParam</code> is <code>null</code>, the signature
  346. * must be:
  347. *
  348. * <ul><code>Object method(Object[] params)</code></ul>
  349. *
  350. * <p>The method body copied from <code>body</code> is wrapped in
  351. * parameter-conversion code, which converts parameters specified by
  352. * <code>parameterTypes</code> into an array of <code>Object</code>.
  353. * The returned value is also converted from the <code>Object</code>
  354. * type to the type specified by <code>returnType</code>. Thus,
  355. * the resulting method body is as follows:
  356. *
  357. * <ul><pre>Object[] params = new Object[] { p0, p1, ... };
  358. * &lt;<i>type</i>&gt; cvalue = &lt;<i>constant-value</i>&gt;;
  359. * <i>... copied method body ...</i>
  360. * Object result = &lt;<i>returned value</i>&gt;
  361. * return (<i>&lt;returnType&gt;</i>)result;
  362. * </pre></ul>
  363. *
  364. * <p>The variables <code>p0</code>, <code>p2</code>, ... represent
  365. * formal parameters of the created method.
  366. * The value of <code>cvalue</code> is specified by
  367. * <code>constParam</code>.
  368. *
  369. * <p>If the type of a parameter or a returned value is a primitive
  370. * type, then the value is converted into a wrapper object such as
  371. * <code>java.lang.Integer</code>. If the type of the returned value
  372. * is <code>void</code>, the returned value is discarded.
  373. *
  374. * <p><i>Example:</i>
  375. *
  376. * <ul><pre>ClassPool pool = ... ;
  377. * CtClass vec = pool.makeClass("intVector");
  378. * vec.setSuperclass(pool.get("java.util.Vector"));
  379. * CtMethod addMethod = pool.getMethod("Sample", "add0");
  380. *
  381. * CtClass[] argTypes = { CtClass.intType };
  382. * CtMethod m = CtNewMethod.wrapped(CtClass.voidType, "add", argTypes,
  383. * null, addMethod, null, vec);
  384. * vec.addMethod(m);</pre></ul>
  385. *
  386. * <p>where the class <code>Sample</code> is as follows:
  387. *
  388. * <ul><pre>public class Sample extends java.util.Vector {
  389. * public Object add0(Object[] args) {
  390. * super.addElement(args[0]);
  391. * return null;
  392. * }
  393. * }</pre></ul>
  394. *
  395. * <p>This program produces a class <code>intVector</code>:
  396. *
  397. * <ul><pre>public class intVector extends java.util.Vector {
  398. * public void add(int p0) {
  399. * Object[] args = new Object[] { p0 };
  400. * // begin of copied body
  401. * super.addElement(args[0]);
  402. * Object result = null;
  403. * // end
  404. * }
  405. * }</pre></ul>
  406. *
  407. * <p>Note that the type of the parameter to <code>add()</code> depends
  408. * only on the value of <code>argTypes</code> passed to
  409. * <code>CtNewMethod.wrapped()</code>. Thus, it is easy to
  410. * modify this program to produce a
  411. * <code>StringVector</code> class, which is a vector containing
  412. * only <code>String</code> objects, and other vector classes.
  413. *
  414. * @param returnType the type of the returned value.
  415. * @param mname the method name.
  416. * @param parameterTypes a list of the parameter types.
  417. * @param exceptionTypes a list of the exception types.
  418. * @param body the method body
  419. * (must not be a static method).
  420. * @param constParam the constant parameter
  421. * (maybe <code>null</code>).
  422. * @param declaring the class to which the created method is
  423. * added.
  424. */
  425. public static CtMethod wrapped(CtClass returnType,
  426. String mname,
  427. CtClass[] parameterTypes,
  428. CtClass[] exceptionTypes,
  429. CtMethod body, ConstParameter constParam,
  430. CtClass declaring)
  431. throws CannotCompileException
  432. {
  433. return CtNewWrappedMethod.wrapped(returnType, mname, parameterTypes,
  434. exceptionTypes, body, constParam, declaring);
  435. }
  436. }