01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.*;
07:
08: /* Represents the contents of a standard "SourceFile" attribute.
09: * @author Per Bothner
10: */
11:
12: public class SourceFileAttr extends Attribute {
13: String filename;
14: int filename_index;
15:
16: public String getSourceFile() {
17: return filename;
18: }
19:
20: public void setSourceFile(String filename) {
21: this .filename = filename;
22: filename_index = 0;
23: }
24:
25: public static void setSourceFile(ClassType cl, String filename) {
26: Attribute attr = Attribute.get(cl, "SourceFile");
27: if (attr != null && attr instanceof SourceFileAttr) {
28: ((SourceFileAttr) attr).setSourceFile(filename);
29: } else {
30: SourceFileAttr sattr = new SourceFileAttr(filename);
31: sattr.addToFrontOf(cl);
32: }
33: }
34:
35: public SourceFileAttr(String filename) {
36: super ("SourceFile");
37: this .filename = filename;
38: }
39:
40: public SourceFileAttr(int index, ClassType ctype) {
41: super ("SourceFile");
42: CpoolUtf8 filenameConstant = (CpoolUtf8) ctype.constants
43: .getForced(index, ConstantPool.UTF8);
44: this .filename = filenameConstant.string;
45: this .filename_index = index;
46: }
47:
48: public void assignConstants(ClassType cl) {
49: super .assignConstants(cl);
50: if (filename_index == 0)
51: filename_index = cl.getConstants().addUtf8(filename)
52: .getIndex();
53: }
54:
55: public final int getLength() {
56: return 2;
57: }
58:
59: public void write(DataOutputStream dstr) throws java.io.IOException {
60: dstr.writeShort(filename_index);
61: }
62:
63: public void print(ClassTypeWriter dst) {
64: dst.print("Attribute \"");
65: dst.print(getName());
66: dst.print("\", length:");
67: dst.print(getLength());
68: dst.print(", \"");
69: dst.print(getSourceFile());
70: dst.println('\"');
71: }
72: }
|