View Javadoc
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 ...]] -&gt; ...
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      public INVOKEINTERFACE(final int index, final int nargs) {
50          super(Const.INVOKEINTERFACE, index);
51          super.setLength(5);
52          if (nargs < 1) {
53              throw new ClassGenException("Number of arguments must be > 0 " + nargs);
54          }
55          this.nargs = nargs;
56      }
57  
58      /**
59       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
60       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
61       *
62       * @param v Visitor object
63       */
64      @Override
65      public void accept(final Visitor v) {
66          v.visitExceptionThrower(this);
67          v.visitTypedInstruction(this);
68          v.visitStackConsumer(this);
69          v.visitStackProducer(this);
70          v.visitLoadClass(this);
71          v.visitCPInstruction(this);
72          v.visitFieldOrMethod(this);
73          v.visitInvokeInstruction(this);
74          v.visitINVOKEINTERFACE(this);
75      }
76  
77      @Override
78      public int consumeStack(final ConstantPoolGen cpg) { // nargs is given in byte-code
79          return nargs; // nargs includes this reference
80      }
81  
82      /**
83       * Dump instruction as byte code to stream out.
84       *
85       * @param out Output stream
86       */
87      @Override
88      public void dump(final DataOutputStream out) throws IOException {
89          out.writeByte(super.getOpcode());
90          out.writeShort(super.getIndex());
91          out.writeByte(nargs);
92          out.writeByte(0);
93      }
94  
95      /**
96       * The <B>count</B> argument according to the Java Language Specification, Second Edition.
97       */
98      public int getCount() {
99          return nargs;
100     }
101 
102     @Override
103     public Class<?>[] getExceptions() {
104         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
105             ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
106     }
107 
108     /**
109      * Reads needed data (i.e., index) from file.
110      */
111     @Override
112     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
113         super.initFromFile(bytes, wide);
114         super.setLength(5);
115         nargs = bytes.readUnsignedByte();
116         bytes.readByte(); // Skip 0 byte
117     }
118 
119     /**
120      * @return mnemonic for instruction with symbolic references resolved
121      */
122     @Override
123     public String toString(final ConstantPool cp) {
124         return super.toString(cp) + " " + nargs;
125     }
126 }