View Javadoc
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  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.util.Args;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and represents a reference to the source file of this class. At most
28   * one SourceFile attribute should appear per classfile. The intention of this class is that it is instantiated from the
29   * <em>Attribute.readAttribute()</em> method.
30   *
31   * @see Attribute
32   */
33  public final class SourceFile extends Attribute {
34  
35      private int sourceFileIndex;
36  
37      /**
38       * Constructs object from input stream.
39       *
40       * @param nameIndex Index in constant pool to CONSTANT_Utf8
41       * @param length Content length in bytes
42       * @param input Input stream
43       * @param constantPool Array of constants
44       * @throws IOException if an I/O error occurs.
45       */
46      SourceFile(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
47          this(nameIndex, length, input.readUnsignedShort(), constantPool);
48      }
49  
50      /**
51       * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "SourceFile".
52       * @param length Content length in bytes, the value should be 2.
53       * @param constantPool The constant pool that this attribute is associated with.
54       * @param sourceFileIndex Index in constant pool to CONSTANT_Utf8. This string will be interpreted as the name of the
55       *        file from which this class was compiled. It will not be interpreted as indicating the name of the directory
56       *        contqining the file or an absolute path; this information has to be supplied the consumer of this attribute -
57       *        in many cases, the JVM.
58       */
59      public SourceFile(final int nameIndex, final int length, final int sourceFileIndex, final ConstantPool constantPool) {
60          super(Const.ATTR_SOURCE_FILE, nameIndex, Args.require(length, 2, "SourceFile length attribute"), constantPool);
61          this.sourceFileIndex = Args.requireU2(sourceFileIndex, 0, constantPool.getLength(), "SourceFile source file index");
62      }
63  
64      /**
65       * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
66       * physical copy.
67       *
68       * @param c Source to copy.
69       */
70      public SourceFile(final SourceFile c) {
71          this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
72      }
73  
74      /**
75       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
76       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
77       *
78       * @param v Visitor object
79       */
80      @Override
81      public void accept(final Visitor v) {
82          v.visitSourceFile(this);
83      }
84  
85      /**
86       * @return deep copy of this attribute
87       */
88      @Override
89      public Attribute copy(final ConstantPool constantPool) {
90          return (Attribute) clone();
91      }
92  
93      /**
94       * Dump source file attribute to file stream in binary format.
95       *
96       * @param file Output file stream
97       * @throws IOException if an I/O error occurs.
98       */
99      @Override
100     public void dump(final DataOutputStream file) throws IOException {
101         super.dump(file);
102         file.writeShort(sourceFileIndex);
103     }
104 
105     /**
106      * @return Index in constant pool of source file name.
107      */
108     public int getSourceFileIndex() {
109         return sourceFileIndex;
110     }
111 
112     /**
113      * @return Source file name.
114      */
115     public String getSourceFileName() {
116         return super.getConstantPool().getConstantUtf8(sourceFileIndex).getBytes();
117     }
118 
119     /**
120      * @param sourceFileIndex
121      */
122     public void setSourceFileIndex(final int sourceFileIndex) {
123         this.sourceFileIndex = sourceFileIndex;
124     }
125 
126     /**
127      * @return String representation
128      */
129     @Override
130     public String toString() {
131         return "SourceFile: " + getSourceFileName();
132     }
133 }