Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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