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 * 017 */ 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025 026/** 027 * This class is derived from the abstract {@link Constant} 028 * and represents a reference to the name and signature 029 * of a field or method. 030 * 031 * @see Constant 032 */ 033public final class ConstantNameAndType extends Constant { 034 035 private int name_index; // Name of field/method 036 private int signature_index; // and its signature. 037 038 039 /** 040 * Initialize from another object. 041 */ 042 public ConstantNameAndType(final ConstantNameAndType c) { 043 this(c.getNameIndex(), c.getSignatureIndex()); 044 } 045 046 047 /** 048 * Initialize instance from file data. 049 * 050 * @param file Input stream 051 * @throws IOException 052 */ 053 ConstantNameAndType(final DataInput file) throws IOException { 054 this(file.readUnsignedShort(), file.readUnsignedShort()); 055 } 056 057 058 /** 059 * @param name_index Name of field/method 060 * @param signature_index and its signature 061 */ 062 public ConstantNameAndType(final int name_index, final int signature_index) { 063 super(Const.CONSTANT_NameAndType); 064 this.name_index = name_index; 065 this.signature_index = signature_index; 066 } 067 068 069 /** 070 * Called by objects that are traversing the nodes of the tree implicitely 071 * defined by the contents of a Java class. I.e., the hierarchy of methods, 072 * fields, attributes, etc. spawns a tree of objects. 073 * 074 * @param v Visitor object 075 */ 076 @Override 077 public void accept( final Visitor v ) { 078 v.visitConstantNameAndType(this); 079 } 080 081 082 /** 083 * Dump name and signature index to file stream in binary format. 084 * 085 * @param file Output file stream 086 * @throws IOException 087 */ 088 @Override 089 public void dump( final DataOutputStream file ) throws IOException { 090 file.writeByte(super.getTag()); 091 file.writeShort(name_index); 092 file.writeShort(signature_index); 093 } 094 095 096 /** 097 * @return Name index in constant pool of field/method name. 098 */ 099 public int getNameIndex() { 100 return name_index; 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 signature_index; 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 name_index the name index of this constant 128 */ 129 public void setNameIndex( final int name_index ) { 130 this.name_index = name_index; 131 } 132 133 134 /** 135 * @param signature_index the signature index in the constant pool of this type 136 */ 137 public void setSignatureIndex( final int signature_index ) { 138 this.signature_index = signature_index; 139 } 140 141 142 /** 143 * @return String representation 144 */ 145 @Override 146 public String toString() { 147 return super.toString() + "(name_index = " + name_index + ", signature_index = " 148 + signature_index + ")"; 149 } 150}