001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.classfile; 020 021import java.io.DataInput; 022import java.io.DataOutputStream; 023import java.io.IOException; 024 025import org.apache.bcel.Const; 026 027/** 028 * Abstract super class for Fieldref, Methodref, InterfaceMethodref and InvokeDynamic constants. 029 * 030 * @see ConstantFieldref 031 * @see ConstantMethodref 032 * @see ConstantInterfaceMethodref 033 * @see ConstantInvokeDynamic 034 */ 035public abstract class ConstantCP extends Constant { 036 037 /** 038 * References to the constants containing the class and the field signature 039 */ 040 // Note that this field is used to store the 041 // bootstrap_method_attr_index of a ConstantInvokeDynamic. 042 043 /** 044 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter. 045 */ 046 @java.lang.Deprecated 047 protected int class_index; // TODO make private (has getter & setter) 048 // This field has the same meaning for all subclasses. 049 050 /** 051 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter. 052 */ 053 @java.lang.Deprecated 054 protected int name_and_type_index; // TODO make private (has getter & setter) 055 056 /** 057 * Initialize instance from file data. 058 * 059 * @param tag Constant type tag. 060 * @param file Input stream. 061 * @throws IOException if an I/O error occurs. 062 */ 063 ConstantCP(final byte tag, final DataInput file) throws IOException { 064 this(tag, file.readUnsignedShort(), file.readUnsignedShort()); 065 } 066 067 /** 068 * Constructs a ConstantCP. 069 * 070 * @param tag the constant type tag. 071 * @param classIndex Reference to the class containing the field. 072 * @param nameAndTypeIndex and the field signature. 073 */ 074 protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) { 075 super(tag); 076 this.class_index = classIndex; 077 this.name_and_type_index = nameAndTypeIndex; 078 } 079 080 /** 081 * Initialize from another object. 082 * 083 * @param c Source to copy. 084 */ 085 public ConstantCP(final ConstantCP c) { 086 this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex()); 087 } 088 089 /** 090 * Dumps constant field reference to file stream in binary format. 091 * 092 * @param file Output file stream. 093 * @throws IOException if an I/O error occurs. 094 */ 095 @Override 096 public final void dump(final DataOutputStream file) throws IOException { 097 file.writeByte(super.getTag()); 098 file.writeShort(class_index); 099 file.writeShort(name_and_type_index); 100 } 101 102 /** 103 * Gets the class this field belongs to. 104 * 105 * @param cp the constant pool. 106 * @return Class this field belongs to. 107 */ 108 public String getClass(final ConstantPool cp) { 109 return cp.constantToString(class_index, Const.CONSTANT_Class); 110 } 111 112 /** 113 * Gets the reference (index) to class this constant refers to. 114 * 115 * @return Reference (index) to class this constant refers to. 116 */ 117 public final int getClassIndex() { 118 return class_index; 119 } 120 121 /** 122 * Gets the reference (index) to signature of the field. 123 * 124 * @return Reference (index) to signature of the field. 125 */ 126 public final int getNameAndTypeIndex() { 127 return name_and_type_index; 128 } 129 130 /** 131 * Sets the class index. 132 * 133 * @param classIndex points to Constant_class. 134 */ 135 public final void setClassIndex(final int classIndex) { 136 this.class_index = classIndex; 137 } 138 139 /** 140 * Sets the name and type index. 141 * 142 * @param nameAndTypeIndex points to Constant_NameAndType. 143 */ 144 public final void setNameAndTypeIndex(final int nameAndTypeIndex) { 145 this.name_and_type_index = nameAndTypeIndex; 146 } 147 148 /** 149 * @return String representation. 150 * 151 * not final as ConstantInvokeDynamic needs to modify 152 */ 153 @Override 154 public String toString() { 155 return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")"; 156 } 157}