Skip to content

Commit

Permalink
Merge pull request #484 from kuznet1/master
Browse files Browse the repository at this point in the history
Line numbers support
  • Loading branch information
chibash authored May 3, 2024
2 parents 204d678 + 9777bae commit bb261b4
Show file tree
Hide file tree
Showing 49 changed files with 958 additions and 478 deletions.
10 changes: 10 additions & 0 deletions src/main/javassist/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public abstract class CtClass {
*/
public static final String version = "3.30.2-GA";

private int linesCount = 0;

/**
* Prints the version number and the copyright notice.
*
Expand Down Expand Up @@ -1575,6 +1577,14 @@ protected DataOutputStream makeFileOutput(String directoryName) {
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
Expand Down
1 change: 1 addition & 0 deletions src/main/javassist/CtNewConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static CtConstructor make(String src, CtClass declaring)
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;
Expand Down
1 change: 1 addition & 0 deletions src/main/javassist/CtNewMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static CtMethod make(String src, CtClass declaring,
compiler.recordProceed(delegateObj, delegateMethod);

CtMember obj = compiler.compile(src);
declaring.addLines(src.split("\n").length);
if (obj instanceof CtMethod)
return (CtMethod)obj;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/javassist/bytecode/CodeAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,20 @@ public void setAttribute(StackMap sm) {
attributes.add(sm);
}

/**
* Adds a line number table. If another copy of line number table
* is already contained, the old one is removed.
*
* @param lnt the line number table added to this code attribute.
* If it is null, a new line number is not added.
* Only the old line number is removed.
*/
public void setAttribute(LineNumberAttribute lnt) {
AttributeInfo.remove(attributes, LineNumberAttribute.tag);
if (lnt != null)
attributes.add(lnt);
}

/**
* Copies code.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/javassist/bytecode/LineNumberAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class LineNumberAttribute extends AttributeInfo {
super(cp, n, in);
}

private LineNumberAttribute(ConstPool cp, byte[] i) {
LineNumberAttribute(ConstPool cp, byte[] i) {
super(cp, tag, i);
}

Expand Down
40 changes: 40 additions & 0 deletions src/main/javassist/bytecode/LineNumberAttributeBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package javassist.bytecode;

import javassist.compiler.ast.ASTree;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LineNumberAttributeBuilder {
private final HashMap<Integer, Integer> map = new HashMap<>();

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

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

public LineNumberAttribute build(ConstPool cp) {
int size = map.size();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(size * 4 + 2);
DataOutputStream dos = new DataOutputStream(bos)) {
dos.writeShort(size);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
dos.writeShort(entry.getValue());
dos.writeShort(entry.getKey());
}
return new LineNumberAttribute(cp, bos.toByteArray());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit bb261b4

Please sign in to comment.