jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/SourceFile.java
author joehw
Mon, 17 Apr 2017 16:24:10 -0700
changeset 44797 8b3b3b911b8a
parent 25868 686eef1e7a79
child 46174 5611d2529b49
permissions -rw-r--r--
8162572: Update License Header for all JAXP sources Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
/*
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    14
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
 */
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.classfile;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import  com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import  java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * This class is derived from <em>Attribute</em> and represents a reference
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * to the source file of this class.  At most one SourceFile attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * should appear per classfile.  The intention of this class is that it is
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * instantiated from the <em>Attribute.readAttribute()</em> method.
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * @see     Attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
public final class SourceFile extends Attribute {
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
  private int sourcefile_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
   * Initialize from another object. Note that both objects use the same
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
   * references (shallow copy). Use clone() for a physical copy.
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
  public SourceFile(SourceFile c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(),
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
         c.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
   * Construct object from file stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
   * @param name_index Index in constant pool to CONSTANT_Utf8
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   * @param length Content length in bytes
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * @param file Input stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   * @param constant_pool Array of constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  SourceFile(int name_index, int length, DataInputStream file,
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
             ConstantPool constant_pool) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    this(name_index, length, file.readUnsignedShort(), constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   * @param name_index Index in constant pool to CONSTANT_Utf8, which
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   * should represent the string "SourceFile".
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * @param length Content length in bytes, the value should be 2.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * @param constant_pool The constant pool that this attribute is
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * associated with.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * @param sourcefile_index Index in constant pool to CONSTANT_Utf8.  This
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * string will be interpreted as the name of the file from which this
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * class was compiled.  It will not be interpreted as indicating the name
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * of the directory contqining the file or an absolute path; this
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * information has to be supplied the consumer of this attribute - in
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   * many cases, the JVM.
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  public SourceFile(int name_index, int length, int sourcefile_index,
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
                    ConstantPool constant_pool)
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    super(Constants.ATTR_SOURCE_FILE, name_index, length, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    this.sourcefile_index = sourcefile_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   * Called by objects that are traversing the nodes of the tree implicitely
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   * defined by the contents of a Java class. I.e., the hierarchy of methods,
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
   * fields, attributes, etc. spawns a tree of objects.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   * @param v Visitor object
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  public void accept(Visitor v) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    v.visitSourceFile(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   * Dump source file attribute to file stream in binary format.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * @param file Output file stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   * @throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
  public final void dump(DataOutputStream file) throws IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    super.dump(file);
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    file.writeShort(sourcefile_index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
   * @return Index in constant pool of source file name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  public final int getSourceFileIndex() { return sourcefile_index; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * @param sourcefile_index.
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
  public final void setSourceFileIndex(int sourcefile_index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    this.sourcefile_index = sourcefile_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * @return Source file name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  public final String getSourceFileName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(sourcefile_index,
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
                                                             Constants.CONSTANT_Utf8);
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    return c.getBytes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   * @return String representation
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  public final String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    return "SourceFile(" + getSourceFileName() + ")";
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   * @return deep copy of this attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  public Attribute copy(ConstantPool constant_pool) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    return (SourceFile)clone();
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
}