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 */
017package org.apache.bcel.generic;
018
019import org.apache.bcel.classfile.ConstantPool;
020
021/**
022 * Super class for the GET/PUTxxx family of instructions.
023 */
024public abstract class FieldInstruction extends FieldOrMethod {
025
026    /**
027     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
028     */
029    FieldInstruction() {
030    }
031
032    /**
033     * @param index to constant pool
034     */
035    protected FieldInstruction(final short opcode, final int index) {
036        super(opcode, index);
037    }
038
039    /**
040     * @return name of referenced field.
041     */
042    public String getFieldName(final ConstantPoolGen cpg) {
043        return getName(cpg);
044    }
045
046    /**
047     * @return size of field (1 or 2)
048     */
049    protected int getFieldSize(final ConstantPoolGen cpg) {
050        return Type.size(Type.getTypeSize(getSignature(cpg)));
051    }
052
053    /**
054     * @return type of field
055     */
056    public Type getFieldType(final ConstantPoolGen cpg) {
057        return Type.getType(getSignature(cpg));
058    }
059
060    /**
061     * @return return type of referenced field
062     */
063    @Override
064    public Type getType(final ConstantPoolGen cpg) {
065        return getFieldType(cpg);
066    }
067
068    /**
069     * @return mnemonic for instruction with symbolic references resolved
070     */
071    @Override
072    public String toString(final ConstantPool cp) {
073        return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + cp.constantToString(super.getIndex(), org.apache.bcel.Const.CONSTANT_Fieldref);
074    }
075}