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 */ 019package org.apache.bcel.generic; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025import org.apache.bcel.ExceptionConst; 026import org.apache.bcel.classfile.ConstantPool; 027import org.apache.bcel.util.ByteSequence; 028 029/** 030 * INVOKEINTERFACE - Invoke interface method 031 * 032 * <pre> 033 * Stack: ..., objectref, [arg1, [arg2 ...]] -> ... 034 * </pre> 035 * 036 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface"> The 037 * invokeinterface instruction in The Java Virtual Machine Specification</a> 038 */ 039public final class INVOKEINTERFACE extends InvokeInstruction { 040 041 private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2 042 043 /** 044 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise. 045 */ 046 INVOKEINTERFACE() { 047 } 048 049 /** 050 * Constructs an INVOKEINTERFACE instruction. 051 * 052 * @param index index into constant pool. 053 * @param nargs number of arguments. 054 */ 055 public INVOKEINTERFACE(final int index, final int nargs) { 056 super(Const.INVOKEINTERFACE, index); 057 super.setLength(5); 058 if (nargs < 1 || nargs > Const.MAX_BYTE) { 059 throw new ClassGenException("Number of arguments out of range (1 - " + Const.MAX_BYTE + "): " + nargs); 060 } 061 this.nargs = nargs; 062 } 063 064 /** 065 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call 066 * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last. 067 * 068 * @param v Visitor object. 069 */ 070 @Override 071 public void accept(final Visitor v) { 072 v.visitExceptionThrower(this); 073 v.visitTypedInstruction(this); 074 v.visitStackConsumer(this); 075 v.visitStackProducer(this); 076 v.visitLoadClass(this); 077 v.visitCPInstruction(this); 078 v.visitFieldOrMethod(this); 079 v.visitInvokeInstruction(this); 080 v.visitINVOKEINTERFACE(this); 081 } 082 083 @Override 084 public int consumeStack(final ConstantPoolGen cpg) { // nargs is given in byte-code 085 return nargs; // nargs includes this reference 086 } 087 088 /** 089 * Dumps instruction as byte code to stream out. 090 * 091 * @param out Output stream. 092 */ 093 @Override 094 public void dump(final DataOutputStream out) throws IOException { 095 out.writeByte(super.getOpcode()); 096 out.writeShort(super.getIndex()); 097 out.writeByte(nargs); 098 out.writeByte(0); 099 } 100 101 /** 102 * The <strong>count</strong> argument according to the Java Language Specification, Second Edition. 103 * 104 * @return The count argument. 105 */ 106 public int getCount() { 107 return nargs; 108 } 109 110 @Override 111 public Class<?>[] getExceptions() { 112 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR, 113 ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR); 114 } 115 116 /** 117 * Reads needed data (that is, index) from file. 118 */ 119 @Override 120 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { 121 super.initFromFile(bytes, wide); 122 super.setLength(5); 123 nargs = bytes.readUnsignedByte(); 124 bytes.readByte(); // Skip 0 byte 125 } 126 127 /** 128 * @return mnemonic for instruction with symbolic references resolved. 129 */ 130 @Override 131 public String toString(final ConstantPool cp) { 132 return super.toString(cp) + " " + nargs; 133 } 134}