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 String fixSourceFile(String fname) {
26: String fsep = System.getProperty("file.separator", "/");
27: if (fsep != null && fsep.length() == 1) {
28: char fsep0 = fsep.charAt(0);
29: if (fsep0 != '/')
30: fname = fname.replace(fsep0, '/');
31: }
32: return fname;
33: }
34:
35: public static void setSourceFile(ClassType cl, String filename) {
36: Attribute attr = Attribute.get(cl, "SourceFile");
37: if (attr != null && attr instanceof SourceFileAttr) {
38: ((SourceFileAttr) attr).setSourceFile(filename);
39: } else {
40: SourceFileAttr sattr = new SourceFileAttr(filename);
41: sattr.addToFrontOf(cl);
42: }
43: }
44:
45: public SourceFileAttr(String filename) {
46: super ("SourceFile");
47: this .filename = filename;
48: }
49:
50: public SourceFileAttr(int index, ClassType ctype) {
51: super ("SourceFile");
52: CpoolUtf8 filenameConstant = (CpoolUtf8) ctype.constants
53: .getForced(index, ConstantPool.UTF8);
54: this .filename = filenameConstant.string;
55: this .filename_index = index;
56: }
57:
58: public void assignConstants(ClassType cl) {
59: super .assignConstants(cl);
60: if (filename_index == 0)
61: filename_index = cl.getConstants().addUtf8(filename)
62: .getIndex();
63: }
64:
65: public final int getLength() {
66: return 2;
67: }
68:
69: public void write(DataOutputStream dstr) throws java.io.IOException {
70: dstr.writeShort(filename_index);
71: }
72:
73: public void print(ClassTypeWriter dst) {
74: dst.print("Attribute \"");
75: dst.print(getName());
76: dst.print("\", length:");
77: dst.print(getLength());
78: dst.print(", \"");
79: dst.print(getSourceFile());
80: dst.println('\"');
81: }
82: }
|