Browse Source

save line number count though all methods/ctors

pull/484/head
akuznetsov 7 months ago
parent
commit
15d56cfb70

+ 10
- 0
src/main/javassist/CtClass.java View 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

+ 1
- 0
src/main/javassist/CtNewConstructor.java View 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;

+ 1
- 0
src/main/javassist/CtNewMethod.java View 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;
}

+ 2
- 1
src/main/javassist/compiler/Javac.java View 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)

+ 5
- 1
src/main/javassist/compiler/Lex.java View 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() {

+ 25
- 0
src/test/javassist/bytecode/BytecodeTest.java View 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",

Loading…
Cancel
Save