View Javadoc

1   package serp.bytecode;
2   
3   import java.io.*;
4   
5   import serp.bytecode.lowlevel.*;
6   import serp.bytecode.visitor.*;
7   
8   /***
9    * Attribute naming the source file for this class.
10   *
11   * @author Abe White
12   */
13  public class SourceFile extends Attribute {
14      int _sourceFileIndex = 0;
15  
16      SourceFile(int nameIndex, Attributes owner) {
17          super(nameIndex, owner);
18      }
19  
20      int getLength() {
21          return 2;
22      }
23  
24      /***
25       * Return the index into the class {@link ConstantPool} of the
26       * {@link UTF8Entry} naming the source file for this class, or 0 if not set.
27       */
28      public int getFileIndex() {
29          return _sourceFileIndex;
30      }
31  
32      /***
33       * Set the index into the class {@link ConstantPool} of the
34       * {@link UTF8Entry} naming the source file for this class.
35       */
36      public void setFileIndex(int sourceFileIndex) {
37          if (sourceFileIndex < 0)
38              sourceFileIndex = 0;
39          _sourceFileIndex = sourceFileIndex;
40      }
41  
42      /***
43       * Return the name of the source file, or null if not set.
44       */
45      public String getFileName() {
46          if (_sourceFileIndex == 0)
47              return null;
48          return ((UTF8Entry) getPool().getEntry(_sourceFileIndex)).getValue();
49      }
50  
51      /***
52       * Return the file object for the source file, or null if not set.
53       *
54       * @param dir the directory of the file, or null
55       */
56      public File getFile(File dir) {
57          String name = getFileName();
58          if (name == null)
59              return null;
60          return new File(dir, name);
61      }
62  
63      /***
64       * Set the name of the source file. The name should be the file name
65       * only; it should not include the path to the file.
66       */
67      public void setFile(String name) {
68          if (name == null)
69              setFileIndex(0);
70          else
71              setFileIndex(getPool().findUTF8Entry(name, true));
72      }
73  
74      /***
75       * Set the source file. Note that only the file name is recorded;
76       * the path to the file is discarded.
77       */
78      public void setFile(File file) {
79          if (file == null)
80              setFile((String) null);
81          else
82              setFile(file.getName());
83      }
84  
85      /***
86       * Set the file name from the current class name plus the .java extension.
87       */
88      public void setFromClassName() {
89          setFile(((BCClass) getOwner()).getClassName() + ".java");
90      }
91  
92      public void acceptVisit(BCVisitor visit) {
93          visit.enterSourceFile(this);
94          visit.exitSourceFile(this);
95      }
96  
97      void read(Attribute other) {
98          setFile(((SourceFile) other).getFileName());
99      }
100 
101     void read(DataInput in, int length) throws IOException {
102         setFileIndex(in.readUnsignedShort());
103     }
104 
105     void write(DataOutput out, int length) throws IOException {
106         out.writeShort(getFileIndex());
107     }
108 }