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.

Handler.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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.expr;
  16. import javassist.*;
  17. import javassist.bytecode.*;
  18. import javassist.compiler.*;
  19. /**
  20. * Catch clause.
  21. */
  22. public class Handler extends Expr {
  23. private static String EXCEPTION_NAME = "$1";
  24. private ExceptionTable etable;
  25. private int index;
  26. /**
  27. * Undocumented constructor. Do not use; internal-use only.
  28. */
  29. Handler(ExceptionTable et, int nth,
  30. CodeIterator it, CtClass declaring, MethodInfo m) {
  31. super(et.handlerPc(nth), it, declaring, m);
  32. etable = et;
  33. index = nth;
  34. }
  35. /**
  36. * Returns the method or constructor containing the catch clause.
  37. */
  38. public CtBehavior where() { return super.where(); }
  39. /**
  40. * Returns the source line number of the catch clause.
  41. *
  42. * @return -1 if this information is not available.
  43. */
  44. public int getLineNumber() {
  45. return super.getLineNumber();
  46. }
  47. /**
  48. * Returns the source file containing the catch clause.
  49. *
  50. * @return null if this information is not available.
  51. */
  52. public String getFileName() {
  53. return super.getFileName();
  54. }
  55. /**
  56. * Returns the list of exceptions that the catch clause may throw.
  57. */
  58. public CtClass[] mayThrow() {
  59. return super.mayThrow();
  60. }
  61. /**
  62. * Returns the type handled by the catch clause.
  63. */
  64. public CtClass getType() throws NotFoundException {
  65. ConstPool cp = getConstPool();
  66. String name = cp.getClassInfo(etable.catchType(index));
  67. return Descriptor.toCtClass(name, thisClass.getClassPool());
  68. }
  69. /**
  70. * This method has not been implemented yet.
  71. *
  72. * @param statement a Java statement.
  73. */
  74. public void replace(String statement) throws CannotCompileException {
  75. throw new RuntimeException("not implemented yet");
  76. }
  77. /**
  78. * Inserts bytecode at the beginning of the catch clause.
  79. * The caught exception is stored in <code>$1</code>.
  80. *
  81. * @param src the source code representing the inserted bytecode.
  82. * It must be a single statement or block.
  83. */
  84. public void insertBefore(String src) throws CannotCompileException {
  85. edited = true;
  86. ConstPool cp = getConstPool();
  87. CodeAttribute ca = iterator.get();
  88. Javac jv = new Javac(thisClass);
  89. Bytecode b = jv.getBytecode();
  90. b.setStackDepth(1);
  91. b.setMaxLocals(ca.getMaxLocals());
  92. try {
  93. CtClass type = getType();
  94. int var = jv.recordVariable(type, EXCEPTION_NAME);
  95. jv.recordReturnType(type, false);
  96. b.addAstore(var);
  97. jv.compileStmnt(src);
  98. b.addAload(var);
  99. int oldHandler = etable.handlerPc(index);
  100. b.addOpcode(Opcode.GOTO);
  101. b.addIndex(oldHandler - iterator.getCodeLength()
  102. - b.currentPc() + 1);
  103. maxStack = b.getMaxStack();
  104. maxLocals = b.getMaxLocals();
  105. int pos = iterator.append(b.get());
  106. iterator.append(b.getExceptionTable(), pos);
  107. etable.setHandlerPc(index, pos);
  108. }
  109. catch (NotFoundException e) {
  110. throw new CannotCompileException(e);
  111. }
  112. catch (CompileError e) {
  113. throw new CannotCompileException(e);
  114. }
  115. }
  116. }