jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantCP.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
     1 /*
     1 /*
     2  * reserved comment block
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT REMOVE OR ALTER!
       
     4  */
     3  */
     5 /*
     4 /*
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     7  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     8  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    16  * distributed under the License is distributed on an "AS IS" BASIS,
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18  * See the License for the specific language governing permissions and
    17  * See the License for the specific language governing permissions and
    19  * limitations under the License.
    18  * limitations under the License.
    20  */
    19  */
    21 
       
    22 package com.sun.org.apache.bcel.internal.classfile;
    20 package com.sun.org.apache.bcel.internal.classfile;
    23 
    21 
    24 import java.io.*;
    22 import java.io.DataInput;
    25 import  com.sun.org.apache.bcel.internal.Constants;
    23 import java.io.DataOutputStream;
       
    24 import java.io.IOException;
       
    25 
       
    26 import com.sun.org.apache.bcel.internal.Const;
    26 
    27 
    27 /**
    28 /**
    28  * Abstract super class for Fieldref and Methodref constants.
    29  * Abstract super class for Fieldref, Methodref, InterfaceMethodref and
       
    30  * InvokeDynamic constants.
    29  *
    31  *
    30  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    32  * @version $Id: ConstantCP.java 1747278 2016-06-07 17:28:43Z britter $
    31  * @see     ConstantFieldref
    33  * @see ConstantFieldref
    32  * @see     ConstantMethodref
    34  * @see ConstantMethodref
    33  * @see     ConstantInterfaceMethodref
    35  * @see ConstantInterfaceMethodref
       
    36  * @see ConstantInvokeDynamic
    34  */
    37  */
    35 public abstract class ConstantCP extends Constant {
    38 public abstract class ConstantCP extends Constant {
    36   /** References to the constants containing the class and the field signature
       
    37    */
       
    38   protected int class_index, name_and_type_index;
       
    39 
    39 
    40   /**
    40     /**
    41    * Initialize from another object.
    41      * References to the constants containing the class and the field signature
    42    */
    42      */
    43   public ConstantCP(ConstantCP c) {
    43     // Note that this field is used to store the
    44     this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
    44     // bootstrap_method_attr_index of a ConstantInvokeDynamic.
    45   }
    45     private int class_index;
       
    46     // This field has the same meaning for all subclasses.
       
    47     private int name_and_type_index;
    46 
    48 
    47   /**
    49     /**
    48    * Initialize instance from file data.
    50      * Initialize from another object.
    49    *
    51      */
    50    * @param tag  Constant type tag
    52     public ConstantCP(final ConstantCP c) {
    51    * @param file Input stream
    53         this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
    52    * @throws IOException
    54     }
    53    */
       
    54   ConstantCP(byte tag, DataInputStream file) throws IOException
       
    55   {
       
    56     this(tag, file.readUnsignedShort(), file.readUnsignedShort());
       
    57   }
       
    58 
    55 
    59   /**
    56     /**
    60    * @param class_index Reference to the class containing the field
    57      * Initialize instance from file data.
    61    * @param name_and_type_index and the field signature
    58      *
    62    */
    59      * @param tag Constant type tag
    63   protected ConstantCP(byte tag, int class_index,
    60      * @param file Input stream
    64                        int name_and_type_index) {
    61      * @throws IOException
    65     super(tag);
    62      */
    66     this.class_index         = class_index;
    63     ConstantCP(final byte tag, final DataInput file) throws IOException {
    67     this.name_and_type_index = name_and_type_index;
    64         this(tag, file.readUnsignedShort(), file.readUnsignedShort());
    68   }
    65     }
    69 
    66 
    70   /**
    67     /**
    71    * Dump constant field reference to file stream in binary format.
    68      * @param class_index Reference to the class containing the field
    72    *
    69      * @param name_and_type_index and the field signature
    73    * @param file Output file stream
    70      */
    74    * @throws IOException
    71     protected ConstantCP(final byte tag, final int class_index, final int name_and_type_index) {
    75    */
    72         super(tag);
    76   public final void dump(DataOutputStream file) throws IOException
    73         this.class_index = class_index;
    77   {
    74         this.name_and_type_index = name_and_type_index;
    78     file.writeByte(tag);
    75     }
    79     file.writeShort(class_index);
       
    80     file.writeShort(name_and_type_index);
       
    81   }
       
    82 
    76 
    83   /**
    77     /**
    84    * @return Reference (index) to class this field or method belongs to.
    78      * Dump constant field reference to file stream in binary format.
    85    */
    79      *
    86   public final int getClassIndex()       { return class_index; }
    80      * @param file Output file stream
       
    81      * @throws IOException
       
    82      */
       
    83     @Override
       
    84     public final void dump(final DataOutputStream file) throws IOException {
       
    85         file.writeByte(super.getTag());
       
    86         file.writeShort(class_index);
       
    87         file.writeShort(name_and_type_index);
       
    88     }
    87 
    89 
    88   /**
    90     /**
    89    * @return Reference (index) to signature of the field.
    91      * @return Reference (index) to class this constant refers to.
    90    */
    92      */
    91   public final int getNameAndTypeIndex() { return name_and_type_index; }
    93     public final int getClassIndex() {
       
    94         return class_index;
       
    95     }
    92 
    96 
    93   /**
    97     /**
    94    * @param class_index points to Constant_class
    98      * @param class_index points to Constant_class
    95    */
    99      */
    96   public final void setClassIndex(int class_index) {
   100     public final void setClassIndex(final int class_index) {
    97     this.class_index = class_index;
   101         this.class_index = class_index;
    98   }
   102     }
    99 
   103 
   100   /**
   104     /**
   101    * @return Class this field belongs to.
   105      * @return Reference (index) to signature of the field.
   102    */
   106      */
   103   public String getClass(ConstantPool cp) {
   107     public final int getNameAndTypeIndex() {
   104     return cp.constantToString(class_index, Constants.CONSTANT_Class);
   108         return name_and_type_index;
   105   }
   109     }
   106 
   110 
   107   /**
   111     /**
   108    * @param name_and_type_index points to Constant_NameAndType
   112      * @param name_and_type_index points to Constant_NameAndType
   109    */
   113      */
   110   public final void setNameAndTypeIndex(int name_and_type_index) {
   114     public final void setNameAndTypeIndex(final int name_and_type_index) {
   111     this.name_and_type_index = name_and_type_index;
   115         this.name_and_type_index = name_and_type_index;
   112   }
   116     }
   113 
   117 
   114   /**
   118     /**
   115    * @return String representation.
   119      * @return Class this field belongs to.
   116    */
   120      */
   117   public final String toString() {
   121     public String getClass(final ConstantPool cp) {
   118     return super.toString() + "(class_index = " + class_index +
   122         return cp.constantToString(class_index, Const.CONSTANT_Class);
   119       ", name_and_type_index = " + name_and_type_index + ")";
   123     }
   120   }
   124 
       
   125     /**
       
   126      * @return String representation.
       
   127      *
       
   128      * not final as ConstantInvokeDynamic needs to modify
       
   129      */
       
   130     @Override
       
   131     public String toString() {
       
   132         return super.toString() + "(class_index = " + class_index + ", name_and_type_index = "
       
   133                 + name_and_type_index + ")";
       
   134     }
   121 }
   135 }