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