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. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. */
  15. package javassist.expr;
  16. import javassist.*;
  17. import javassist.bytecode.*;
  18. import javassist.compiler.*;
  19. import javassist.compiler.ast.ASTList;
  20. /**
  21. * Catch clause.
  22. */
  23. public class Handler extends Expr {
  24. private static String EXCEPTION_NAME = "$1";
  25. private ExceptionTable etable;
  26. private int index;
  27. /**
  28. * Undocumented constructor. Do not use; internal-use only.
  29. */
  30. Handler(ExceptionTable et, int nth,
  31. CodeIterator it, CtClass declaring, MethodInfo m) {
  32. super(et.handlerPc(nth), it, declaring, m);
  33. etable = et;
  34. index = nth;
  35. }
  36. /**
  37. * Returns the method or constructor containing the catch clause.
  38. */
  39. public CtBehavior where() { return super.where(); }
  40. /**
  41. * Returns the source line number of the catch clause.
  42. *
  43. * @return -1 if this information is not available.
  44. */
  45. public int getLineNumber() {
  46. return super.getLineNumber();
  47. }
  48. /**
  49. * Returns the source file containing the catch clause.
  50. *
  51. * @return null if this information is not available.
  52. */
  53. public String getFileName() {
  54. return super.getFileName();
  55. }
  56. /**
  57. * Returns the list of exceptions that the catch clause may throw.
  58. */
  59. public CtClass[] mayThrow() {
  60. return super.mayThrow();
  61. }
  62. /**
  63. * Returns the type handled by the catch clause.
  64. */
  65. public CtClass getType() throws NotFoundException {
  66. ConstPool cp = getConstPool();
  67. String name = cp.getClassInfo(etable.catchType(index));
  68. return Descriptor.toCtClass(name, thisClass.getClassPool());
  69. }
  70. /**
  71. * This method has not been implemented yet.
  72. *
  73. * @param statement a Java statement.
  74. */
  75. public void replace(String statement) throws CannotCompileException {
  76. throw new RuntimeException("not implemented yet");
  77. }
  78. /**
  79. * Inserts bytecode at the beginning of the catch clause.
  80. * The caught exception is stored in <code>$1</code>.
  81. *
  82. * @param src the source code representing the inserted bytecode.
  83. * It must be a single statement or block.
  84. */
  85. public void insertBefore(String src) throws CannotCompileException {
  86. edited = true;
  87. ConstPool cp = getConstPool();
  88. CodeAttribute ca = iterator.get();
  89. Javac jv = new Javac(thisClass);
  90. Bytecode b = jv.getBytecode();
  91. b.setStackDepth(1);
  92. b.setMaxLocals(ca.getMaxLocals());
  93. try {
  94. CtClass type = getType();
  95. int var = jv.recordVariable(type, EXCEPTION_NAME);
  96. jv.recordReturnType(type, false);
  97. b.addAstore(var);
  98. jv.compileStmnt(src);
  99. b.addAload(var);
  100. int oldHandler = etable.handlerPc(index);
  101. b.addOpcode(Opcode.GOTO);
  102. b.addIndex(oldHandler - iterator.getCodeLength()
  103. - b.currentPc() + 1);
  104. maxStack = b.getMaxStack();
  105. maxLocals = b.getMaxLocals();
  106. int pos = iterator.append(b.get());
  107. iterator.append(b.getExceptionTable(), pos);
  108. etable.setHandlerPc(index, pos);
  109. }
  110. catch (NotFoundException e) {
  111. throw new CannotCompileException(e);
  112. }
  113. catch (CompileError e) {
  114. throw new CannotCompileException(e);
  115. }
  116. }
  117. }