Skip to content

Commit

Permalink
Merge pull request #492 from kuznet1/master
Browse files Browse the repository at this point in the history
Fix wrong line numbers in some cases
  • Loading branch information
chibash authored Oct 3, 2024
2 parents c3bbec2 + 4520f03 commit 7013f30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/javassist/bytecode/LineNumberAttributeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
public class LineNumberAttributeBuilder {
private final HashMap<Integer, Integer> map = new HashMap<>();

public void put(int newPc, ASTree tree) {
public void put(int pc, ASTree tree) {
if (tree != null)
put(newPc, tree.getLineNumber());
put(pc, tree.getLineNumber());
}

private void put(int newPc, int lineNum) {
Integer pc = map.get(lineNum);
if (pc == null || newPc < pc) {
map.put(lineNum, newPc);
private void put(int pc, int lineNum) {
Integer oldLineNum = map.get(pc);
if (oldLineNum == null || lineNum > oldLineNum) {
map.put(pc, lineNum);
}
}

Expand All @@ -29,8 +29,8 @@ public LineNumberAttribute build(ConstPool cp) {
DataOutputStream dos = new DataOutputStream(bos)) {
dos.writeShort(size);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
dos.writeShort(entry.getValue());
dos.writeShort(entry.getKey());
dos.writeShort(entry.getValue());
}
return new LineNumberAttribute(cp, bos.toByteArray());
} catch (IOException e) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/javassist/LineNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public void testFor() {
"}"), 1, 5);
}

public void test16Line() {
doTestRuntime(String.join("\n",
"public void run() {",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"throwException();",
"}"), 1, 16);
}

private void doTestCompile(String src, String msg) {
CtClass testClass = loader.makeClass("javassist.LineNumberCompileTest" + classNumber++);
try {
Expand Down

0 comments on commit 7013f30

Please sign in to comment.