Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CtMethod.java 13KB

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