]> source.dussan.org Git - javassist.git/commitdiff
save line number count though all methods/ctors
authorakuznetsov <akuznetsov@tradingview.com>
Wed, 4 Oct 2023 10:22:28 +0000 (14:22 +0400)
committerakuznetsov <akuznetsov@tradingview.com>
Wed, 4 Oct 2023 10:22:28 +0000 (14:22 +0400)
src/main/javassist/CtClass.java
src/main/javassist/CtNewConstructor.java
src/main/javassist/CtNewMethod.java
src/main/javassist/compiler/Javac.java
src/main/javassist/compiler/Lex.java
src/test/javassist/bytecode/BytecodeTest.java

index 8880b1f61fe5fa4b58d2396bef72e553549d1da5..24f4e25f024cd0963b8e4d7a1c12f7ba4bd6db7c 100644 (file)
@@ -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 <code>writeFile()</code> does although this
      * method does not prune or freeze the class after writing the class
index 9f4225f1895fb48cd63e475b4d120973bb5c0417..ed3e379b9332ec532d2b4a273047e2dc734fb213 100644 (file)
@@ -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;
index 3daaa026043db6a982b1acf55fa5728b7f50a95f..2cd363b024f6a6d74dea23a6e0a395a0ea37be64 100644 (file)
@@ -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;
         }
index 6e67ab668c5356dc9e157203f8e58fcc9127a906..63da50a6dececeda36f440e9f10760debd69e11e 100644 (file)
@@ -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)
index c2c8666c1f33f672d0fab0add3c714e29c3859cd..9f7f2ec2148c58969322892d2f577e92f8b40f3c 100644 (file)
@@ -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() {
index b7ab1f33e4996ad04e0ebb299cfcd8951574ce15..cc02801df4d7366177a619956962093fb3566900 100644 (file)
@@ -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",