View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.classfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutputStream;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  
27  /**
28   * This class is derived from the abstract {@link Constant} and represents a reference to the name and signature of a
29   * field or method.
30   *
31   * @see Constant
32   */
33  public final class ConstantNameAndType extends Constant {
34  
35      private int nameIndex; // Name of field/method
36      private int signatureIndex; // and its signature.
37  
38      /**
39       * Initialize from another object.
40       *
41       * @param c Source to copy.
42       */
43      public ConstantNameAndType(final ConstantNameAndType c) {
44          this(c.getNameIndex(), c.getSignatureIndex());
45      }
46  
47      /**
48       * Initialize instance from file data.
49       *
50       * @param file Input stream.
51       * @throws IOException if an I/O error occurs.
52       */
53      ConstantNameAndType(final DataInput file) throws IOException {
54          this(file.readUnsignedShort(), file.readUnsignedShort());
55      }
56  
57      /**
58       * Constructs a ConstantNameAndType.
59       *
60       * @param nameIndex Name of field/method.
61       * @param signatureIndex and its signature.
62       */
63      public ConstantNameAndType(final int nameIndex, final int signatureIndex) {
64          super(Const.CONSTANT_NameAndType);
65          this.nameIndex = nameIndex;
66          this.signatureIndex = signatureIndex;
67      }
68  
69      /**
70       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
71       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
72       *
73       * @param v Visitor object.
74       */
75      @Override
76      public void accept(final Visitor v) {
77          v.visitConstantNameAndType(this);
78      }
79  
80      /**
81       * Dumps name and signature index to file stream in binary format.
82       *
83       * @param file Output file stream.
84       * @throws IOException if an I/O error occurs.
85       */
86      @Override
87      public void dump(final DataOutputStream file) throws IOException {
88          file.writeByte(super.getTag());
89          file.writeShort(nameIndex);
90          file.writeShort(signatureIndex);
91      }
92  
93      /**
94       * Gets the name.
95       *
96       * @param cp the constant pool.
97       * @return name.
98       */
99      public String getName(final ConstantPool cp) {
100         return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8);
101     }
102 
103     /**
104      * Gets the name index in constant pool of field/method name.
105      *
106      * @return Name index in constant pool of field/method name.
107      */
108     public int getNameIndex() {
109         return nameIndex;
110     }
111 
112     /**
113      * Gets the signature.
114      *
115      * @param cp the constant pool.
116      * @return signature.
117      */
118     public String getSignature(final ConstantPool cp) {
119         return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8);
120     }
121 
122     /**
123      * Gets the index in constant pool of field/method signature.
124      *
125      * @return Index in constant pool of field/method signature.
126      */
127     public int getSignatureIndex() {
128         return signatureIndex;
129     }
130 
131     /**
132      * Sets the name index.
133      *
134      * @param nameIndex the name index of this constant.
135      */
136     public void setNameIndex(final int nameIndex) {
137         this.nameIndex = nameIndex;
138     }
139 
140     /**
141      * Sets the signature index.
142      *
143      * @param signatureIndex the signature index in the constant pool of this type.
144      */
145     public void setSignatureIndex(final int signatureIndex) {
146         this.signatureIndex = signatureIndex;
147     }
148 
149     /**
150      * @return String representation.
151      */
152     @Override
153     public String toString() {
154         return super.toString() + "(nameIndex = " + nameIndex + ", signatureIndex = " + signatureIndex + ")";
155     }
156 }