1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 25 import com.sun.org.apache.bcel.internal.Constants; 26 import java.io.*; 27 28 /** 29 * This class is derived from the abstract 30 * <A HREF="com.sun.org.apache.bcel.internal.classfile.Constant.html">Constant</A> class 31 * and represents a reference to the name and signature 32 * of a field or method. 33 * 34 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 35 * @see Constant 36 */ 37 public final class ConstantNameAndType extends Constant { 38 private int name_index; // Name of field/method 39 private int signature_index; // and its signature. 40 41 /** 42 * Initialize from another object. 43 */ 44 public ConstantNameAndType(ConstantNameAndType c) { 45 this(c.getNameIndex(), c.getSignatureIndex()); 46 } 47 48 /** 49 * Initialize instance from file data. 50 * 51 * @param file Input stream 52 * @throws IOException 53 */ 54 ConstantNameAndType(DataInputStream file) throws IOException 55 { 56 this((int)file.readUnsignedShort(), (int)file.readUnsignedShort()); 57 } 58 59 /** 60 * @param name_index Name of field/method 61 * @param signature_index and its signature 62 */ 63 public ConstantNameAndType(int name_index, 64 int signature_index) 65 { 66 super(Constants.CONSTANT_NameAndType); 67 this.name_index = name_index; 68 this.signature_index = signature_index; 69 } 70 71 /** 72 * Called by objects that are traversing the nodes of the tree implicitely 73 * defined by the contents of a Java class. I.e., the hierarchy of methods, 74 * fields, attributes, etc. spawns a tree of objects. 75 * 76 * @param v Visitor object 77 */ 78 public void accept(Visitor v) { 79 v.visitConstantNameAndType(this); 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 public final void dump(DataOutputStream file) throws IOException 89 { 90 file.writeByte(tag); 91 file.writeShort(name_index); 92 file.writeShort(signature_index); 93 } 94 95 /** 96 * @return Name index in constant pool of field/method name. 97 */ 98 public final int getNameIndex() { return name_index; } 99 100 /** @return name 101 */ 102 public final String getName(ConstantPool cp) { 103 return cp.constantToString(getNameIndex(), Constants.CONSTANT_Utf8); 104 } 105 106 /** 107 * @return Index in constant pool of field/method signature. 108 */ 109 public final int getSignatureIndex() { return signature_index; } 110 111 /** @return signature 112 */ 113 public final String getSignature(ConstantPool cp) { 114 return cp.constantToString(getSignatureIndex(), Constants.CONSTANT_Utf8); 115 } 116 117 /** 118 * @param name_index. 119 */ 120 public final void setNameIndex(int name_index) { 121 this.name_index = name_index; 122 } 123 124 /** 125 * @param signature_index. 126 */ 127 public final void setSignatureIndex(int signature_index) { 128 this.signature_index = signature_index; 129 } 130 131 /** 132 * @return String representation 133 */ 134 public final String toString() { 135 return super.toString() + "(name_index = " + name_index + 136 ", signature_index = " + signature_index + ")"; 137 } 138 }