    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     * @param index to constant pool
    036     */
    037    protected FieldInstruction(final short opcode, final int index) {
    038        super(opcode, index);
    039    }
    040
    041    /**
    042     * @return name of referenced field.
    043     */
    044    public String getFieldName(final ConstantPoolGen cpg) {
    045        return getName(cpg);
    046    }
    047
    048    /**
    049     * @return size of field (1 or 2)
    050     */
    051    protected int getFieldSize(final ConstantPoolGen cpg) {
    052        return Type.size(Type.getTypeSize(getSignature(cpg)));
    053    }
    054
    055    /**
    056     * @return type of field
    057     */
    058    public Type getFieldType(final ConstantPoolGen cpg) {
    059        return Type.getType(getSignature(cpg));
    060    }
    061
    062    /**
    063     * @return return type of referenced field
    064     */
    065    @Override
    066    public Type getType(final ConstantPoolGen cpg) {
    067        return getFieldType(cpg);
    068    }
    069
    070    /**
    071     * @return mnemonic for instruction with symbolic references resolved
    072     */
    073    @Override
    074    public String toString(final ConstantPool cp) {
    075        return org.apache.bcel.Const.getOpcodeName(super.getOpcode()) + " " + cp.constantToString(super.getIndex(), org.apache.bcel.Const.CONSTANT_Fieldref);
    076    }
    077}