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