Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CtBehavior.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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.expr.ExprEditor;
  20. /**
  21. * <code>CtBehavior</code> represents a method, a constructor,
  22. * or a static constructor (class initializer).
  23. * It is the abstract super class of
  24. * <code>CtMethod</code> and <code>CtConstructor</code>.
  25. */
  26. public abstract class CtBehavior extends CtMember {
  27. protected MethodInfo methodInfo;
  28. protected CtBehavior(CtClass clazz, MethodInfo minfo) {
  29. super(clazz);
  30. methodInfo = minfo;
  31. }
  32. protected void extendToString(StringBuffer buffer) {
  33. buffer.append(' ');
  34. buffer.append(getName());
  35. buffer.append(' ');
  36. buffer.append(methodInfo.getDescriptor());
  37. }
  38. /**
  39. * Returns the MethodInfo representing this method/constructor in the
  40. * class file.
  41. */
  42. public MethodInfo getMethodInfo() {
  43. declaringClass.checkModify();
  44. return methodInfo;
  45. }
  46. /**
  47. * Undocumented method. Do not use; internal-use only.
  48. */
  49. public MethodInfo getMethodInfo2() { return methodInfo; }
  50. /**
  51. * Obtains the modifiers of the method/constructor.
  52. *
  53. * @return modifiers encoded with
  54. * <code>javassist.Modifier</code>.
  55. * @see Modifier
  56. */
  57. public int getModifiers() {
  58. return AccessFlag.toModifier(methodInfo.getAccessFlags());
  59. }
  60. /**
  61. * Sets the encoded modifiers of the method/constructor.
  62. *
  63. * <p>Changing the modifiers may cause a problem.
  64. * For example, if a non-static method is changed to static,
  65. * the method will be rejected by the bytecode verifier.
  66. *
  67. * @see Modifier
  68. */
  69. public void setModifiers(int mod) {
  70. declaringClass.checkModify();
  71. methodInfo.setAccessFlags(AccessFlag.of(mod));
  72. }
  73. /**
  74. * Obtains parameter types of this method/constructor.
  75. */
  76. public CtClass[] getParameterTypes() throws NotFoundException {
  77. return Descriptor.getParameterTypes(methodInfo.getDescriptor(),
  78. declaringClass.getClassPool());
  79. }
  80. /**
  81. * Obtains the type of the returned value.
  82. */
  83. CtClass getReturnType0() throws NotFoundException {
  84. return Descriptor.getReturnType(methodInfo.getDescriptor(),
  85. declaringClass.getClassPool());
  86. }
  87. /**
  88. * Returns the character string representing the parameter types
  89. * and the return type. If two methods/constructors have
  90. * the same parameter types
  91. * and the return type, <code>getSignature()</code> returns the
  92. * same string (the return type of constructors is <code>void</code>).
  93. */
  94. public String getSignature() {
  95. return methodInfo.getDescriptor();
  96. }
  97. /**
  98. * Obtains exceptions that this method/constructor may throw.
  99. */
  100. public CtClass[] getExceptionTypes() throws NotFoundException {
  101. String[] exceptions;
  102. ExceptionsAttribute ea = methodInfo.getExceptionsAttribute();
  103. if (ea == null)
  104. exceptions = null;
  105. else
  106. exceptions = ea.getExceptions();
  107. return declaringClass.getClassPool().get(exceptions);
  108. }
  109. /**
  110. * Sets exceptions that this method/constructor may throw.
  111. */
  112. public void setExceptionTypes(CtClass[] types) throws NotFoundException {
  113. declaringClass.checkModify();
  114. if (types == null) {
  115. methodInfo.removeExceptionsAttribute();
  116. return;
  117. }
  118. String[] names = new String[types.length];
  119. for (int i = 0; i < types.length; ++i)
  120. names[i] = types[i].getName();
  121. ExceptionsAttribute ea = methodInfo.getExceptionsAttribute();
  122. if (ea == null) {
  123. ea = new ExceptionsAttribute(methodInfo.getConstPool());
  124. methodInfo.setExceptionsAttribute(ea);
  125. }
  126. ea.setExceptions(names);
  127. }
  128. /**
  129. * Returns true if the body is empty.
  130. */
  131. public abstract boolean isEmpty();
  132. /**
  133. * Sets a method/constructor body.
  134. *
  135. * @param src the source code representing the body.
  136. * It must be a single statement or block.
  137. * If it is <code>null</code>, the substituted
  138. * body does nothing except returning zero or null.
  139. */
  140. public void setBody(String src) throws CannotCompileException {
  141. setBody(src, null, null);
  142. }
  143. /**
  144. * Sets a method/constructor body.
  145. *
  146. * @param src the source code representing the body.
  147. * It must be a single statement or block.
  148. * If it is <code>null</code>, the substituted
  149. * body does nothing except returning zero or null.
  150. * @param delegateObj the source text specifying the object
  151. * that is called on by <code>$proceed()</code>.
  152. * @param delegateMethod the name of the method
  153. * that is called by <code>$proceed()</code>.
  154. */
  155. public void setBody(String src,
  156. String delegateObj, String delegateMethod)
  157. throws CannotCompileException
  158. {
  159. declaringClass.checkModify();
  160. try {
  161. Javac jv = new Javac(declaringClass);
  162. if (delegateMethod != null)
  163. jv.recordProceed(delegateObj, delegateMethod);
  164. Bytecode b = jv.compileBody(this, src);
  165. methodInfo.setCodeAttribute(b.toCodeAttribute());
  166. methodInfo.setAccessFlags(methodInfo.getAccessFlags()
  167. & ~AccessFlag.ABSTRACT);
  168. }
  169. catch (CompileError e) {
  170. throw new CannotCompileException(e);
  171. }
  172. }
  173. static void setBody0(CtClass srcClass, MethodInfo srcInfo,
  174. CtClass destClass, MethodInfo destInfo,
  175. ClassMap map)
  176. throws CannotCompileException
  177. {
  178. destClass.checkModify();
  179. if (map == null)
  180. map = new ClassMap();
  181. map.put(srcClass.getName(), destClass.getName());
  182. try {
  183. CodeAttribute cattr = srcInfo.getCodeAttribute();
  184. if (cattr != null) {
  185. ConstPool cp = destInfo.getConstPool();
  186. CodeAttribute ca = (CodeAttribute)cattr.copy(cp, map);
  187. destInfo.setCodeAttribute(ca);
  188. }
  189. }
  190. catch (CodeAttribute.RuntimeCopyException e) {
  191. /* the exception may be thrown by copy() in CodeAttribute.
  192. */
  193. throw new CannotCompileException(e);
  194. }
  195. destInfo.setAccessFlags(destInfo.getAccessFlags()
  196. & ~AccessFlag.ABSTRACT);
  197. }
  198. /**
  199. * Obtains an attribute with the given name.
  200. * If that attribute is not found in the class file, this
  201. * method returns null.
  202. *
  203. * @param name attribute name
  204. */
  205. public byte[] getAttribute(String name) {
  206. AttributeInfo ai = methodInfo.getAttribute(name);
  207. if (ai == null)
  208. return null;
  209. else
  210. return ai.get();
  211. }
  212. /**
  213. * Adds an attribute. The attribute is saved in the class file.
  214. *
  215. * @param name attribute name
  216. * @param data attribute value
  217. */
  218. public void setAttribute(String name, byte[] data) {
  219. declaringClass.checkModify();
  220. methodInfo.addAttribute(new AttributeInfo(methodInfo.getConstPool(),
  221. name, data));
  222. }
  223. /**
  224. * Declares to use <code>$cflow</code> for this method/constructor.
  225. * If <code>$cflow</code> is used, the class files modified
  226. * with Javassist requires a support class
  227. * <code>javassist.runtime.Cflow</code> at runtime
  228. * (other Javassist classes are not required at runtime).
  229. *
  230. * <p>Every <code>$cflow</code> variable is given a unique name.
  231. * For example, if the given name is <code>"Point.paint"</code>,
  232. * then the variable is indicated by <code>$cflow(Point.paint)</code>.
  233. *
  234. * @param name <code>$cflow</code> name. It can include
  235. * alphabets, numbers, <code>_</code>,
  236. * <code>$</code>, and <code>.</code> (dot).
  237. *
  238. * @see javassist.runtime.Cflow
  239. */
  240. public void useCflow(String name) throws CannotCompileException {
  241. CtClass cc = declaringClass;
  242. cc.checkModify();
  243. ClassPool pool = cc.getClassPool();
  244. String fname;
  245. int i = 0;
  246. while (true) {
  247. fname = "_cflow$" + i++;
  248. try {
  249. cc.getDeclaredField(fname);
  250. }
  251. catch(NotFoundException e) {
  252. break;
  253. }
  254. }
  255. pool.recordCflow(name, declaringClass.getName(), fname);
  256. try {
  257. CtClass type = pool.get("javassist.runtime.Cflow");
  258. CtField field = new CtField(type, fname, cc);
  259. field.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
  260. cc.addField(field, CtField.Initializer.byNew(type));
  261. insertBefore(fname + ".enter();");
  262. String src = fname + ".exit();";
  263. insertAfter(src, true);
  264. }
  265. catch (NotFoundException e) {
  266. throw new CannotCompileException(e);
  267. }
  268. }
  269. /**
  270. * Modifies the method/constructor body.
  271. *
  272. * @param converter specifies how to modify.
  273. */
  274. public void instrument(CodeConverter converter)
  275. throws CannotCompileException
  276. {
  277. declaringClass.checkModify();
  278. ConstPool cp = methodInfo.getConstPool();
  279. converter.doit(getDeclaringClass(), methodInfo, cp);
  280. }
  281. /**
  282. * Modifies the method/constructor body.
  283. *
  284. * @param editor specifies how to modify.
  285. */
  286. public void instrument(ExprEditor editor)
  287. throws CannotCompileException
  288. {
  289. // if the class is not frozen,
  290. // does not trun the modified flag on.
  291. if (declaringClass.isFrozen())
  292. declaringClass.checkModify();
  293. if (editor.doit(declaringClass, methodInfo))
  294. declaringClass.checkModify();
  295. }
  296. /**
  297. * Inserts bytecode at the beginning of the body.
  298. *
  299. * <p>If this object represents a constructor,
  300. * the bytecode is inserted before
  301. * a constructor in the super class or this class is called.
  302. * Therefore, the inserted bytecode is subject to constraints described
  303. * in Section 4.8.2 of The Java Virtual Machine Specification (2nd ed).
  304. * For example, it cannot access instance fields or methods
  305. * although it can access static fields and methods.
  306. * Use <code>insertBeforeBody()</code> in <code>CtConstructor</code>.
  307. *
  308. * @param src the source code representing the inserted bytecode.
  309. * It must be a single statement or block.
  310. * @see CtConstructor#insertBeforeBody(String)
  311. */
  312. public void insertBefore(String src) throws CannotCompileException {
  313. declaringClass.checkModify();
  314. CodeAttribute ca = methodInfo.getCodeAttribute();
  315. if (ca == null)
  316. throw new CannotCompileException("no method body");
  317. CodeIterator iterator = ca.iterator();
  318. Javac jv = new Javac(declaringClass);
  319. try {
  320. int nvars = jv.recordParams(getParameterTypes(),
  321. Modifier.isStatic(getModifiers()));
  322. jv.recordParamNames(ca, nvars);
  323. jv.compileStmnt(src);
  324. Bytecode b = jv.getBytecode();
  325. int stack = b.getMaxStack();
  326. int locals = b.getMaxLocals();
  327. if (stack > ca.getMaxStack())
  328. ca.setMaxStack(stack);
  329. if (locals > ca.getMaxLocals())
  330. ca.setMaxLocals(locals);
  331. int pos = iterator.insertEx(b.get());
  332. iterator.insert(b.getExceptionTable(), pos);
  333. }
  334. catch (NotFoundException e) {
  335. throw new CannotCompileException(e);
  336. }
  337. catch (CompileError e) {
  338. throw new CannotCompileException(e);
  339. }
  340. catch (BadBytecode e) {
  341. throw new CannotCompileException(e);
  342. }
  343. }
  344. /**
  345. * Inserts bytecode at the end of the body.
  346. * The bytecode is inserted just before every return insturction.
  347. * It is not executed when an exception is thrown.
  348. *
  349. * @param src the source code representing the inserted bytecode.
  350. * It must be a single statement or block.
  351. */
  352. public void insertAfter(String src)
  353. throws CannotCompileException
  354. {
  355. insertAfter(src, false);
  356. }
  357. /**
  358. * Inserts bytecode at the end of the body.
  359. * The bytecode is inserted just before every return insturction.
  360. *
  361. * @param src the source code representing the inserted bytecode.
  362. * It must be a single statement or block.
  363. * @param asFinally true if the inserted bytecode is executed
  364. * not only when the control normally returns
  365. * but also when an exception is thrown.
  366. */
  367. public void insertAfter(String src, boolean asFinally)
  368. throws CannotCompileException
  369. {
  370. declaringClass.checkModify();
  371. ConstPool pool = methodInfo.getConstPool();
  372. CodeAttribute ca = methodInfo.getCodeAttribute();
  373. if (ca == null)
  374. throw new CannotCompileException("no method body");
  375. CodeIterator iterator = ca.iterator();
  376. int retAddr = ca.getMaxLocals();
  377. Bytecode b = new Bytecode(pool, 0, retAddr + 1);
  378. b.setStackDepth(ca.getMaxStack() + 1);
  379. Javac jv = new Javac(b, declaringClass);
  380. try {
  381. int nvars = jv.recordParams(getParameterTypes(),
  382. Modifier.isStatic(getModifiers()));
  383. jv.recordParamNames(ca, nvars);
  384. CtClass rtype = getReturnType0();
  385. int varNo = jv.recordReturnType(rtype, true);
  386. int handlerLen = insertAfterHandler(asFinally, b, rtype, varNo);
  387. byte[] save = makeSaveCode(pool, rtype, varNo);
  388. byte[] restore = makeRestoreCode(b, pool, rtype, varNo);
  389. b.addAstore(retAddr);
  390. jv.compileStmnt(src);
  391. b.addRet(retAddr);
  392. ca.setMaxStack(b.getMaxStack());
  393. ca.setMaxLocals(b.getMaxLocals());
  394. int gapPos = iterator.append(b.get());
  395. iterator.append(b.getExceptionTable(), gapPos);
  396. if (asFinally)
  397. ca.getExceptionTable().add(0, gapPos, gapPos, 0);
  398. int gapLen = iterator.getCodeLength() - gapPos - handlerLen;
  399. int subr = iterator.getCodeLength() - gapLen;
  400. while (iterator.hasNext()) {
  401. int pos = iterator.next();
  402. if (pos >= subr)
  403. break;
  404. int c = iterator.byteAt(pos);
  405. if (c == Opcode.ARETURN || c == Opcode.IRETURN
  406. || c == Opcode.FRETURN || c == Opcode.LRETURN
  407. || c == Opcode.DRETURN || c == Opcode.RETURN) {
  408. insertJSR(iterator, subr, pos, save, restore);
  409. subr = iterator.getCodeLength() - gapLen;
  410. }
  411. }
  412. }
  413. catch (NotFoundException e) {
  414. throw new CannotCompileException(e);
  415. }
  416. catch (CompileError e) {
  417. throw new CannotCompileException(e);
  418. }
  419. catch (BadBytecode e) {
  420. throw new CannotCompileException(e);
  421. }
  422. }
  423. private byte[] makeSaveCode(ConstPool cp, CtClass rtype, int varNo) {
  424. Bytecode b = new Bytecode(cp, 0, 0);
  425. if (rtype == CtClass.voidType) {
  426. b.addOpcode(Opcode.ACONST_NULL);
  427. b.addAstore(varNo);
  428. return b.get();
  429. }
  430. else {
  431. b.addStore(varNo, rtype);
  432. return b.get();
  433. }
  434. }
  435. private byte[] makeRestoreCode(Bytecode code, ConstPool cp,
  436. CtClass rtype, int varNo) {
  437. if (rtype == CtClass.voidType) {
  438. if (code.getMaxLocals() < 1)
  439. code.setMaxLocals(1);
  440. return new byte[0];
  441. }
  442. else {
  443. Bytecode b = new Bytecode(cp, 0, 0);
  444. b.addLoad(varNo, rtype);
  445. return b.get();
  446. }
  447. }
  448. private void insertJSR(CodeIterator iterator, int subr, int pos,
  449. byte[] save, byte[] restore)
  450. throws BadBytecode
  451. {
  452. int gapSize = 5 + save.length + restore.length;
  453. boolean wide = subr - pos > Short.MAX_VALUE - gapSize - 4;
  454. gapSize = iterator.insertGap(pos, wide ? gapSize : gapSize - 2);
  455. iterator.write(save, pos);
  456. pos += save.length;
  457. if (wide) {
  458. iterator.writeByte(Opcode.JSR_W, pos);
  459. iterator.write32bit(subr - pos + gapSize, pos + 1);
  460. pos += 5;
  461. }
  462. else {
  463. iterator.writeByte(Opcode.JSR, pos);
  464. iterator.write16bit(subr - pos + gapSize, pos + 1);
  465. pos += 3;
  466. }
  467. iterator.write(restore, pos);
  468. }
  469. private int insertAfterHandler(boolean asFinally, Bytecode b,
  470. CtClass rtype, int returnVarNo)
  471. {
  472. if (!asFinally)
  473. return 0;
  474. int var = b.getMaxLocals();
  475. b.incMaxLocals(1);
  476. int pc = b.currentPc();
  477. b.addAstore(var);
  478. if (rtype.isPrimitive()) {
  479. char c = ((CtPrimitiveType)rtype).getDescriptor();
  480. if (c == 'D') {
  481. b.addDconst(0.0);
  482. b.addDstore(returnVarNo);
  483. }
  484. else if (c == 'F') {
  485. b.addFconst(0);
  486. b.addFstore(returnVarNo);
  487. }
  488. else if (c == 'J') {
  489. b.addLconst(0);
  490. b.addLstore(returnVarNo);
  491. }
  492. else if (c != 'V') { // int, boolean, char, short, ...
  493. b.addIconst(0);
  494. b.addIstore(returnVarNo);
  495. }
  496. }
  497. else {
  498. b.addOpcode(Opcode.ACONST_NULL);
  499. b.addAstore(returnVarNo);
  500. }
  501. b.addOpcode(Opcode.JSR);
  502. int pc2 = b.currentPc();
  503. b.addIndex(0); // correct later
  504. b.addAload(var);
  505. b.addOpcode(Opcode.ATHROW);
  506. int pc3 = b.currentPc();
  507. b.write16bit(pc2, pc3 - pc2 + 1);
  508. return pc3 - pc;
  509. }
  510. /* -- OLD version --
  511. public void insertAfter(String src) throws CannotCompileException {
  512. declaringClass.checkModify();
  513. CodeAttribute ca = methodInfo.getCodeAttribute();
  514. CodeIterator iterator = ca.iterator();
  515. Bytecode b = new Bytecode(methodInfo.getConstPool(),
  516. ca.getMaxStack(), ca.getMaxLocals());
  517. b.setStackDepth(ca.getMaxStack());
  518. Javac jv = new Javac(b, declaringClass);
  519. try {
  520. jv.recordParams(getParameterTypes(),
  521. Modifier.isStatic(getModifiers()));
  522. CtClass rtype = getReturnType0();
  523. int varNo = jv.recordReturnType(rtype, true);
  524. boolean isVoid = rtype == CtClass.voidType;
  525. if (isVoid) {
  526. b.addOpcode(Opcode.ACONST_NULL);
  527. b.addAstore(varNo);
  528. jv.compileStmnt(src);
  529. }
  530. else {
  531. b.addStore(varNo, rtype);
  532. jv.compileStmnt(src);
  533. b.addLoad(varNo, rtype);
  534. }
  535. byte[] code = b.get();
  536. ca.setMaxStack(b.getMaxStack());
  537. ca.setMaxLocals(b.getMaxLocals());
  538. while (iterator.hasNext()) {
  539. int pos = iterator.next();
  540. int c = iterator.byteAt(pos);
  541. if (c == Opcode.ARETURN || c == Opcode.IRETURN
  542. || c == Opcode.FRETURN || c == Opcode.LRETURN
  543. || c == Opcode.DRETURN || c == Opcode.RETURN)
  544. iterator.insert(pos, code);
  545. }
  546. }
  547. catch (NotFoundException e) {
  548. throw new CannotCompileException(e);
  549. }
  550. catch (CompileError e) {
  551. throw new CannotCompileException(e);
  552. }
  553. catch (BadBytecode e) {
  554. throw new CannotCompileException(e);
  555. }
  556. }
  557. */
  558. /**
  559. * Adds a catch clause that handles an exception thrown in the
  560. * body. The catch clause must end with a return or throw statement.
  561. *
  562. * @param src the source code representing the catch clause.
  563. * It must be a single statement or block.
  564. * @param exceptionType the type of the exception handled by the
  565. * catch clause.
  566. */
  567. public void addCatch(String src, CtClass exceptionType)
  568. throws CannotCompileException
  569. {
  570. addCatch(src, exceptionType, "$e");
  571. }
  572. /**
  573. * Adds a catch clause that handles an exception thrown in the
  574. * body. The catch clause must end with a return or throw statement.
  575. *
  576. * @param src the source code representing the catch clause.
  577. * It must be a single statement or block.
  578. * @param exceptionType the type of the exception handled by the
  579. * catch clause.
  580. * @param exceptionName the name of the variable containing the
  581. * caught exception, for example,
  582. * <code>$e</code>.
  583. */
  584. public void addCatch(String src, CtClass exceptionType,
  585. String exceptionName)
  586. throws CannotCompileException
  587. {
  588. declaringClass.checkModify();
  589. ConstPool cp = methodInfo.getConstPool();
  590. CodeAttribute ca = methodInfo.getCodeAttribute();
  591. CodeIterator iterator = ca.iterator();
  592. Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
  593. b.setStackDepth(1);
  594. Javac jv = new Javac(b, declaringClass);
  595. try {
  596. jv.recordParams(getParameterTypes(),
  597. Modifier.isStatic(getModifiers()));
  598. int var = jv.recordVariable(exceptionType, exceptionName);
  599. b.addAstore(var);
  600. jv.compileStmnt(src);
  601. int stack = b.getMaxStack();
  602. int locals = b.getMaxLocals();
  603. if (stack > ca.getMaxStack())
  604. ca.setMaxStack(stack);
  605. if (locals > ca.getMaxLocals())
  606. ca.setMaxLocals(locals);
  607. int len = iterator.getCodeLength();
  608. int pos = iterator.append(b.get());
  609. ca.getExceptionTable().add(0, len, len,
  610. cp.addClassInfo(exceptionType));
  611. iterator.append(b.getExceptionTable(), pos);
  612. }
  613. catch (NotFoundException e) {
  614. throw new CannotCompileException(e);
  615. }
  616. catch (CompileError e) {
  617. throw new CannotCompileException(e);
  618. }
  619. }
  620. /**
  621. * Inserts bytecode at the specified line in the body.
  622. * It is equivalent to:
  623. *
  624. * <br><code>insertAt(lineNum, true, src)</code>
  625. *
  626. * <br>See this method as well.
  627. *
  628. * @param lineNum the line number. The bytecode is inserted at the
  629. * beginning of the code at the line specified by this
  630. * line number.
  631. * @param src the source code representing the inserted bytecode.
  632. * It must be a single statement or block.
  633. * @return the line number at which the bytecode has been inserted.
  634. *
  635. * @see CtBehavior#insertAt(int,boolean,String)
  636. */
  637. public int insertAt(int lineNum, String src)
  638. throws CannotCompileException
  639. {
  640. return insertAt(lineNum, true, src);
  641. }
  642. /**
  643. * Inserts bytecode at the specified line in the body.
  644. *
  645. * <p>If there is not
  646. * a statement at the specified line, the bytecode might be inserted
  647. * at the line including the first statement after that line specified.
  648. * For example, if there is only a closing brace at that line, the
  649. * bytecode would be inserted at another line below.
  650. * To know exactly where the bytecode will be inserted, call with
  651. * <code>modify</code> set to <code>false</code>.
  652. *
  653. * @param lineNum the line number. The bytecode is inserted at the
  654. * beginning of the code at the line specified by this
  655. * line number.
  656. * @param modify if false, this method does not insert the bytecode.
  657. * It instead only returns the line number at which
  658. * the bytecode would be inserted.
  659. * @param src the source code representing the inserted bytecode.
  660. * It must be a single statement or block.
  661. * If modify is false, the value of src can be null.
  662. * @return the line number at which the bytecode has been inserted.
  663. */
  664. public int insertAt(int lineNum, boolean modify, String src)
  665. throws CannotCompileException
  666. {
  667. CodeAttribute ca = methodInfo.getCodeAttribute();
  668. if (ca == null)
  669. throw new CannotCompileException("no method body");
  670. LineNumberAttribute ainfo
  671. = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
  672. if (ainfo == null)
  673. throw new CannotCompileException("no line number info");
  674. LineNumberAttribute.Pc pc = ainfo.toNearPc(lineNum);
  675. lineNum = pc.line;
  676. int index = pc.index;
  677. if (!modify)
  678. return lineNum;
  679. declaringClass.checkModify();
  680. CodeIterator iterator = ca.iterator();
  681. Javac jv = new Javac(declaringClass);
  682. try {
  683. jv.recordLocalVariables(ca, index);
  684. jv.recordParams(getParameterTypes(),
  685. Modifier.isStatic(getModifiers()));
  686. jv.compileStmnt(src);
  687. Bytecode b = jv.getBytecode();
  688. int stack = b.getMaxStack();
  689. int locals = b.getMaxLocals();
  690. /* We assume that there is no values in the operand stack
  691. * at the position where the bytecode is inserted.
  692. */
  693. if (stack > ca.getMaxStack())
  694. ca.setMaxStack(stack);
  695. if (locals > ca.getMaxLocals())
  696. ca.setMaxLocals(locals);
  697. iterator.insert(index, b.get());
  698. iterator.insert(b.getExceptionTable(), index);
  699. return lineNum;
  700. }
  701. catch (NotFoundException e) {
  702. throw new CannotCompileException(e);
  703. }
  704. catch (CompileError e) {
  705. throw new CannotCompileException(e);
  706. }
  707. catch (BadBytecode e) {
  708. throw new CannotCompileException(e);
  709. }
  710. }
  711. }