jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/Synthetic.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
    19  * limitations under the License.
    19  * limitations under the License.
    20  */
    20  */
    21 
    21 
    22 package com.sun.org.apache.bcel.internal.classfile;
    22 package com.sun.org.apache.bcel.internal.classfile;
    23 
    23 
       
    24 import java.io.DataInput;
       
    25 import java.io.DataOutputStream;
       
    26 import java.io.IOException;
    24 
    27 
    25 import  com.sun.org.apache.bcel.internal.Constants;
    28 import com.sun.org.apache.bcel.internal.Const;
    26 import  java.io.*;
       
    27 
    29 
    28 /**
    30 /**
    29  * This class is derived from <em>Attribute</em> and declares this class as
    31  * This class is derived from <em>Attribute</em> and declares this class as
    30  * `synthetic', i.e., it needs special handling.  The JVM specification
    32  * `synthetic', i.e., it needs special handling.  The JVM specification
    31  * states "A class member that does not appear in the source code must be
    33  * states "A class member that does not appear in the source code must be
    32  * marked using a Synthetic attribute."  It may appear in the ClassFile
    34  * marked using a Synthetic attribute."  It may appear in the ClassFile
    33  * attribute table, a field_info table or a method_info table.  This class
    35  * attribute table, a field_info table or a method_info table.  This class
    34  * is intended to be instantiated from the
    36  * is intended to be instantiated from the
    35  * <em>Attribute.readAttribute()</em> method.
    37  * <em>Attribute.readAttribute()</em> method.
    36  *
    38  *
    37  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    39  * @version $Id: Synthetic.java 1749603 2016-06-21 20:50:19Z ggregory $
    38  * @see     Attribute
    40  * @see     Attribute
    39  */
    41  */
    40 public final class Synthetic extends Attribute {
    42 public final class Synthetic extends Attribute {
    41   private byte[] bytes;
       
    42 
    43 
    43   /**
    44     private byte[] bytes;
    44    * Initialize from another object. Note that both objects use the same
       
    45    * references (shallow copy). Use copy() for a physical copy.
       
    46    */
       
    47   public Synthetic(Synthetic c) {
       
    48     this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
       
    49   }
       
    50 
    45 
    51   /**
       
    52    * @param name_index Index in constant pool to CONSTANT_Utf8, which
       
    53    * should represent the string "Synthetic".
       
    54    * @param length Content length in bytes - should be zero.
       
    55    * @param bytes Attribute contents
       
    56    * @param constant_pool The constant pool this attribute is associated
       
    57    * with.
       
    58    */
       
    59   public Synthetic(int name_index, int length, byte[] bytes,
       
    60                    ConstantPool constant_pool)
       
    61   {
       
    62     super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool);
       
    63     this.bytes         = bytes;
       
    64   }
       
    65 
    46 
    66   /**
    47     /**
    67    * Construct object from file stream.
    48      * Initialize from another object. Note that both objects use the same
    68    * @param name_index Index in constant pool to CONSTANT_Utf8
    49      * references (shallow copy). Use copy() for a physical copy.
    69    * @param length Content length in bytes
    50      */
    70    * @param file Input stream
    51     public Synthetic(final Synthetic c) {
    71    * @param constant_pool Array of constants
    52         this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
    72    * @throws IOException
    53     }
    73    */
       
    74   Synthetic(int name_index, int length, DataInputStream file,
       
    75             ConstantPool constant_pool) throws IOException
       
    76   {
       
    77     this(name_index, length, (byte [])null, constant_pool);
       
    78 
    54 
    79     if(length > 0) {
    55 
    80       bytes = new byte[length];
    56     /**
    81       file.readFully(bytes);
    57      * @param name_index Index in constant pool to CONSTANT_Utf8, which
    82       System.err.println("Synthetic attribute with length > 0");
    58      * should represent the string "Synthetic".
       
    59      * @param length Content length in bytes - should be zero.
       
    60      * @param bytes Attribute contents
       
    61      * @param constant_pool The constant pool this attribute is associated
       
    62      * with.
       
    63      */
       
    64     public Synthetic(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
       
    65         super(Const.ATTR_SYNTHETIC, name_index, length, constant_pool);
       
    66         this.bytes = bytes;
    83     }
    67     }
    84   }
       
    85   /**
       
    86    * Called by objects that are traversing the nodes of the tree implicitely
       
    87    * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    88    * fields, attributes, etc. spawns a tree of objects.
       
    89    *
       
    90    * @param v Visitor object
       
    91    */
       
    92   public void accept(Visitor v) {
       
    93     v.visitSynthetic(this);
       
    94   }
       
    95   /**
       
    96    * Dump source file attribute to file stream in binary format.
       
    97    *
       
    98    * @param file Output file stream
       
    99    * @throws IOException
       
   100    */
       
   101   public final void dump(DataOutputStream file) throws IOException
       
   102   {
       
   103     super.dump(file);
       
   104     if(length > 0)
       
   105       file.write(bytes, 0, length);
       
   106   }
       
   107   /**
       
   108    * @return data bytes.
       
   109    */
       
   110   public final byte[] getBytes() { return bytes; }
       
   111 
    68 
   112   /**
       
   113    * @param bytes.
       
   114    */
       
   115   public final void setBytes(byte[] bytes) {
       
   116     this.bytes = bytes;
       
   117   }
       
   118 
    69 
   119   /**
    70     /**
   120    * @return String representation.
    71      * Construct object from input stream.
   121    */
    72      *
   122   public final String toString() {
    73      * @param name_index Index in constant pool to CONSTANT_Utf8
   123     StringBuffer buf = new StringBuffer("Synthetic");
    74      * @param length Content length in bytes
       
    75      * @param input Input stream
       
    76      * @param constant_pool Array of constants
       
    77      * @throws IOException
       
    78      */
       
    79     Synthetic(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
       
    80             throws IOException {
       
    81         this(name_index, length, (byte[]) null, constant_pool);
       
    82         if (length > 0) {
       
    83             bytes = new byte[length];
       
    84             input.readFully(bytes);
       
    85             System.err.println("Synthetic attribute with length > 0");
       
    86         }
       
    87     }
   124 
    88 
   125     if(length > 0)
       
   126       buf.append(" " + Utility.toHexString(bytes));
       
   127 
    89 
   128     return buf.toString();
    90     /**
   129   }
    91      * Called by objects that are traversing the nodes of the tree implicitely
       
    92      * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    93      * fields, attributes, etc. spawns a tree of objects.
       
    94      *
       
    95      * @param v Visitor object
       
    96      */
       
    97     @Override
       
    98     public void accept( final Visitor v ) {
       
    99         v.visitSynthetic(this);
       
   100     }
   130 
   101 
   131   /**
       
   132    * @return deep copy of this attribute
       
   133    */
       
   134   public Attribute copy(ConstantPool constant_pool) {
       
   135     Synthetic c = (Synthetic)clone();
       
   136 
   102 
   137     if(bytes != null)
   103     /**
   138       c.bytes = (byte[])bytes.clone();
   104      * Dump source file attribute to file stream in binary format.
       
   105      *
       
   106      * @param file Output file stream
       
   107      * @throws IOException
       
   108      */
       
   109     @Override
       
   110     public final void dump( final DataOutputStream file ) throws IOException {
       
   111         super.dump(file);
       
   112         if (super.getLength() > 0) {
       
   113             file.write(bytes, 0, super.getLength());
       
   114         }
       
   115     }
   139 
   116 
   140     c.constant_pool = constant_pool;
   117 
   141     return c;
   118     /**
   142   }
   119      * @return data bytes.
       
   120      */
       
   121     public final byte[] getBytes() {
       
   122         return bytes;
       
   123     }
       
   124 
       
   125 
       
   126     /**
       
   127      * @param bytes
       
   128      */
       
   129     public final void setBytes( final byte[] bytes ) {
       
   130         this.bytes = bytes;
       
   131     }
       
   132 
       
   133 
       
   134     /**
       
   135      * @return String representation.
       
   136      */
       
   137     @Override
       
   138     public final String toString() {
       
   139         final StringBuilder buf = new StringBuilder("Synthetic");
       
   140         if (super.getLength() > 0) {
       
   141             buf.append(" ").append(Utility.toHexString(bytes));
       
   142         }
       
   143         return buf.toString();
       
   144     }
       
   145 
       
   146 
       
   147     /**
       
   148      * @return deep copy of this attribute
       
   149      */
       
   150     @Override
       
   151     public Attribute copy( final ConstantPool _constant_pool ) {
       
   152         final Synthetic c = (Synthetic) clone();
       
   153         if (bytes != null) {
       
   154             c.bytes = new byte[bytes.length];
       
   155             System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
       
   156         }
       
   157         c.setConstantPool(_constant_pool);
       
   158         return c;
       
   159     }
   143 }
   160 }