    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 */
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.Const;
    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     * Construct object from input stream.
    039     *
    040     * @param name_index Index in constant pool to CONSTANT_Utf8
    041     * @param length Content length in bytes
    042     * @param input Input stream
    043     * @param constant_pool Array of constants
    044     * @throws IOException if an I/O error occurs.
    045     */
    046    SourceFile(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
    047        this(name_index, length, input.readUnsignedShort(), constant_pool);
    048    }
    049
    050    /**
    051     * @param name_index 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 name_index, final int length, final int sourceFileIndex, final ConstantPool constantPool) {
    060        super(Const.ATTR_SOURCE_FILE, name_index, length, constantPool);
    061        this.sourceFileIndex = sourceFileIndex;
    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    public SourceFile(final SourceFile c) {
    069        this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
    070    }
    071
    072    /**
    073     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    074     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    075     *
    076     * @param v Visitor object
    077     */
    078    @Override
    079    public void accept(final Visitor v) {
    080        v.visitSourceFile(this);
    081    }
    082
    083    /**
    084     * @return deep copy of this attribute
    085     */
    086    @Override
    087    public Attribute copy(final ConstantPool constantPool) {
    088        return (Attribute) clone();
    089    }
    090
    091    /**
    092     * Dump source file attribute to file stream in binary format.
    093     *
    094     * @param file Output file stream
    095     * @throws IOException if an I/O error occurs.
    096     */
    097    @Override
    098    public void dump(final DataOutputStream file) throws IOException {
    099        super.dump(file);
    100        file.writeShort(sourceFileIndex);
    101    }
    102
    103    /**
    104     * @return Index in constant pool of source file name.
    105     */
    106    public int getSourceFileIndex() {
    107        return sourceFileIndex;
    108    }
    109
    110    /**
    111     * @return Source file name.
    112     */
    113    public String getSourceFileName() {
    114        return super.getConstantPool().getConstantUtf8(sourceFileIndex).getBytes();
    115    }
    116
    117    /**
    118     * @param sourceFileIndex
    119     */
    120    public void setSourceFileIndex(final int sourceFileIndex) {
    121        this.sourceFileIndex = sourceFileIndex;
    122    }
    123
    124    /**
    125     * @return String representation
    126     */
    127    @Override
    128    public String toString() {
    129        return "SourceFile: " + getSourceFileName();
    130    }
    131}