summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JSourceFileAttribute.java
blob: 037e7278376c46d04319f97e74afbf49af04f28d (plain) (blame)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2012 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Sourcefile attribute, which can be attached to class files to
 * associate them with their source file.
 *
 * There can be no more than one SourceFile attribute in the attributes table
 * of a given ClassFile structure. See section 4.8.9 of the JVM specification.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JSourceFileAttribute extends JAttribute {
    protected final String sourceFileName;
    protected final int sourceFileIndex;

    public JSourceFileAttribute(FJBGContext context,
                                JClass clazz,
                                String sourceFileName) {
        super(context, clazz);
        this.sourceFileName = sourceFileName;
        this.sourceFileIndex = clazz.getConstantPool().addUtf8(sourceFileName);
    }

    public JSourceFileAttribute(FJBGContext context,
                                JClass clazz,
                                Object owner,
                                String name,
                                int size,
                                DataInputStream stream)
        throws IOException {
        super(context, clazz, name);

        this.sourceFileIndex = stream.readShort();
        this.sourceFileName = clazz.getConstantPool().lookupUtf8(sourceFileIndex);

        assert name.equals(getName());
    }

    public String getName() { return "SourceFile"; }

    public String getFileName() { return sourceFileName; }

    // Follows javap output format for SourceFile attribute.
    /*@Override*/ public String toString() {
        StringBuffer buf = new StringBuffer("  SourceFile: \"");
        buf.append(sourceFileName);
        buf.append("\"\n");
        return buf.toString();
    }

    protected int getSize() {
        return 2; // Short.SIZE
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        stream.writeShort(sourceFileIndex);
    }
}