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 /** 035 * Constructs a NameSignatureInstruction. 036 */ 037 public NameSignatureInstruction() { 038 } 039 040 /** 041 * Constructs a NameSignatureInstruction. 042 * 043 * @param opcode The opcode. 044 * @param index index into constant pool. 045 */ 046 public NameSignatureInstruction(final short opcode, final int index) { 047 super(opcode, index); 048 } 049 050 /** 051 * Gets the name of the referenced method or field. 052 * 053 * @param cpg constant pool generator. 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 /** 063 * Gets the name and type constant. 064 * 065 * @param cpg constant pool generator. 066 * @return The name and type constant. 067 */ 068 public ConstantNameAndType getNameAndType(final ConstantPoolGen cpg) { 069 final ConstantPool cp = cpg.getConstantPool(); 070 final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex()); 071 return (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex()); 072 } 073 074 /** 075 * Gets the signature of the referenced method or field. 076 * 077 * @param cpg constant pool generator. 078 * @return signature of referenced method/field. 079 */ 080 public String getSignature(final ConstantPoolGen cpg) { 081 final ConstantPool cp = cpg.getConstantPool(); 082 final ConstantNameAndType cnat = getNameAndType(cpg); 083 return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes(); 084 } 085 086}