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.generic;
019    
020    import org.apache.bcel.classfile.ConstantPool;
021    
022    /**
023     * Super class for the GET/PUTxxx family of instructions.
024     *
025     * @version $Id: FieldInstruction.java 1152072 2011-07-29 01:54:05Z dbrosius $
026     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
027     */
028    public abstract class FieldInstruction extends FieldOrMethod {
029    
030        private static final long serialVersionUID = -7870956226459765817L;
031    
032    
033        /**
034         * Empty constructor needed for the Class.newInstance() statement in
035         * Instruction.readInstruction(). Not to be used otherwise.
036         */
037        FieldInstruction() {
038        }
039    
040    
041        /**
042         * @param index to constant pool
043         */
044        protected FieldInstruction(short opcode, int index) {
045            super(opcode, index);
046        }
047    
048    
049        /**
050         * @return mnemonic for instruction with symbolic references resolved
051         */
052        @Override
053        public String toString( ConstantPool cp ) {
054            return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " "
055                    + cp.constantToString(index, org.apache.bcel.Constants.CONSTANT_Fieldref);
056        }
057    
058    
059        /** @return size of field (1 or 2)
060         */
061        protected int getFieldSize( ConstantPoolGen cpg ) {
062            return Type.size(Type.getTypeSize(getSignature(cpg)));
063        }
064    
065    
066        /** @return return type of referenced field
067         */
068        @Override
069        public Type getType( ConstantPoolGen cpg ) {
070            return getFieldType(cpg);
071        }
072    
073    
074        /** @return type of field
075         */
076        public Type getFieldType( ConstantPoolGen cpg ) {
077            return Type.getType(getSignature(cpg));
078        }
079    
080    
081        /** @return name of referenced field.
082         */
083        public String getFieldName( ConstantPoolGen cpg ) {
084            return getName(cpg);
085        }
086    }