SourceFile.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * This class is derived from <em>Attribute</em> and represents a reference to the source file of this class. At most
  25.  * one SourceFile attribute should appear per classfile. The intention of this class is that it is instantiated from the
  26.  * <em>Attribute.readAttribute()</em> method.
  27.  *
  28.  * @see Attribute
  29.  */
  30. public final class SourceFile extends Attribute {

  31.     private int sourceFileIndex;

  32.     /**
  33.      * Constructs object from input stream.
  34.      *
  35.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  36.      * @param length Content length in bytes
  37.      * @param input Input stream
  38.      * @param constantPool Array of constants
  39.      * @throws IOException if an I/O error occurs.
  40.      */
  41.     SourceFile(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  42.         this(nameIndex, length, input.readUnsignedShort(), constantPool);
  43.     }

  44.     /**
  45.      * @param nameIndex Index in constant pool to CONSTANT_Utf8, which should represent the string "SourceFile".
  46.      * @param length Content length in bytes, the value should be 2.
  47.      * @param constantPool The constant pool that this attribute is associated with.
  48.      * @param sourceFileIndex Index in constant pool to CONSTANT_Utf8. This string will be interpreted as the name of the
  49.      *        file from which this class was compiled. It will not be interpreted as indicating the name of the directory
  50.      *        contqining the file or an absolute path; this information has to be supplied the consumer of this attribute -
  51.      *        in many cases, the JVM.
  52.      */
  53.     public SourceFile(final int nameIndex, final int length, final int sourceFileIndex, final ConstantPool constantPool) {
  54.         super(Const.ATTR_SOURCE_FILE, nameIndex, Args.require(length, 2, "SourceFile length attribute"), constantPool);
  55.         this.sourceFileIndex = Args.requireU2(sourceFileIndex, 0, constantPool.getLength(), "SourceFile source file index");
  56.     }

  57.     /**
  58.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
  59.      * physical copy.
  60.      *
  61.      * @param c Source to copy.
  62.      */
  63.     public SourceFile(final SourceFile c) {
  64.         this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());
  65.     }

  66.     /**
  67.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  68.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  69.      *
  70.      * @param v Visitor object
  71.      */
  72.     @Override
  73.     public void accept(final Visitor v) {
  74.         v.visitSourceFile(this);
  75.     }

  76.     /**
  77.      * @return deep copy of this attribute
  78.      */
  79.     @Override
  80.     public Attribute copy(final ConstantPool constantPool) {
  81.         return (Attribute) clone();
  82.     }

  83.     /**
  84.      * Dump source file attribute to file stream in binary format.
  85.      *
  86.      * @param file Output file stream
  87.      * @throws IOException if an I/O error occurs.
  88.      */
  89.     @Override
  90.     public void dump(final DataOutputStream file) throws IOException {
  91.         super.dump(file);
  92.         file.writeShort(sourceFileIndex);
  93.     }

  94.     /**
  95.      * @return Index in constant pool of source file name.
  96.      */
  97.     public int getSourceFileIndex() {
  98.         return sourceFileIndex;
  99.     }

  100.     /**
  101.      * @return Source file name.
  102.      */
  103.     public String getSourceFileName() {
  104.         return super.getConstantPool().getConstantUtf8(sourceFileIndex).getBytes();
  105.     }

  106.     /**
  107.      * @param sourceFileIndex
  108.      */
  109.     public void setSourceFileIndex(final int sourceFileIndex) {
  110.         this.sourceFileIndex = sourceFileIndex;
  111.     }

  112.     /**
  113.      * @return String representation
  114.      */
  115.     @Override
  116.     public String toString() {
  117.         return "SourceFile: " + getSourceFileName();
  118.     }
  119. }