aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/javassist/bytecode')
-rw-r--r--src/main/javassist/bytecode/CodeAttribute.java14
-rw-r--r--src/main/javassist/bytecode/LineNumberAttribute.java2
-rw-r--r--src/main/javassist/bytecode/LineNumberAttributeBuilder.java40
3 files changed, 55 insertions, 1 deletions
diff --git a/src/main/javassist/bytecode/CodeAttribute.java b/src/main/javassist/bytecode/CodeAttribute.java
index 4c8ea2f2..98479b21 100644
--- a/src/main/javassist/bytecode/CodeAttribute.java
+++ b/src/main/javassist/bytecode/CodeAttribute.java
@@ -348,6 +348,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);
+ }
+ }
+}