001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import org.apache.bcel.classfile.ConstantPool;
022
023/**
024 * Super class for the GET/PUTxxx family of instructions.
025 */
026public abstract class FieldInstruction extends FieldOrMethod {
027
028    /**
029     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
030     */
031    FieldInstruction() {
032    }
033
034    /**
035     * Constructs a FieldInstruction.
036     *
037     * @param opcode the opcode.
038     * @param index to constant pool.
039     */
040    protected FieldInstruction(final short opcode, final int index) {
041        super(opcode, index);
042    }
043
044    /**
045     * Gets the name of referenced field.
046     *
047     * @param cpg the constant pool generator.
048     * @return name of referenced field.
049     */
050    public String getFieldName(final ConstantPoolGen cpg) {
051        return getName(cpg);
052    }
053
054    /**
055     * Gets the size of field.
056     *
057     * @param cpg the constant pool generator.
058     * @return size of field (1 or 2).
059     */
060    protected int getFieldSize(final ConstantPoolGen cpg) {
061        return Type.size(Type.getTypeSize(getSignature(cpg)));
062    }
063
064    /**
065     * Gets the type of field.
066     *
067     * @param cpg the constant pool generator.
068     * @return type of field.
069     */
070    public Type getFieldType(final ConstantPoolGen cpg) {
071        return Type.getType(getSignature(cpg));
072    }
073
074    /**
075     * @return return type of referenced field.
076     */
077    @Override
078    public Type getType(final ConstantPoolGen cpg) {
079        return getFieldType(cpg);
080    }
081
082    /**
083     * @return mnemonic for instruction with symbolic references resolved.
084     */
085    @Override
086    public String toString(final ConstantPool cp) {
087        return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + cp.constantToString(super.getIndex(), org.apache.bcel.Const.CONSTANT_Fieldref);
088    }
089}