diff options
author | Shigeru Chiba <chibash@users.noreply.github.com> | 2024-05-03 19:01:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-03 19:01:47 +0900 |
commit | bb261b443831e52409eeed6e49ddfb24db2c4d08 (patch) | |
tree | 3f8189ef73984d71ad0b8fe595c0e3a2a425e6bb /src/main/javassist/bytecode | |
parent | 204d67844433c9b781054154695eeb14c2291847 (diff) | |
parent | 9777bae93cc84249fe08a7e81b2101c415bd55e5 (diff) | |
download | javassist-bb261b443831e52409eeed6e49ddfb24db2c4d08.tar.gz javassist-bb261b443831e52409eeed6e49ddfb24db2c4d08.zip |
Merge pull request #484 from kuznet1/master
Line numbers support
Diffstat (limited to 'src/main/javassist/bytecode')
-rw-r--r-- | src/main/javassist/bytecode/CodeAttribute.java | 14 | ||||
-rw-r--r-- | src/main/javassist/bytecode/LineNumberAttribute.java | 2 | ||||
-rw-r--r-- | src/main/javassist/bytecode/LineNumberAttributeBuilder.java | 40 |
3 files changed, 55 insertions, 1 deletions
diff --git a/src/main/javassist/bytecode/CodeAttribute.java b/src/main/javassist/bytecode/CodeAttribute.java index 091edf4c..cf8806ef 100644 --- a/src/main/javassist/bytecode/CodeAttribute.java +++ b/src/main/javassist/bytecode/CodeAttribute.java @@ -353,6 +353,20 @@ public class CodeAttribute extends AttributeInfo implements Opcode { } /** + * 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. */ private byte[] copyCode(ConstPool destCp, Map<String,String> classnames, diff --git a/src/main/javassist/bytecode/LineNumberAttribute.java b/src/main/javassist/bytecode/LineNumberAttribute.java index cead96e0..16b37377 100644 --- a/src/main/javassist/bytecode/LineNumberAttribute.java +++ b/src/main/javassist/bytecode/LineNumberAttribute.java @@ -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); } diff --git a/src/main/javassist/bytecode/LineNumberAttributeBuilder.java b/src/main/javassist/bytecode/LineNumberAttributeBuilder.java new file mode 100644 index 00000000..52d9d0c8 --- /dev/null +++ b/src/main/javassist/bytecode/LineNumberAttributeBuilder.java @@ -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); + } + } +} |