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 */ 017package org.apache.bcel.classfile; 018 019import java.io.DataInput; 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.Const; 024 025/** 026 * This class is derived from <em>Attribute</em> and represents a reference to the source file of this class. At most 027 * one SourceFile attribute should appear per classfile. The intention of this class is that it is instantiated from the 028 * <em>Attribute.readAttribute()</em> method. 029 * 030 * @see Attribute 031 */ 032public final class SourceFile extends Attribute { 033 034 private int sourceFileIndex; 035 036 /** 037 * Construct object from input stream. 038 * 039 * @param nameIndex Index in constant pool to CONSTANT_Utf8 040 * @param length Content length in bytes 041 * @param input Input stream 042 * @param constantPool Array of constants 043 * @throws IOException if an I/O error occurs. 044 */ 045 SourceFile(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 046 this(nameIndex, length, input.readUnsignedShort(), constantPool); 047 } 048 049 /** 050 * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "SourceFile". 051 * @param length Content length in bytes, the value should be 2. 052 * @param constantPool The constant pool that this attribute is associated with. 053 * @param sourceFileIndex Index in constant pool to CONSTANT_Utf8. This string will be interpreted as the name of the 054 * file from which this class was compiled. It will not be interpreted as indicating the name of the directory 055 * contqining the file or an absolute path; this information has to be supplied the consumer of this attribute - 056 * in many cases, the JVM. 057 */ 058 public SourceFile(final int nameIndex, final int length, final int sourceFileIndex, final ConstantPool constantPool) { 059 super(Const.ATTR_SOURCE_FILE, nameIndex, length, constantPool); 060 this.sourceFileIndex = sourceFileIndex; 061 } 062 063 /** 064 * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a 065 * physical copy. 066 */ 067 public SourceFile(final SourceFile c) { 068 this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool()); 069 } 070 071 /** 072 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 073 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 074 * 075 * @param v Visitor object 076 */ 077 @Override 078 public void accept(final Visitor v) { 079 v.visitSourceFile(this); 080 } 081 082 /** 083 * @return deep copy of this attribute 084 */ 085 @Override 086 public Attribute copy(final ConstantPool constantPool) { 087 return (Attribute) clone(); 088 } 089 090 /** 091 * Dump source file attribute to file stream in binary format. 092 * 093 * @param file Output file stream 094 * @throws IOException if an I/O error occurs. 095 */ 096 @Override 097 public void dump(final DataOutputStream file) throws IOException { 098 super.dump(file); 099 file.writeShort(sourceFileIndex); 100 } 101 102 /** 103 * @return Index in constant pool of source file name. 104 */ 105 public int getSourceFileIndex() { 106 return sourceFileIndex; 107 } 108 109 /** 110 * @return Source file name. 111 */ 112 public String getSourceFileName() { 113 return super.getConstantPool().getConstantUtf8(sourceFileIndex).getBytes(); 114 } 115 116 /** 117 * @param sourceFileIndex 118 */ 119 public void setSourceFileIndex(final int sourceFileIndex) { 120 this.sourceFileIndex = sourceFileIndex; 121 } 122 123 /** 124 * @return String representation 125 */ 126 @Override 127 public String toString() { 128 return "SourceFile: " + getSourceFileName(); 129 } 130}