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.

LineNumberTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package javassist;
  2. import junit.framework.TestCase;
  3. public class LineNumberTest extends TestCase {
  4. private final ClassPool loader = ClassPool.getDefault();
  5. private static int classNumber = 0;
  6. public void testComma() {
  7. doTestCompile(String.join("\n",
  8. "public void run() {",
  9. " return",
  10. "}"), "line 3: syntax error near \" return\n}\"");
  11. }
  12. public void testUndevField() {
  13. doTestCompile(String.join("\n",
  14. "public void run() {",
  15. " foo = 5;",
  16. "}"), "line 2: no such field: foo");
  17. }
  18. public void testUndevMethod() {
  19. doTestCompile(String.join("\n",
  20. "public void run() {",
  21. " foo();",
  22. "}"), "line 2: foo() not found in javassist.LineNumberCompileTest2");
  23. }
  24. public void testException() {
  25. doTestRuntime(String.join("\n",
  26. "public void run() {",
  27. " throw new java.lang.RuntimeException();",
  28. "}"), 0, 5);
  29. }
  30. public void testIf() {
  31. doTestRuntime(String.join("\n",
  32. "public void run() {",
  33. " if (throwException()) {",
  34. " }",
  35. "}"), 1, 5);
  36. }
  37. public void testWhile() {
  38. doTestRuntime(String.join("\n",
  39. "public void run() {",
  40. " while (throwException()) {",
  41. " }",
  42. "}"), 1, 5);
  43. }
  44. public void testFor() {
  45. doTestRuntime(String.join("\n",
  46. "public void run() {",
  47. " for (; throwException(); ) {",
  48. " ",
  49. " }",
  50. "}"), 1, 5);
  51. }
  52. private void doTestCompile(String src, String msg) {
  53. CtClass testClass = loader.makeClass("javassist.LineNumberCompileTest" + classNumber++);
  54. try {
  55. testClass.addMethod(CtMethod.make(src, testClass));
  56. } catch (CannotCompileException e) {
  57. assertEquals(msg, e.getCause().getMessage());
  58. return;
  59. }
  60. fail("should not happen");
  61. }
  62. private void doTestRuntime(String src, int stackOffset, int lineNumber) {
  63. CtClass testClass = loader.makeClass("javassist.LineNumberRuntimeTest" + classNumber++);
  64. String test = String.join("\n",
  65. "private boolean throwException() {",
  66. " throw new java.lang.RuntimeException();",
  67. "}");
  68. try {
  69. testClass.addInterface(loader.get("java.lang.Runnable"));
  70. testClass.addMethod(CtMethod.make(test, testClass));
  71. testClass.addMethod(CtMethod.make(src, testClass));
  72. Class cls = testClass.toClass(LineNumberTest.class);
  73. var runnable = (Runnable) cls.getConstructor().newInstance();
  74. runnable.run();
  75. } catch (Exception e) {
  76. var lineNum = e.getStackTrace()[stackOffset].getLineNumber();
  77. assertEquals("Line number should be right", lineNumber, lineNum);
  78. return;
  79. }
  80. fail("should not happen");
  81. }
  82. }