From 15d56cfb702000400ff7d888e6084da72530221e Mon Sep 17 00:00:00 2001 From: akuznetsov Date: Wed, 4 Oct 2023 14:22:28 +0400 Subject: [PATCH] save line number count though all methods/ctors --- src/main/javassist/CtClass.java | 10 ++++++++ src/main/javassist/CtNewConstructor.java | 1 + src/main/javassist/CtNewMethod.java | 1 + src/main/javassist/compiler/Javac.java | 3 ++- src/main/javassist/compiler/Lex.java | 6 ++++- src/test/javassist/bytecode/BytecodeTest.java | 25 +++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 8880b1f6..24f4e25f 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -71,6 +71,8 @@ public abstract class CtClass { */ public static final String version = "3.29.2-GA"; + private int linesCount = 0; + /** * Prints the version number and the copyright notice. * @@ -1575,6 +1577,14 @@ public abstract class CtClass { new DelayedFileOutputStream(filename))); } + public int getLinesCount() { + return linesCount; + } + + void addLines(int count) { + this.linesCount += count; + } + /** * Writes a class file as writeFile() does although this * method does not prune or freeze the class after writing the class diff --git a/src/main/javassist/CtNewConstructor.java b/src/main/javassist/CtNewConstructor.java index 9f4225f1..ed3e379b 100644 --- a/src/main/javassist/CtNewConstructor.java +++ b/src/main/javassist/CtNewConstructor.java @@ -69,6 +69,7 @@ public class CtNewConstructor { Javac compiler = new Javac(declaring); try { CtMember obj = compiler.compile(src); + declaring.addLines(src.split("\n").length); if (obj instanceof CtConstructor) { // a stack map table has been already created. return (CtConstructor)obj; diff --git a/src/main/javassist/CtNewMethod.java b/src/main/javassist/CtNewMethod.java index 3daaa026..2cd363b0 100644 --- a/src/main/javassist/CtNewMethod.java +++ b/src/main/javassist/CtNewMethod.java @@ -77,6 +77,7 @@ public class CtNewMethod { compiler.recordProceed(delegateObj, delegateMethod); CtMember obj = compiler.compile(src); + declaring.addLines(src.split("\n").length); if (obj instanceof CtMethod) return (CtMethod)obj; } diff --git a/src/main/javassist/compiler/Javac.java b/src/main/javassist/compiler/Javac.java index 6e67ab66..63da50a6 100644 --- a/src/main/javassist/compiler/Javac.java +++ b/src/main/javassist/compiler/Javac.java @@ -94,7 +94,8 @@ public class Javac { * @see #recordProceed(String,String) */ public CtMember compile(String src) throws CompileError { - Parser p = new Parser(new Lex(src)); + int startLine = gen.thisClass.getLinesCount(); + Parser p = new Parser(new Lex(src, startLine)); ASTList mem = p.parseMember1(stable); try { if (mem instanceof FieldDecl) diff --git a/src/main/javassist/compiler/Lex.java b/src/main/javassist/compiler/Lex.java index c2c8666c..9f7f2ec2 100644 --- a/src/main/javassist/compiler/Lex.java +++ b/src/main/javassist/compiler/Lex.java @@ -39,6 +39,10 @@ public class Lex implements TokenId { * Constructs a lexical analyzer. */ public Lex(String s) { + this(s, 0); + } + + Lex(String s, int startLineNumber) { lastChar = -1; textBuffer = new StringBuilder(); currentToken = new Token(); @@ -47,7 +51,7 @@ public class Lex implements TokenId { input = s; position = 0; maxlen = s.length(); - lineNumber = 0; + lineNumber = startLineNumber; } public int get() { diff --git a/src/test/javassist/bytecode/BytecodeTest.java b/src/test/javassist/bytecode/BytecodeTest.java index b7ab1f33..cc02801d 100644 --- a/src/test/javassist/bytecode/BytecodeTest.java +++ b/src/test/javassist/bytecode/BytecodeTest.java @@ -317,6 +317,31 @@ public class BytecodeTest extends TestCase { 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", -- 2.39.5