diff options
author | akuznetsov <akuznetsov@tradingview.com> | 2023-10-04 14:22:28 +0400 |
---|---|---|
committer | akuznetsov <akuznetsov@tradingview.com> | 2023-10-04 14:22:28 +0400 |
commit | 15d56cfb702000400ff7d888e6084da72530221e (patch) | |
tree | ba9a83625af43ec9ce0be18f49fcb01a148108df /src | |
parent | d5a7881381080fa4953b24c069675ca435598b95 (diff) | |
download | javassist-15d56cfb702000400ff7d888e6084da72530221e.tar.gz javassist-15d56cfb702000400ff7d888e6084da72530221e.zip |
save line number count though all methods/ctors
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/CtClass.java | 10 | ||||
-rw-r--r-- | src/main/javassist/CtNewConstructor.java | 1 | ||||
-rw-r--r-- | src/main/javassist/CtNewMethod.java | 1 | ||||
-rw-r--r-- | src/main/javassist/compiler/Javac.java | 3 | ||||
-rw-r--r-- | src/main/javassist/compiler/Lex.java | 6 | ||||
-rw-r--r-- | 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 <code>writeFile()</code> 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", |