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 * Abstract super class for Fieldref, Methodref, InterfaceMethodref and 028 * InvokeDynamic constants. 029 * 030 * @version $Id: ConstantCP.java 1747278 2016-06-07 17:28:43Z britter $ 031 * @see ConstantFieldref 032 * @see ConstantMethodref 033 * @see ConstantInterfaceMethodref 034 * @see ConstantInvokeDynamic 035 */ 036public abstract class ConstantCP extends Constant { 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 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 044 */ 045 @java.lang.Deprecated 046 protected int class_index; // TODO make private (has getter & setter) 047 // This field has the same meaning for all subclasses. 048 049 /** 050 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 051 */ 052 @java.lang.Deprecated 053 protected int name_and_type_index; // TODO make private (has getter & setter) 054 055 056 /** 057 * Initialize from another object. 058 */ 059 public ConstantCP(final ConstantCP c) { 060 this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex()); 061 } 062 063 064 /** 065 * Initialize instance from file data. 066 * 067 * @param tag Constant type tag 068 * @param file Input stream 069 * @throws IOException 070 */ 071 ConstantCP(final byte tag, final DataInput file) throws IOException { 072 this(tag, file.readUnsignedShort(), file.readUnsignedShort()); 073 } 074 075 076 /** 077 * @param class_index Reference to the class containing the field 078 * @param name_and_type_index and the field signature 079 */ 080 protected ConstantCP(final byte tag, final int class_index, final int name_and_type_index) { 081 super(tag); 082 this.class_index = class_index; 083 this.name_and_type_index = name_and_type_index; 084 } 085 086 087 /** 088 * Dump constant field reference to file stream in binary format. 089 * 090 * @param file Output file stream 091 * @throws IOException 092 */ 093 @Override 094 public final void dump( final DataOutputStream file ) throws IOException { 095 file.writeByte(super.getTag()); 096 file.writeShort(class_index); 097 file.writeShort(name_and_type_index); 098 } 099 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}