001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.bcel.classfile; 018 019import java.io.DataInput; 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.Const; 024 025/** 026 * This class is derived from the abstract {@link Constant} and represents a reference to the name and signature of a 027 * field or method. 028 * 029 * @see Constant 030 */ 031public final class ConstantNameAndType extends Constant { 032 033 private int nameIndex; // Name of field/method 034 private int signatureIndex; // and its signature. 035 036 /** 037 * Initialize from another object. 038 * 039 * @param c Source to copy. 040 */ 041 public ConstantNameAndType(final ConstantNameAndType c) { 042 this(c.getNameIndex(), c.getSignatureIndex()); 043 } 044 045 /** 046 * Initialize instance from file data. 047 * 048 * @param file Input stream 049 * @throws IOException if an I/O error occurs. 050 */ 051 ConstantNameAndType(final DataInput file) throws IOException { 052 this(file.readUnsignedShort(), file.readUnsignedShort()); 053 } 054 055 /** 056 * @param nameIndex Name of field/method 057 * @param signatureIndex and its signature 058 */ 059 public ConstantNameAndType(final int nameIndex, final int signatureIndex) { 060 super(Const.CONSTANT_NameAndType); 061 this.nameIndex = nameIndex; 062 this.signatureIndex = signatureIndex; 063 } 064 065 /** 066 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 067 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 068 * 069 * @param v Visitor object 070 */ 071 @Override 072 public void accept(final Visitor v) { 073 v.visitConstantNameAndType(this); 074 } 075 076 /** 077 * Dump name and signature index to file stream in binary format. 078 * 079 * @param file Output file stream 080 * @throws IOException if an I/O error occurs. 081 */ 082 @Override 083 public void dump(final DataOutputStream file) throws IOException { 084 file.writeByte(super.getTag()); 085 file.writeShort(nameIndex); 086 file.writeShort(signatureIndex); 087 } 088 089 /** 090 * @return name 091 */ 092 public String getName(final ConstantPool cp) { 093 return cp.constantToString(getNameIndex(), Const.CONSTANT_Utf8); 094 } 095 096 /** 097 * @return Name index in constant pool of field/method name. 098 */ 099 public int getNameIndex() { 100 return nameIndex; 101 } 102 103 /** 104 * @return signature 105 */ 106 public String getSignature(final ConstantPool cp) { 107 return cp.constantToString(getSignatureIndex(), Const.CONSTANT_Utf8); 108 } 109 110 /** 111 * @return Index in constant pool of field/method signature. 112 */ 113 public int getSignatureIndex() { 114 return signatureIndex; 115 } 116 117 /** 118 * @param nameIndex the name index of this constant 119 */ 120 public void setNameIndex(final int nameIndex) { 121 this.nameIndex = nameIndex; 122 } 123 124 /** 125 * @param signatureIndex the signature index in the constant pool of this type 126 */ 127 public void setSignatureIndex(final int signatureIndex) { 128 this.signatureIndex = signatureIndex; 129 } 130 131 /** 132 * @return String representation 133 */ 134 @Override 135 public String toString() { 136 return super.toString() + "(nameIndex = " + nameIndex + ", signatureIndex = " + signatureIndex + ")"; 137 } 138}