Home | History | Annotate | Download | only in classfile
      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  */
     18 package org.apache.bcel.classfile;
     19 
     20 import java.io.DataInput;
     21 import java.io.DataOutputStream;
     22 import java.io.IOException;
     23 
     24 import org.apache.bcel.Const;
     25 
     26 /**
     27  * This class is derived from the abstract {@link Constant}
     28  * and represents a reference to the name and signature
     29  * of a field or method.
     30  *
     31  * @version $Id$
     32  * @see     Constant
     33  */
     34 public final class ConstantNameAndType extends Constant {
     35 
     36     private int name_index; // Name of field/method
     37     private int signature_index; // and its signature.
     38 
     39 
     40     /**
     41      * Initialize from another object.
     42      */
     43     public ConstantNameAndType(final ConstantNameAndType c) {
     44         this(c.getNameIndex(), c.getSignatureIndex());
     45     }
     46 
     47 
     48     /**
     49      * Initialize instance from file data.
     50      *
     51      * @param file Input stream
     52      * @throws IOException
     53      */
     54     ConstantNameAndType(final DataInput file) throws IOException {
     55         this(file.readUnsignedShort(), file.readUnsignedShort());
     56     }
     57 
     58 
     59     /**
     60      * @param name_index Name of field/method
     61      * @param signature_index and its signature
     62      */
     63     public ConstantNameAndType(final int name_index, final int signature_index) {
     64         super(Const.CONSTANT_NameAndType);
     65         this.name_index = name_index;
     66         this.signature_index = signature_index;
     67     }
     68 
     69 
     70     /**
     71      * Called by objects that are traversing the nodes of the tree implicitely
     72      * defined by the contents of a Java class. I.e., the hierarchy of methods,
     73      * fields, attributes, etc. spawns a tree of objects.
     74      *
     75      * @param v Visitor object
     76      */
     77     @Override
     78     public void accept( final Visitor v ) {
     79         v.visitConstantNameAndType(this);
     80     }
     81 
     82 
     83     /**
     84      * Dump name and signature index to file stream in binary format.
     85      *
     86      * @param file Output file stream
     87      * @throws IOException
     88      */
     89     @Override
     90     public final void dump( final DataOutputStream file ) throws IOException {
     91         file.writeByte(super.getTag());
     92         file.writeShort(name_index);
     93         file.writeShort(signature_index);
     94     }
     95 
     96 
     97     /**
     98      * @return Name index in constant pool of field/method name.
     99      */
    100     public final int getNameIndex() {
    101         return name_index;
    102     }
    103 
    104 
    105     /** @return name
    106      */
    107     public final String getName( final ConstantPool cp ) {
    108         return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8);
    109     }
    110 
    111 
    112     /**
    113      * @return Index in constant pool of field/method signature.
    114      */
    115     public final int getSignatureIndex() {
    116         return signature_index;
    117     }
    118 
    119 
    120     /** @return signature
    121      */
    122     public final String getSignature( final ConstantPool cp ) {
    123         return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8);
    124     }
    125 
    126 
    127     /**
    128      * @param name_index the name index of this constant
    129      */
    130     public final void setNameIndex( final int name_index ) {
    131         this.name_index = name_index;
    132     }
    133 
    134 
    135     /**
    136      * @param signature_index the signature index in the constant pool of this type
    137      */
    138     public final void setSignatureIndex( final int signature_index ) {
    139         this.signature_index = signature_index;
    140     }
    141 
    142 
    143     /**
    144      * @return String representation
    145      */
    146     @Override
    147     public final String toString() {
    148         return super.toString() + "(name_index = " + name_index + ", signature_index = "
    149                 + signature_index + ")";
    150     }
    151 }
    152