1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package org.apache.bcel.classfile;
19 
20 import java.io.DataInput;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 
24 import org.apache.bcel.Const;
25 
26 /**
27  * This class is derived from <em>Attribute</em> and represents a reference
28  * to the source file of this class.  At most one SourceFile attribute
29  * should appear per classfile.  The intention of this class is that it is
30  * instantiated from the <em>Attribute.readAttribute()</em> method.
31  *
32  * @version $Id$
33  * @see     Attribute
34  */
35 public final class SourceFile extends Attribute {
36 
37     private int sourcefile_index;
38 
39 
40     /**
41      * Initialize from another object. Note that both objects use the same
42      * references (shallow copy). Use clone() for a physical copy.
43      */
SourceFile(final SourceFile c)44     public SourceFile(final SourceFile c) {
45         this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
46     }
47 
48 
49     /**
50      * Construct object from input stream.
51      * @param name_index Index in constant pool to CONSTANT_Utf8
52      * @param length Content length in bytes
53      * @param input Input stream
54      * @param constant_pool Array of constants
55      * @throws IOException
56      */
SourceFile(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)57     SourceFile(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
58             throws IOException {
59         this(name_index, length, input.readUnsignedShort(), constant_pool);
60     }
61 
62 
63     /**
64      * @param name_index Index in constant pool to CONSTANT_Utf8, which
65      * should represent the string "SourceFile".
66      * @param length Content length in bytes, the value should be 2.
67      * @param constant_pool The constant pool that this attribute is
68      * associated with.
69      * @param sourcefile_index Index in constant pool to CONSTANT_Utf8.  This
70      * string will be interpreted as the name of the file from which this
71      * class was compiled.  It will not be interpreted as indicating the name
72      * of the directory contqining the file or an absolute path; this
73      * information has to be supplied the consumer of this attribute - in
74      * many cases, the JVM.
75      */
SourceFile(final int name_index, final int length, final int sourcefile_index, final ConstantPool constant_pool)76     public SourceFile(final int name_index, final int length, final int sourcefile_index, final ConstantPool constant_pool) {
77         super(Const.ATTR_SOURCE_FILE, name_index, length, constant_pool);
78         this.sourcefile_index = sourcefile_index;
79     }
80 
81 
82     /**
83      * Called by objects that are traversing the nodes of the tree implicitely
84      * defined by the contents of a Java class. I.e., the hierarchy of methods,
85      * fields, attributes, etc. spawns a tree of objects.
86      *
87      * @param v Visitor object
88      */
89     @Override
accept( final Visitor v )90     public void accept( final Visitor v ) {
91         v.visitSourceFile(this);
92     }
93 
94 
95     /**
96      * Dump source file attribute to file stream in binary format.
97      *
98      * @param file Output file stream
99      * @throws IOException
100      */
101     @Override
dump( final DataOutputStream file )102     public final void dump( final DataOutputStream file ) throws IOException {
103         super.dump(file);
104         file.writeShort(sourcefile_index);
105     }
106 
107 
108     /**
109      * @return Index in constant pool of source file name.
110      */
getSourceFileIndex()111     public final int getSourceFileIndex() {
112         return sourcefile_index;
113     }
114 
115 
116     /**
117      * @param sourcefile_index
118      */
setSourceFileIndex( final int sourcefile_index )119     public final void setSourceFileIndex( final int sourcefile_index ) {
120         this.sourcefile_index = sourcefile_index;
121     }
122 
123 
124     /**
125      * @return Source file name.
126      */
getSourceFileName()127     public final String getSourceFileName() {
128         final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(sourcefile_index,
129                 Const.CONSTANT_Utf8);
130         return c.getBytes();
131     }
132 
133 
134     /**
135      * @return String representation
136      */
137     @Override
toString()138     public final String toString() {
139         return "SourceFile: " + getSourceFileName();
140     }
141 
142 
143     /**
144      * @return deep copy of this attribute
145      */
146     @Override
copy( final ConstantPool _constant_pool )147     public Attribute copy( final ConstantPool _constant_pool ) {
148         return (Attribute) clone();
149     }
150 }
151