    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
    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 1749603 2016-06-21 20:50:19Z ggregory $
    033 * @see     Attribute
    034 */
    035public final class SourceFile extends Attribute {
    036
    037    private int sourcefile_index;
    038
    039
    040    /**
    041     * Initialize from another object. Note that both objects use the same
    042     * references (shallow copy). Use clone() for a physical copy.
    043     */
    044    public SourceFile(final SourceFile c) {
    045        this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
    046    }
    047
    048
    049    /**
    050     * Construct object from input stream.
    051     * @param name_index Index in constant pool to CONSTANT_Utf8
    052     * @param length Content length in bytes
    053     * @param input Input stream
    054     * @param constant_pool Array of constants
    055     * @throws IOException
    056     */
    057    SourceFile(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
    058            throws IOException {
    059        this(name_index, length, input.readUnsignedShort(), constant_pool);
    060    }
    061
    062
    063    /**
    064     * @param name_index Index in constant pool to CONSTANT_Utf8, which
    065     * should represent the string "SourceFile".
    066     * @param length Content length in bytes, the value should be 2.
    067     * @param constant_pool The constant pool that this attribute is
    068     * associated with.
    069     * @param sourcefile_index Index in constant pool to CONSTANT_Utf8.  This
    070     * string will be interpreted as the name of the file from which this
    071     * class was compiled.  It will not be interpreted as indicating the name
    072     * of the directory contqining the file or an absolute path; this
    073     * information has to be supplied the consumer of this attribute - in
    074     * many cases, the JVM.
    075     */
    076    public SourceFile(final int name_index, final int length, final int sourcefile_index, final ConstantPool constant_pool) {
    077        super(Const.ATTR_SOURCE_FILE, name_index, length, constant_pool);
    078        this.sourcefile_index = sourcefile_index;
    079    }
    080
    081
    082    /**
    083     * Called by objects that are traversing the nodes of the tree implicitely
    084     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    085     * fields, attributes, etc. spawns a tree of objects.
    086     *
    087     * @param v Visitor object
    088     */
    089    @Override
    090    public void accept( final Visitor v ) {
    091        v.visitSourceFile(this);
    092    }
    093
    094
    095    /**
    096     * Dump source file attribute to file stream in binary format.
    097     *
    098     * @param file Output file stream
    099     * @throws IOException
    100     */
    101    @Override
    102    public final void dump( final DataOutputStream file ) throws IOException {
    103        super.dump(file);
    104        file.writeShort(sourcefile_index);
    105    }
    106
    107
    108    /**
    109     * @return Index in constant pool of source file name.
    110     */
    111    public final int getSourceFileIndex() {
    112        return sourcefile_index;
    113    }
    114
    115
    116    /**
    117     * @param sourcefile_index
    118     */
    119    public final void setSourceFileIndex( final int sourcefile_index ) {
    120        this.sourcefile_index = sourcefile_index;
    121    }
    122
    123
    124    /**
    125     * @return Source file name.
    126     */
    127    public final String getSourceFileName() {
    128        final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(sourcefile_index,
    129                Const.CONSTANT_Utf8);
    130        return c.getBytes();
    131    }
    132
    133
    134    /**
    135     * @return String representation
    136     */
    137    @Override
    138    public final String toString() {
    139        return "SourceFile: " + getSourceFileName();
    140    }
    141
    142
    143    /**
    144     * @return deep copy of this attribute
    145     */
    146    @Override
    147    public Attribute copy( final ConstantPool _constant_pool ) {
    148        return (Attribute) clone();
    149    }
    150}