1 cananian 1.1.2.2 // AttributeSourceFile.java, created Mon Jan 18 22:44:36 1999 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.2.1 package harpoon.IR.RawClass;
 5 cananian 1.1.2.1 
 6 cananian 1.1.2.1 /** 
 7 cananian 1.1.2.1  * The <code>SourceFile</code> attribute is an optional fixed-length
 8 cananian 1.1.2.1  * attribute in the <code>attributes</code> table of the
 9 cananian 1.1.2.1  * <code>ClassFile</code> structure.  There can be no more than one
10 cananian 1.1.2.1  * <code>SourceFile</code> attribute in the <code>attributes</code>
11 cananian 1.1.2.1  *  table of a given <code>ClassFile</code> structure.
12 cananian 1.1.2.1  * <p>
13 cananian 1.1.2.1  * Only the name of the source file is given by the
14 cananian 1.1.2.1  * <code>SourceFile</code> attribute.  It never represents tha name of a
15 cananian 1.1.2.1  * directory containing the file or an absolute path name for the file.
16 cananian 1.1.2.1  * For instance, the <code>SourceFile</code> attribute might contain the
17 cananian 1.1.2.1  * file name <code>foo.java</code> but not the UNIX pathname
18 cananian 1.1.2.1  * <code>/home/lindholm/foo.java</code>.
19 cananian 1.1.2.1  *
20 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
21 cananian 1.3      * @version $Id: AttributeSourceFile.java,v 1.3 2003/09/05 21:45:16 cananian Exp $
22 cananian 1.1.2.1  * @see "The Java Virtual Machine Specification, section 4.7.2"
23 cananian 1.1.2.1  * @see Attribute
24 cananian 1.1.2.1  * @see ClassFile
25 cananian 1.1.2.1  */
26 cananian 1.1.2.1 public class AttributeSourceFile extends Attribute {
27 cananian 1.3       /** The string naming this <code>Attribute</code> type. */
28 cananian 1.3       public static final String ATTRIBUTE_NAME = "SourceFile";
29 cananian 1.1.2.1   /** The value of the <code>sourcefile_index</code> item must be a
30 cananian 1.1.2.1       valid index into the <code>constant_pool</code> table.  The
31 cananian 1.1.2.1       constant pool entry at that index must be a
32 cananian 1.1.2.1       <code>CONSTANT_Utf8_info</code> structure representing the
33 cananian 1.1.2.1       string giving the name of the source file from which this
34 cananian 1.1.2.1       <code>class</code> file was compiled. */
35 cananian 1.1.2.1   public int sourcefile_index;
36 cananian 1.1.2.1   
37 cananian 1.1.2.1   /** Constructor. */
38 cananian 1.1.2.1   AttributeSourceFile(ClassFile parent, ClassDataInputStream in,
39 cananian 1.1.2.1                       int attribute_name_index) throws java.io.IOException {
40 cananian 1.1.2.1     super(parent, attribute_name_index);
41 cananian 1.1.2.1     long attribute_length = in.read_u4();
42 cananian 1.1.2.1     if (attribute_length != 2)
43 cananian 1.1.2.1       throw new ClassDataException("SourceFile attribute with length "
44 cananian 1.1.2.1                                    + attribute_length);
45 cananian 1.1.2.1     sourcefile_index = in.read_u2();
46 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
47 cananian 1.1.2.1   }
48 cananian 1.1.2.1   /** Constructor. */
49 cananian 1.1.2.1   public AttributeSourceFile(ClassFile parent, int attribute_name_index,
50 cananian 1.1.2.1                              int sourcefile_index) {
51 cananian 1.1.2.1     super(parent, attribute_name_index);
52 cananian 1.1.2.1     this.sourcefile_index = sourcefile_index;
53 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
54 cananian 1.1.2.1   }
55 cananian 1.1.2.1   public long attribute_length() { return 2; }
56 cananian 1.1.2.1 
57 cananian 1.1.2.1   // convenience
58 cananian 1.1.2.1   public ConstantUtf8 sourcefile_index()
59 cananian 1.1.2.1   { return (ConstantUtf8) parent.constant_pool[sourcefile_index]; }
60 cananian 1.1.2.1   public String sourcefile() { return sourcefile_index().val; }
61 cananian 1.1.2.1 
62 cananian 1.1.2.1   /** Write to bytecode stream. */
63 cananian 1.1.2.1   public void write(ClassDataOutputStream out) throws java.io.IOException {
64 cananian 1.1.2.1     out.write_u2(attribute_name_index);
65 cananian 1.1.2.1     out.write_u4(attribute_length());
66 cananian 1.1.2.1     out.write_u2(sourcefile_index);
67 cananian 1.1.2.1   }
68 cananian 1.1.2.1 
69 cananian 1.1.2.1   /** Pretty-print this attribute structure. 
70 cananian 1.1.2.1    *  @param indent the indentation level to use.
71 cananian 1.1.2.1    */
72 cananian 1.1.2.1   public void print(java.io.PrintWriter pw, int indent) {
73 cananian 1.1.2.1     indent(pw, indent, "SourceFile Attribute: " + 
74 cananian 1.1.2.1            sourcefile() + " {" + sourcefile_index + "}");
75 cananian 1.1.2.1   }
76 cananian 1.2     }