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