diff options
author | akuznetsov <akuznetsov@tradingview.com> | 2023-11-02 15:59:48 +0400 |
---|---|---|
committer | akuznetsov <akuznetsov@tradingview.com> | 2023-11-02 15:59:48 +0400 |
commit | 0824a7a04e2f5ca8acc355c2379cf625dadc8943 (patch) | |
tree | ba7325aadbc4262698016381a0e1799ce3db2d8b /src/test | |
parent | 15d56cfb702000400ff7d888e6084da72530221e (diff) | |
download | javassist-0824a7a04e2f5ca8acc355c2379cf625dadc8943.tar.gz javassist-0824a7a04e2f5ca8acc355c2379cf625dadc8943.zip |
fix line numbers in blocks & refactor test
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/javassist/LineNumberTest.java | 79 | ||||
-rw-r--r-- | src/test/javassist/bytecode/BytecodeTest.java | 60 |
2 files changed, 79 insertions, 60 deletions
diff --git a/src/test/javassist/LineNumberTest.java b/src/test/javassist/LineNumberTest.java new file mode 100644 index 00000000..158efd01 --- /dev/null +++ b/src/test/javassist/LineNumberTest.java @@ -0,0 +1,79 @@ +package javassist; + +import junit.framework.TestCase; + +public class LineNumberTest extends TestCase { + private final ClassPool loader = ClassPool.getDefault(); + private static int classNumber = 0; + + public void testComma() { + doTestCompile(String.join("\n", + "public void run() {", + " return", + "}"), "line 3: syntax error near \" return\n}\""); + } + + public void testException() { + doTestRuntime(String.join("\n", + "public void run() {", + " throw new java.lang.RuntimeException();", + "}"), 0, 5); + } + + public void testIf() { + doTestRuntime(String.join("\n", + "public void run() {", + " if (throwException()) {", + " }", + "}"), 1, 5); + } + + public void testWhile() { + doTestRuntime(String.join("\n", + "public void run() {", + " while (throwException()) {", + " }", + "}"), 1, 5); + } + + public void testFor() { + doTestRuntime(String.join("\n", + "public void run() {", + " for (; throwException(); ) {", + " ", + " }", + "}"), 1, 5); + } + + private void doTestCompile(String src, String msg) { + CtClass testClass = loader.makeClass("javassist.LineNumberCompileTest" + classNumber++); + try { + testClass.addMethod(CtMethod.make(src, testClass)); + } catch (CannotCompileException e) { + assertEquals(msg, e.getCause().getMessage()); + return; + } + fail("should not happen"); + } + + private void doTestRuntime(String src, int stackOffset, int lineNumber) { + CtClass testClass = loader.makeClass("javassist.LineNumberRuntimeTest" + classNumber++); + String test = String.join("\n", + "private boolean throwException() {", + " throw new java.lang.RuntimeException();", + "}"); + try { + testClass.addInterface(loader.get("java.lang.Runnable")); + testClass.addMethod(CtMethod.make(test, testClass)); + testClass.addMethod(CtMethod.make(src, testClass)); + Class cls = testClass.toClass(LineNumberTest.class); + var runnable = (Runnable) cls.getConstructor().newInstance(); + runnable.run(); + } catch (Exception e) { + var lineNum = e.getStackTrace()[stackOffset].getLineNumber(); + assertEquals("Line number should be right", lineNumber, lineNum); + return; + } + fail("should not happen"); + } +} diff --git a/src/test/javassist/bytecode/BytecodeTest.java b/src/test/javassist/bytecode/BytecodeTest.java index cc02801d..eac420bc 100644 --- a/src/test/javassist/bytecode/BytecodeTest.java +++ b/src/test/javassist/bytecode/BytecodeTest.java @@ -302,66 +302,6 @@ public class BytecodeTest extends TestCase { assertEquals(20, pc.line); } - public void testLineNumberCompiler() { - CtClass testClass = loader.makeClass("javassist.bytecode.LineNumberCompilerClass"); - String run = String.join("\n", - "public void run() {", - " return", - "}"); - try { - testClass.addMethod(CtMethod.make(run, testClass)); - } catch (CannotCompileException e) { - assertEquals("line 3: syntax error near \" return\n}\"", e.getCause().getMessage()); - return; - } - fail("should not happen"); - } - - public void testLineNumberCompiler2() { - CtClass testClass = loader.makeClass("javassist.bytecode.LineNumberCompilerClass2"); - String ctor = String.join("\n", - "public LineNumberCompilerClass2() {", - " super();", - "}"); - String run = String.join("\n", - "public void run() {", - " return;", - "}"); - String run2 = String.join("\n", - "public void run2() {", - " run()", - "}"); - try { - testClass.addConstructor(CtNewConstructor.make(ctor, testClass)); - testClass.addMethod(CtMethod.make(run, testClass)); - testClass.addMethod(CtMethod.make(run2, testClass)); - } catch (CannotCompileException e) { - assertEquals("line 9: ; is missing", e.getCause().getMessage()); - return; - } - fail("should not happen"); - } - - public void testLineNumberException() { - CtClass testClass = loader.makeClass("javassist.bytecode.LineNumberExceptionClass"); - String run = String.join("\n", - "public void run() {", - " throw new java.lang.RuntimeException();", - "}"); - try { - testClass.addInterface(loader.get("java.lang.Runnable")); - testClass.addMethod(CtMethod.make(run, testClass)); - Class cls = testClass.toClass(BytecodeTest.class); - var runnable = (Runnable) cls.getConstructor().newInstance(); - runnable.run(); - } catch (Exception e) { - var lineNum = e.getStackTrace()[0].getLineNumber(); - assertEquals("Line number should be right", 2, lineNum); - return; - } - fail("should not happen"); - } - public void testRenameClass() throws Exception { CtClass cc = loader.get("test1.RenameClass"); cc.replaceClassName("test1.RenameClass2", "java.lang.String"); |