1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.generic;
  23 
  24 import java.io.DataOutputStream;
  25 import java.io.IOException;
  26 
  27 import com.sun.org.apache.bcel.internal.Const;
  28 import com.sun.org.apache.bcel.internal.ExceptionConst;
  29 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
  30 import com.sun.org.apache.bcel.internal.util.ByteSequence;
  31 
  32 /**
  33  * INVOKEINTERFACE - Invoke interface method
  34  * <PRE>Stack: ..., objectref, [arg1, [arg2 ...]] -&gt; ...</PRE>
  35  *
  36  * @see
  37  * <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface">
  38  * The invokeinterface instruction in The Java Virtual Machine Specification</a>
  39  */
  40 public final class INVOKEINTERFACE extends InvokeInstruction {
  41 
  42     private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2
  43 
  44 
  45     /**
  46      * Empty constructor needed for Instruction.readInstruction.
  47      * Not to be used otherwise.
  48      */
  49     INVOKEINTERFACE() {
  50     }
  51 
  52 
  53     public INVOKEINTERFACE(final int index, final int nargs) {
  54         super(Const.INVOKEINTERFACE, index);
  55         super.setLength(5);
  56         if (nargs < 1) {
  57             throw new ClassGenException("Number of arguments must be > 0 " + nargs);
  58         }
  59         this.nargs = nargs;
  60     }
  61 
  62 
  63     /**
  64      * Dump instruction as byte code to stream out.
  65      * @param out Output stream
  66      */
  67     @Override
  68     public void dump( final DataOutputStream out ) throws IOException {
  69         out.writeByte(super.getOpcode());
  70         out.writeShort(super.getIndex());
  71         out.writeByte(nargs);
  72         out.writeByte(0);
  73     }
  74 
  75 
  76     /**
  77      * The <B>count</B> argument according to the Java Language Specification,
  78      * Second Edition.
  79      */
  80     public int getCount() {
  81         return nargs;
  82     }
  83 
  84 
  85     /**
  86      * Read needed data (i.e., index) from file.
  87      */
  88     @Override
  89     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
  90         super.initFromFile(bytes, wide);
  91         super.setLength(5);
  92         nargs = bytes.readUnsignedByte();
  93         bytes.readByte(); // Skip 0 byte
  94     }
  95 
  96 
  97     /**
  98      * @return mnemonic for instruction with symbolic references resolved
  99      */
 100     @Override
 101     public String toString( final ConstantPool cp ) {
 102         return super.toString(cp) + " " + nargs;
 103     }
 104 
 105 
 106     @Override
 107     public int consumeStack( final ConstantPoolGen cpg ) { // nargs is given in byte-code
 108         return nargs; // nargs includes this reference
 109     }
 110 
 111 
 112     @Override
 113     public Class<?>[] getExceptions() {
 114         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION,
 115             ExceptionConst.UNSATISFIED_LINK_ERROR,
 116             ExceptionConst.ABSTRACT_METHOD_ERROR,
 117             ExceptionConst.ILLEGAL_ACCESS_ERROR,
 118             ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
 119     }
 120 
 121 
 122     /**
 123      * Call corresponding visitor method(s). The order is:
 124      * Call visitor methods of implemented interfaces first, then
 125      * call methods according to the class hierarchy in descending order,
 126      * i.e., the most specific visitXXX() call comes last.
 127      *
 128      * @param v Visitor object
 129      */
 130     @Override
 131     public void accept( final Visitor v ) {
 132         v.visitExceptionThrower(this);
 133         v.visitTypedInstruction(this);
 134         v.visitStackConsumer(this);
 135         v.visitStackProducer(this);
 136         v.visitLoadClass(this);
 137         v.visitCPInstruction(this);
 138         v.visitFieldOrMethod(this);
 139         v.visitInvokeInstruction(this);
 140         v.visitINVOKEINTERFACE(this);
 141     }
 142 }