View Javadoc
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   * @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      /**
40       * Initialize from another object.
41       */
42      public ConstantNameAndTypeNameAndType.html#ConstantNameAndType">ConstantNameAndType(final ConstantNameAndType c) {
43          this(c.getNameIndex(), c.getSignatureIndex());
44      }
45  
46  
47      /**
48       * Initialize instance from file data.
49       *
50       * @param file Input stream
51       * @throws IOException
52       */
53      ConstantNameAndType(final DataInput file) throws IOException {
54          this(file.readUnsignedShort(), file.readUnsignedShort());
55      }
56  
57  
58      /**
59       * @param nameIndex Name of field/method
60       * @param signatureIndex and its signature
61       */
62      public ConstantNameAndType(final int nameIndex, final int signatureIndex) {
63          super(Const.CONSTANT_NameAndType);
64          this.nameIndex = nameIndex;
65          this.signatureIndex = signatureIndex;
66      }
67  
68  
69      /**
70       * Called by objects that are traversing the nodes of the tree implicitely
71       * defined by the contents of a Java class. I.e., the hierarchy of methods,
72       * fields, attributes, etc. spawns a tree of objects.
73       *
74       * @param v Visitor object
75       */
76      @Override
77      public void accept( final Visitor v ) {
78          v.visitConstantNameAndType(this);
79      }
80  
81  
82      /**
83       * Dump name and signature index to file stream in binary format.
84       *
85       * @param file Output file stream
86       * @throws IOException
87       */
88      @Override
89      public void dump( final DataOutputStream file ) throws IOException {
90          file.writeByte(super.getTag());
91          file.writeShort(nameIndex);
92          file.writeShort(signatureIndex);
93      }
94  
95  
96      /**
97       * @return Name index in constant pool of field/method name.
98       */
99      public int getNameIndex() {
100         return nameIndex;
101     }
102 
103 
104     /** @return name
105      */
106     public String getName( final ConstantPool cp ) {
107         return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8);
108     }
109 
110 
111     /**
112      * @return Index in constant pool of field/method signature.
113      */
114     public int getSignatureIndex() {
115         return signatureIndex;
116     }
117 
118 
119     /** @return signature
120      */
121     public String getSignature( final ConstantPool cp ) {
122         return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8);
123     }
124 
125 
126     /**
127      * @param nameIndex the name index of this constant
128      */
129     public void setNameIndex( final int nameIndex ) {
130         this.nameIndex = nameIndex;
131     }
132 
133 
134     /**
135      * @param signatureIndex the signature index in the constant pool of this type
136      */
137     public void setSignatureIndex( final int signatureIndex ) {
138         this.signatureIndex = signatureIndex;
139     }
140 
141 
142     /**
143      * @return String representation
144      */
145     @Override
146     public String toString() {
147         return super.toString() + "(nameIndex = " + nameIndex + ", signatureIndex = "
148                 + signatureIndex + ")";
149     }
150 }