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