001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.classfile;
019
020 import java.io.DataInput;
021 import java.io.DataOutputStream;
022 import java.io.IOException;
023
024 import org.apache.bcel.Constants;
025
026 /**
027 * This class is derived from <em>Attribute</em> and represents a reference
028 * to the source file of this class. At most one SourceFile attribute
029 * should appear per classfile. The intention of this class is that it is
030 * instantiated from the <em>Attribute.readAttribute()</em> method.
031 *
032 * @version $Id: SourceFile.java 1152072 2011-07-29 01:54:05Z dbrosius $
033 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
034 * @see Attribute
035 */
036 public final class SourceFile extends Attribute {
037
038 private static final long serialVersionUID = -804226255663222912L;
039 private int sourcefile_index;
040
041
042 /**
043 * Initialize from another object. Note that both objects use the same
044 * references (shallow copy). Use clone() for a physical copy.
045 */
046 public SourceFile(SourceFile c) {
047 this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
048 }
049
050
051 /**
052 * Construct object from file stream.
053 * @param name_index Index in constant pool to CONSTANT_Utf8
054 * @param length Content length in bytes
055 * @param file Input stream
056 * @param constant_pool Array of constants
057 * @throws IOException
058 */
059 SourceFile(int name_index, int length, DataInput file, ConstantPool constant_pool)
060 throws IOException {
061 this(name_index, length, file.readUnsignedShort(), constant_pool);
062 }
063
064
065 /**
066 * @param name_index Index in constant pool to CONSTANT_Utf8, which
067 * should represent the string "SourceFile".
068 * @param length Content length in bytes, the value should be 2.
069 * @param constant_pool The constant pool that this attribute is
070 * associated with.
071 * @param sourcefile_index Index in constant pool to CONSTANT_Utf8. This
072 * string will be interpreted as the name of the file from which this
073 * class was compiled. It will not be interpreted as indicating the name
074 * of the directory contqining the file or an absolute path; this
075 * information has to be supplied the consumer of this attribute - in
076 * many cases, the JVM.
077 */
078 public SourceFile(int name_index, int length, int sourcefile_index, ConstantPool constant_pool) {
079 super(Constants.ATTR_SOURCE_FILE, name_index, length, constant_pool);
080 this.sourcefile_index = sourcefile_index;
081 }
082
083
084 /**
085 * Called by objects that are traversing the nodes of the tree implicitely
086 * defined by the contents of a Java class. I.e., the hierarchy of methods,
087 * fields, attributes, etc. spawns a tree of objects.
088 *
089 * @param v Visitor object
090 */
091 @Override
092 public void accept( Visitor v ) {
093 v.visitSourceFile(this);
094 }
095
096
097 /**
098 * Dump source file attribute to file stream in binary format.
099 *
100 * @param file Output file stream
101 * @throws IOException
102 */
103 @Override
104 public final void dump( DataOutputStream file ) throws IOException {
105 super.dump(file);
106 file.writeShort(sourcefile_index);
107 }
108
109
110 /**
111 * @return Index in constant pool of source file name.
112 */
113 public final int getSourceFileIndex() {
114 return sourcefile_index;
115 }
116
117
118 /**
119 * @param sourcefile_index
120 */
121 public final void setSourceFileIndex( int sourcefile_index ) {
122 this.sourcefile_index = sourcefile_index;
123 }
124
125
126 /**
127 * @return Source file name.
128 */
129 public final String getSourceFileName() {
130 ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(sourcefile_index,
131 Constants.CONSTANT_Utf8);
132 return c.getBytes();
133 }
134
135
136 /**
137 * @return String representation
138 */
139 @Override
140 public final String toString() {
141 return "SourceFile(" + getSourceFileName() + ")";
142 }
143
144
145 /**
146 * @return deep copy of this attribute
147 */
148 @Override
149 public Attribute copy( ConstantPool _constant_pool ) {
150 return (SourceFile) clone();
151 }
152 }