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 * @since 6.0
030 */
031public abstract class NameSignatureInstruction extends CPInstruction {
032
033    public NameSignatureInstruction() {
034        super();
035    }
036
037    public NameSignatureInstruction(final short opcode, final int index) {
038        super(opcode, index);
039    }
040
041    public ConstantNameAndType getNameAndType(final ConstantPoolGen cpg) {
042        final ConstantPool cp = cpg.getConstantPool();
043        final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
044        return  (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
045    }
046    /** @return signature of referenced method/field.
047     */
048    public String getSignature(final ConstantPoolGen cpg) {
049        final ConstantPool cp = cpg.getConstantPool();
050        final ConstantNameAndType cnat = getNameAndType(cpg);
051        return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
052    }
053
054    /** @return name of referenced method/field.
055     */
056    public String getName(final ConstantPoolGen cpg) {
057        final ConstantPool cp = cpg.getConstantPool();
058        final ConstantNameAndType cnat = getNameAndType(cpg);
059        return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
060    }
061
062}