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 package org.apache.bcel.classfile; 18 19 import java.io.DataInput; 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 23 import org.apache.bcel.Const; 24 25 /** 26 * Abstract super class for Fieldref, Methodref, InterfaceMethodref and InvokeDynamic constants. 27 * 28 * @see ConstantFieldref 29 * @see ConstantMethodref 30 * @see ConstantInterfaceMethodref 31 * @see ConstantInvokeDynamic 32 */ 33 public abstract class ConstantCP extends Constant { 34 35 /** 36 * References to the constants containing the class and the field signature 37 */ 38 // Note that this field is used to store the 39 // bootstrap_method_attr_index of a ConstantInvokeDynamic. 40 /** 41 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 42 */ 43 @java.lang.Deprecated 44 protected int class_index; // TODO make private (has getter & setter) 45 // This field has the same meaning for all subclasses. 46 47 /** 48 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 49 */ 50 @java.lang.Deprecated 51 protected int name_and_type_index; // TODO make private (has getter & setter) 52 53 /** 54 * Initialize instance from file data. 55 * 56 * @param tag Constant type tag 57 * @param file Input stream 58 * @throws IOException if an I/O error occurs. 59 */ 60 ConstantCP(final byte tag, final DataInput file) throws IOException { 61 this(tag, file.readUnsignedShort(), file.readUnsignedShort()); 62 } 63 64 /** 65 * @param classIndex Reference to the class containing the field 66 * @param nameAndTypeIndex and the field signature 67 */ 68 protected ConstantCP(final byte tag, final int classIndex, final int nameAndTypeIndex) { 69 super(tag); 70 this.class_index = classIndex; 71 this.name_and_type_index = nameAndTypeIndex; 72 } 73 74 /** 75 * Initialize from another object. 76 * 77 * @param c Source to copy. 78 */ 79 public ConstantCP(final ConstantCP c) { 80 this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex()); 81 } 82 83 /** 84 * Dump constant field reference to file stream in binary format. 85 * 86 * @param file Output file stream 87 * @throws IOException if an I/O error occurs. 88 */ 89 @Override 90 public final void dump(final DataOutputStream file) throws IOException { 91 file.writeByte(super.getTag()); 92 file.writeShort(class_index); 93 file.writeShort(name_and_type_index); 94 } 95 96 /** 97 * @return Class this field belongs to. 98 */ 99 public String getClass(final ConstantPool cp) { 100 return cp.constantToString(class_index, Const.CONSTANT_Class); 101 } 102 103 /** 104 * @return Reference (index) to class this constant refers to. 105 */ 106 public final int getClassIndex() { 107 return class_index; 108 } 109 110 /** 111 * @return Reference (index) to signature of the field. 112 */ 113 public final int getNameAndTypeIndex() { 114 return name_and_type_index; 115 } 116 117 /** 118 * @param classIndex points to Constant_class 119 */ 120 public final void setClassIndex(final int classIndex) { 121 this.class_index = classIndex; 122 } 123 124 /** 125 * @param nameAndTypeIndex points to Constant_NameAndType 126 */ 127 public final void setNameAndTypeIndex(final int nameAndTypeIndex) { 128 this.name_and_type_index = nameAndTypeIndex; 129 } 130 131 /** 132 * @return String representation. 133 * 134 * not final as ConstantInvokeDynamic needs to modify 135 */ 136 @Override 137 public String toString() { 138 return super.toString() + "(class_index = " + class_index + ", name_and_type_index = " + name_and_type_index + ")"; 139 } 140 }