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