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