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