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