1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import org.apache.bcel.Constants;
23
24 /***
25 * This class is derived from <em>Attribute</em> and represents a reference
26 * to the source file of this class. At most one SourceFile attribute
27 * should appear per classfile. The intention of this class is that it is
28 * instantiated from the <em>Attribute.readAttribute()</em> method.
29 *
30 * @version $Id: SourceFile.java 386056 2006-03-15 11:31:56Z tcurdt $
31 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
32 * @see Attribute
33 */
34 public final class SourceFile extends Attribute {
35
36 private int sourcefile_index;
37
38
39 /***
40 * Initialize from another object. Note that both objects use the same
41 * references (shallow copy). Use clone() for a physical copy.
42 */
43 public SourceFile(SourceFile c) {
44 this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
45 }
46
47
48 /***
49 * Construct object from file stream.
50 * @param name_index Index in constant pool to CONSTANT_Utf8
51 * @param length Content length in bytes
52 * @param file Input stream
53 * @param constant_pool Array of constants
54 * @throws IOException
55 */
56 SourceFile(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
57 throws IOException {
58 this(name_index, length, file.readUnsignedShort(), constant_pool);
59 }
60
61
62 /***
63 * @param name_index Index in constant pool to CONSTANT_Utf8, which
64 * should represent the string "SourceFile".
65 * @param length Content length in bytes, the value should be 2.
66 * @param constant_pool The constant pool that this attribute is
67 * associated with.
68 * @param sourcefile_index Index in constant pool to CONSTANT_Utf8. This
69 * string will be interpreted as the name of the file from which this
70 * class was compiled. It will not be interpreted as indicating the name
71 * of the directory contqining the file or an absolute path; this
72 * information has to be supplied the consumer of this attribute - in
73 * many cases, the JVM.
74 */
75 public SourceFile(int name_index, int length, int sourcefile_index, ConstantPool constant_pool) {
76 super(Constants.ATTR_SOURCE_FILE, name_index, length, constant_pool);
77 this.sourcefile_index = sourcefile_index;
78 }
79
80
81 /***
82 * Called by objects that are traversing the nodes of the tree implicitely
83 * defined by the contents of a Java class. I.e., the hierarchy of methods,
84 * fields, attributes, etc. spawns a tree of objects.
85 *
86 * @param v Visitor object
87 */
88 public void accept( Visitor v ) {
89 v.visitSourceFile(this);
90 }
91
92
93 /***
94 * Dump source file attribute to file stream in binary format.
95 *
96 * @param file Output file stream
97 * @throws IOException
98 */
99 public final void dump( DataOutputStream file ) throws IOException {
100 super.dump(file);
101 file.writeShort(sourcefile_index);
102 }
103
104
105 /***
106 * @return Index in constant pool of source file name.
107 */
108 public final int getSourceFileIndex() {
109 return sourcefile_index;
110 }
111
112
113 /***
114 * @param sourcefile_index
115 */
116 public final void setSourceFileIndex( int sourcefile_index ) {
117 this.sourcefile_index = sourcefile_index;
118 }
119
120
121 /***
122 * @return Source file name.
123 */
124 public final String getSourceFileName() {
125 ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(sourcefile_index,
126 Constants.CONSTANT_Utf8);
127 return c.getBytes();
128 }
129
130
131 /***
132 * @return String representation
133 */
134 public final String toString() {
135 return "SourceFile(" + getSourceFileName() + ")";
136 }
137
138
139 /***
140 * @return deep copy of this attribute
141 */
142 public Attribute copy( ConstantPool _constant_pool ) {
143 return (SourceFile) clone();
144 }
145 }