aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/bytecode/LineNumberAttributeBuilder.java
blob: 25182e2de03f627f92c9ddbf1a796d940244fb1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 pc, ASTree tree) {
        if (tree != null)
            put(pc, tree.getLineNumber());
    }

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

    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.getKey());
                dos.writeShort(entry.getValue());
            }
            return new LineNumberAttribute(cp, bos.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}