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