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   */
18  package org.apache.bcel.generic;
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.ExceptionConst;
25  import org.apache.bcel.classfile.ConstantPool;
26  import org.apache.bcel.util.ByteSequence;
27  
28  /**
29   * INVOKEINTERFACE - Invoke interface method
30   * <PRE>Stack: ..., objectref, [arg1, [arg2 ...]] -&gt; ...</PRE>
31   *
32   * @see
33   * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface">
34   * The invokeinterface instruction in The Java Virtual Machine Specification</a>
35   */
36  public final class INVOKEINTERFACE extends InvokeInstruction {
37  
38      private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2
39  
40  
41      /**
42       * Empty constructor needed for Instruction.readInstruction.
43       * Not to be used otherwise.
44       */
45      INVOKEINTERFACE() {
46      }
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      /**
60       * Dump instruction as byte code to stream out.
61       * @param out Output stream
62       */
63      @Override
64      public void dump( final DataOutputStream out ) throws IOException {
65          out.writeByte(super.getOpcode());
66          out.writeShort(super.getIndex());
67          out.writeByte(nargs);
68          out.writeByte(0);
69      }
70  
71  
72      /**
73       * The <B>count</B> argument according to the Java Language Specification,
74       * Second Edition.
75       */
76      public int getCount() {
77          return nargs;
78      }
79  
80  
81      /**
82       * Read needed data (i.e., index) from file.
83       */
84      @Override
85      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
86          super.initFromFile(bytes, wide);
87          super.setLength(5);
88          nargs = bytes.readUnsignedByte();
89          bytes.readByte(); // Skip 0 byte
90      }
91  
92  
93      /**
94       * @return mnemonic for instruction with symbolic references resolved
95       */
96      @Override
97      public String toString( final ConstantPool cp ) {
98          return super.toString(cp) + " " + nargs;
99      }
100 
101 
102     @Override
103     public int consumeStack( final ConstantPoolGen cpg ) { // nargs is given in byte-code
104         return nargs; // nargs includes this reference
105     }
106 
107 
108     @Override
109     public Class<?>[] getExceptions() {
110         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION,
111             ExceptionConst.UNSATISFIED_LINK_ERROR,
112             ExceptionConst.ABSTRACT_METHOD_ERROR,
113             ExceptionConst.ILLEGAL_ACCESS_ERROR,
114             ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
115     }
116 
117 
118     /**
119      * Call corresponding visitor method(s). The order is:
120      * Call visitor methods of implemented interfaces first, then
121      * call methods according to the class hierarchy in descending order,
122      * i.e., the most specific visitXXX() call comes last.
123      *
124      * @param v Visitor object
125      */
126     @Override
127     public void accept( final Visitor v ) {
128         v.visitExceptionThrower(this);
129         v.visitTypedInstruction(this);
130         v.visitStackConsumer(this);
131         v.visitStackProducer(this);
132         v.visitLoadClass(this);
133         v.visitCPInstruction(this);
134         v.visitFieldOrMethod(this);
135         v.visitInvokeInstruction(this);
136         v.visitINVOKEINTERFACE(this);
137     }
138 }