KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > SourceFileAttr


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6
7
8 /* Represents the contents of a standard "SourceFile" attribute.
9  * @author Per Bothner
10  */

11
12 public class SourceFileAttr extends Attribute
13 {
14   String JavaDoc filename;
15   int filename_index;
16
17   public String JavaDoc getSourceFile() { return filename; }
18   public void setSourceFile(String JavaDoc filename)
19   {
20     this.filename = filename;
21     filename_index = 0;
22   }
23
24   public static String JavaDoc fixSourceFile (String JavaDoc fname)
25   {
26     String JavaDoc fsep = System.getProperty("file.separator", "/");
27     if (fsep != null && fsep.length() == 1)
28       {
29     char fsep0 = fsep.charAt(0);
30     if (fsep0 != '/')
31       fname = fname.replace(fsep0, '/');
32       }
33     return fname;
34   }
35
36   public static void setSourceFile (ClassType cl, String JavaDoc filename)
37   {
38     Attribute attr = Attribute.get (cl, "SourceFile");
39     if (attr != null && attr instanceof SourceFileAttr)
40       {
41     ((SourceFileAttr)attr).setSourceFile(filename);
42       }
43     else
44       {
45     SourceFileAttr sattr = new SourceFileAttr(filename);
46         sattr.addToFrontOf(cl);
47       }
48   }
49
50   public SourceFileAttr (String JavaDoc filename)
51   {
52     super("SourceFile");
53     this.filename = filename;
54   }
55
56   public SourceFileAttr (int index, ClassType ctype)
57   {
58     super("SourceFile");
59     CpoolUtf8 filenameConstant = (CpoolUtf8)
60       ctype.constants.getForced(index, ConstantPool.UTF8);
61     this.filename = filenameConstant.string;
62     this.filename_index = index;
63   }
64
65
66   public void assignConstants (ClassType cl)
67   {
68     super.assignConstants(cl);
69     if (filename_index == 0)
70       filename_index = cl.getConstants().addUtf8(filename).getIndex();
71   }
72
73   public final int getLength() { return 2; }
74
75   public void write (DataOutputStream dstr) throws java.io.IOException JavaDoc
76   {
77     dstr.writeShort(filename_index);
78   }
79
80   public void print (ClassTypeWriter dst)
81   {
82     dst.print("Attribute \"");
83     dst.print(getName());
84     dst.print("\", length:");
85     dst.print(getLength());
86     dst.print(", \"");
87     dst.print(getSourceFile());
88     dst.println('\"');
89   }
90 }
91
Popular Tags