< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/SourceFile.java

Print this page




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



  24 
  25 import  com.sun.org.apache.bcel.internal.Constants;
  26 import  java.io.*;
  27 
  28 /**
  29  * This class is derived from <em>Attribute</em> and represents a reference
  30  * to the source file of this class.  At most one SourceFile attribute
  31  * should appear per classfile.  The intention of this class is that it is
  32  * instantiated from the <em>Attribute.readAttribute()</em> method.
  33  *
  34  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  35  * @see     Attribute
  36  */
  37 public final class SourceFile extends Attribute {

  38   private int sourcefile_index;
  39 

  40   /**
  41    * Initialize from another object. Note that both objects use the same
  42    * references (shallow copy). Use clone() for a physical copy.
  43    */
  44   public SourceFile(SourceFile c) {
  45     this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(),
  46          c.getConstantPool());
  47   }
  48 

  49   /**
  50    * Construct object from file stream.
  51    * @param name_index Index in constant pool to CONSTANT_Utf8
  52    * @param length Content length in bytes
  53    * @param file Input stream
  54    * @param constant_pool Array of constants
  55    * @throws IOException
  56    */
  57   SourceFile(int name_index, int length, DataInputStream file,
  58              ConstantPool constant_pool) throws IOException
  59   {
  60     this(name_index, length, file.readUnsignedShort(), constant_pool);
  61   }
  62 

  63   /**
  64    * @param name_index Index in constant pool to CONSTANT_Utf8, which
  65    * should represent the string "SourceFile".
  66    * @param length Content length in bytes, the value should be 2.
  67    * @param constant_pool The constant pool that this attribute is
  68    * associated with.
  69    * @param sourcefile_index Index in constant pool to CONSTANT_Utf8.  This
  70    * string will be interpreted as the name of the file from which this
  71    * class was compiled.  It will not be interpreted as indicating the name
  72    * of the directory contqining the file or an absolute path; this
  73    * information has to be supplied the consumer of this attribute - in
  74    * many cases, the JVM.
  75    */
  76   public SourceFile(int name_index, int length, int sourcefile_index,
  77                     ConstantPool constant_pool)
  78   {
  79     super(Constants.ATTR_SOURCE_FILE, name_index, length, constant_pool);
  80     this.sourcefile_index = sourcefile_index;
  81   }
  82 

  83   /**
  84    * Called by objects that are traversing the nodes of the tree implicitely
  85    * defined by the contents of a Java class. I.e., the hierarchy of methods,
  86    * fields, attributes, etc. spawns a tree of objects.
  87    *
  88    * @param v Visitor object
  89    */
  90   public void accept(Visitor v) {

  91     v.visitSourceFile(this);
  92   }
  93 

  94   /**
  95    * Dump source file attribute to file stream in binary format.
  96    *
  97    * @param file Output file stream
  98    * @throws IOException
  99    */
 100   public final void dump(DataOutputStream file) throws IOException
 101   {
 102     super.dump(file);
 103     file.writeShort(sourcefile_index);
 104   }
 105 

 106   /**
 107    * @return Index in constant pool of source file name.
 108    */
 109   public final int getSourceFileIndex() { return sourcefile_index; }



 110 
 111   /**
 112    * @param sourcefile_index.
 113    */
 114   public final void setSourceFileIndex(int sourcefile_index) {
 115     this.sourcefile_index = sourcefile_index;
 116   }
 117 

 118   /**
 119    * @return Source file name.
 120    */
 121   public final String getSourceFileName() {
 122     ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(sourcefile_index,
 123                                                              Constants.CONSTANT_Utf8);
 124     return c.getBytes();
 125   }
 126 

 127   /**
 128    * @return String representation
 129    */

 130   public final String toString() {
 131     return "SourceFile(" + getSourceFileName() + ")";
 132   }
 133 

 134   /**
 135    * @return deep copy of this attribute
 136    */
 137   public Attribute copy(ConstantPool constant_pool) {
 138     return (SourceFile)clone();

 139   }
 140 }


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

  29 
  30 /**
  31  * This class is derived from <em>Attribute</em> and represents a reference
  32  * to the source file of this class.  At most one SourceFile attribute
  33  * should appear per classfile.  The intention of this class is that it is
  34  * instantiated from the <em>Attribute.readAttribute()</em> method.
  35  *
  36  * @version $Id: SourceFile.java 1749603 2016-06-21 20:50:19Z ggregory $
  37  * @see     Attribute
  38  */
  39 public final class SourceFile extends Attribute {
  40 
  41     private int sourcefile_index;
  42 
  43 
  44     /**
  45      * Initialize from another object. Note that both objects use the same
  46      * references (shallow copy). Use clone() for a physical copy.
  47      */
  48     public SourceFile(final SourceFile c) {
  49         this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), c.getConstantPool());

  50     }
  51 
  52 
  53     /**
  54      * Construct object from input stream.
  55      * @param name_index Index in constant pool to CONSTANT_Utf8
  56      * @param length Content length in bytes
  57      * @param input Input stream
  58      * @param constant_pool Array of constants
  59      * @throws IOException
  60      */
  61     SourceFile(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
  62             throws IOException {
  63         this(name_index, length, input.readUnsignedShort(), constant_pool);

  64     }
  65 
  66 
  67     /**
  68      * @param name_index Index in constant pool to CONSTANT_Utf8, which
  69      * should represent the string "SourceFile".
  70      * @param length Content length in bytes, the value should be 2.
  71      * @param constant_pool The constant pool that this attribute is
  72      * associated with.
  73      * @param sourcefile_index Index in constant pool to CONSTANT_Utf8.  This
  74      * string will be interpreted as the name of the file from which this
  75      * class was compiled.  It will not be interpreted as indicating the name
  76      * of the directory contqining the file or an absolute path; this
  77      * information has to be supplied the consumer of this attribute - in
  78      * many cases, the JVM.
  79      */
  80     public SourceFile(final int name_index, final int length, final int sourcefile_index, final ConstantPool constant_pool) {
  81         super(Const.ATTR_SOURCE_FILE, name_index, length, constant_pool);


  82         this.sourcefile_index = sourcefile_index;
  83     }
  84 
  85 
  86     /**
  87      * Called by objects that are traversing the nodes of the tree implicitely
  88      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  89      * fields, attributes, etc. spawns a tree of objects.
  90      *
  91      * @param v Visitor object
  92      */
  93     @Override
  94     public void accept( final Visitor v ) {
  95         v.visitSourceFile(this);
  96     }
  97 
  98 
  99     /**
 100      * Dump source file attribute to file stream in binary format.
 101      *
 102      * @param file Output file stream
 103      * @throws IOException
 104      */
 105     @Override
 106     public final void dump( final DataOutputStream file ) throws IOException {
 107         super.dump(file);
 108         file.writeShort(sourcefile_index);
 109     }
 110 
 111 
 112     /**
 113      * @return Index in constant pool of source file name.
 114      */
 115     public final int getSourceFileIndex() {
 116         return sourcefile_index;
 117     }
 118 
 119 
 120     /**
 121      * @param sourcefile_index
 122      */
 123     public final void setSourceFileIndex( final int sourcefile_index ) {
 124         this.sourcefile_index = sourcefile_index;
 125     }
 126 
 127 
 128     /**
 129      * @return Source file name.
 130      */
 131     public final String getSourceFileName() {
 132         final ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(sourcefile_index,
 133                 Const.CONSTANT_Utf8);
 134         return c.getBytes();
 135     }
 136 
 137 
 138     /**
 139      * @return String representation
 140      */
 141     @Override
 142     public final String toString() {
 143         return "SourceFile: " + getSourceFileName();
 144     }
 145 
 146 
 147     /**
 148      * @return deep copy of this attribute
 149      */
 150     @Override
 151     public Attribute copy( final ConstantPool _constant_pool ) {
 152         return (Attribute) clone();
 153     }
 154 }
< prev index next >