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