1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.Const;
25 import org.apache.bcel.ExceptionConst;
26 import org.apache.bcel.classfile.ConstantPool;
27 import org.apache.bcel.util.ByteSequence;
28
29 /**
30 * INVOKEINTERFACE - Invoke interface method
31 *
32 * <pre>
33 * Stack: ..., objectref, [arg1, [arg2 ...]] -> ...
34 * </pre>
35 *
36 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface"> The
37 * invokeinterface instruction in The Java Virtual Machine Specification</a>
38 */
39 public final class INVOKEINTERFACE extends InvokeInstruction {
40
41 private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2
42
43 /**
44 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
45 */
46 INVOKEINTERFACE() {
47 }
48
49 /**
50 * Constructs an INVOKEINTERFACE instruction.
51 *
52 * @param index index into constant pool.
53 * @param nargs number of arguments.
54 */
55 public INVOKEINTERFACE(final int index, final int nargs) {
56 super(Const.INVOKEINTERFACE, index);
57 super.setLength(5);
58 if (nargs < 1 || nargs > Const.MAX_BYTE) {
59 throw new ClassGenException("Number of arguments out of range (1 - " + Const.MAX_BYTE + "): " + nargs);
60 }
61 this.nargs = nargs;
62 }
63
64 /**
65 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
66 * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
67 *
68 * @param v Visitor object.
69 */
70 @Override
71 public void accept(final Visitor v) {
72 v.visitExceptionThrower(this);
73 v.visitTypedInstruction(this);
74 v.visitStackConsumer(this);
75 v.visitStackProducer(this);
76 v.visitLoadClass(this);
77 v.visitCPInstruction(this);
78 v.visitFieldOrMethod(this);
79 v.visitInvokeInstruction(this);
80 v.visitINVOKEINTERFACE(this);
81 }
82
83 @Override
84 public int consumeStack(final ConstantPoolGen cpg) { // nargs is given in byte-code
85 return nargs; // nargs includes this reference
86 }
87
88 /**
89 * Dumps instruction as byte code to stream out.
90 *
91 * @param out Output stream.
92 */
93 @Override
94 public void dump(final DataOutputStream out) throws IOException {
95 out.writeByte(super.getOpcode());
96 out.writeShort(super.getIndex());
97 out.writeByte(nargs);
98 out.writeByte(0);
99 }
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 }