1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.*; 25 import com.sun.org.apache.bcel.internal.Constants; 26 27 /** 28 * Abstract super class for Fieldref and Methodref constants. 29 * 30 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 31 * @see ConstantFieldref 32 * @see ConstantMethodref 33 * @see ConstantInterfaceMethodref 34 */ 35 public abstract class ConstantCP extends Constant { 36 /** References to the constants containing the class and the field signature 37 */ 38 protected int class_index, name_and_type_index; 39 40 /** 41 * Initialize from another object. 42 */ 43 public ConstantCP(ConstantCP c) { 44 this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex()); 45 } 46 47 /** 48 * Initialize instance from file data. 49 * 50 * @param tag Constant type tag 51 * @param file Input stream 52 * @throws IOException 53 */ 54 ConstantCP(byte tag, DataInputStream file) throws IOException 55 { 56 this(tag, file.readUnsignedShort(), file.readUnsignedShort()); 57 } 58 59 /** 60 * @param class_index Reference to the class containing the field 61 * @param name_and_type_index and the field signature 62 */ 63 protected ConstantCP(byte tag, int class_index, 64 int name_and_type_index) { 65 super(tag); 66 this.class_index = class_index; 67 this.name_and_type_index = name_and_type_index; 68 } 69 70 /** 71 * Dump constant field reference to file stream in binary format. 72 * 73 * @param file Output file stream 74 * @throws IOException 75 */ 76 public final void dump(DataOutputStream file) throws IOException 77 { 78 file.writeByte(tag); 79 file.writeShort(class_index); 80 file.writeShort(name_and_type_index); 81 } 82 83 /** 84 * @return Reference (index) to class this field or method belongs to. 85 */ 86 public final int getClassIndex() { return class_index; } 87 88 /** 89 * @return Reference (index) to signature of the field. 90 */ 91 public final int getNameAndTypeIndex() { return name_and_type_index; } 92 93 /** 94 * @param class_index points to Constant_class 95 */ 96 public final void setClassIndex(int class_index) { 97 this.class_index = class_index; 98 } 99 100 /** 101 * @return Class this field belongs to. 102 */ 103 public String getClass(ConstantPool cp) { 104 return cp.constantToString(class_index, Constants.CONSTANT_Class); 105 } 106 107 /** 108 * @param name_and_type_index points to Constant_NameAndType 109 */ 110 public final void setNameAndTypeIndex(int name_and_type_index) { 111 this.name_and_type_index = name_and_type_index; 112 } 113 114 /** 115 * @return String representation. 116 */ 117 public final String toString() { 118 return super.toString() + "(class_index = " + class_index + 119 ", name_and_type_index = " + name_and_type_index + ")"; 120 } 121 }