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     */
018    package org.apache.bcel.classfile;
019    
020    import java.io.DataInput;
021    import java.io.DataOutputStream;
022    import java.io.IOException;
023    
024    import org.apache.bcel.Constants;
025    
026    /** 
027     * Abstract super class for Fieldref and Methodref constants.
028     *
029     * @version $Id: ConstantCP.java 1152072 2011-07-29 01:54:05Z dbrosius $
030     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
031     * @see     ConstantFieldref
032     * @see     ConstantMethodref
033     * @see     ConstantInterfaceMethodref
034     */
035    public abstract class ConstantCP extends Constant {
036    
037        private static final long serialVersionUID = -6275762995206209402L;
038        /** References to the constants containing the class and the field signature
039         */
040        protected int class_index, name_and_type_index;
041    
042    
043        /**
044         * Initialize from another object.
045         */
046        public ConstantCP(ConstantCP c) {
047            this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
048        }
049    
050    
051        /**
052         * Initialize instance from file data.
053         *
054         * @param tag  Constant type tag
055         * @param file Input stream
056         * @throws IOException
057         */
058        ConstantCP(byte tag, DataInput file) throws IOException {
059            this(tag, file.readUnsignedShort(), file.readUnsignedShort());
060        }
061    
062    
063        /**
064         * @param class_index Reference to the class containing the field
065         * @param name_and_type_index and the field signature
066         */
067        protected ConstantCP(byte tag, int class_index, int name_and_type_index) {
068            super(tag);
069            this.class_index = class_index;
070            this.name_and_type_index = name_and_type_index;
071        }
072    
073    
074        /** 
075         * Dump constant field reference to file stream in binary format.
076         *
077         * @param file Output file stream
078         * @throws IOException
079         */
080        @Override
081        public final void dump( DataOutputStream file ) throws IOException {
082            file.writeByte(tag);
083            file.writeShort(class_index);
084            file.writeShort(name_and_type_index);
085        }
086    
087    
088        /**
089         * @return Reference (index) to class this field or method belongs to.
090         */
091        public final int getClassIndex() {
092            return class_index;
093        }
094    
095    
096        /**
097         * @return Reference (index) to signature of the field.
098         */
099        public final int getNameAndTypeIndex() {
100            return name_and_type_index;
101        }
102    
103    
104        /**
105         * @param class_index points to Constant_class 
106         */
107        public final void setClassIndex( int class_index ) {
108            this.class_index = class_index;
109        }
110    
111    
112        /**
113         * @return Class this field belongs to.
114         */
115        public String getClass( ConstantPool cp ) {
116            return cp.constantToString(class_index, Constants.CONSTANT_Class);
117        }
118    
119    
120        /**
121         * @param name_and_type_index points to Constant_NameAndType
122         */
123        public final void setNameAndTypeIndex( int name_and_type_index ) {
124            this.name_and_type_index = name_and_type_index;
125        }
126    
127    
128        /**
129         * @return String representation.
130         */
131        @Override
132        public final String toString() {
133            return super.toString() + "(class_index = " + class_index + ", name_and_type_index = "
134                    + name_and_type_index + ")";
135        }
136    }