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