    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 */
    019
    020package org.apache.bcel.generic;
    021
    022import org.apache.bcel.classfile.ConstantCP;
    023import org.apache.bcel.classfile.ConstantNameAndType;
    024import org.apache.bcel.classfile.ConstantPool;
    025import org.apache.bcel.classfile.ConstantUtf8;
    026
    027/**
    028 * Super class for FieldOrMethod and INVOKEDYNAMIC, since they both have names and signatures
    029 *
    030 * @since 6.0
    031 */
    032public abstract class NameSignatureInstruction extends CPInstruction {
    033
    034    public NameSignatureInstruction() {
    035    }
    036
    037    public NameSignatureInstruction(final short opcode, final int index) {
    038        super(opcode, index);
    039    }
    040
    041    /**
    042     * @return name of referenced method/field.
    043     */
    044    public String getName(final ConstantPoolGen cpg) {
    045        final ConstantPool cp = cpg.getConstantPool();
    046        final ConstantNameAndType cnat = getNameAndType(cpg);
    047        return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
    048    }
    049
    050    public ConstantNameAndType getNameAndType(final ConstantPoolGen cpg) {
    051        final ConstantPool cp = cpg.getConstantPool();
    052        final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    053        return (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
    054    }
    055
    056    /**
    057     * @return signature of referenced method/field.
    058     */
    059    public String getSignature(final ConstantPoolGen cpg) {
    060        final ConstantPool cp = cpg.getConstantPool();
    061        final ConstantNameAndType cnat = getNameAndType(cpg);
    062        return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
    063    }
    064
    065}