INVOKEINTERFACE.java

  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. import java.io.DataOutputStream;
  19. import java.io.IOException;

  20. import org.apache.bcel.Const;
  21. import org.apache.bcel.ExceptionConst;
  22. import org.apache.bcel.classfile.ConstantPool;
  23. import org.apache.bcel.util.ByteSequence;

  24. /**
  25.  * INVOKEINTERFACE - Invoke interface method
  26.  *
  27.  * <PRE>
  28.  * Stack: ..., objectref, [arg1, [arg2 ...]] -&gt; ...
  29.  * </PRE>
  30.  *
  31.  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface"> The
  32.  *      invokeinterface instruction in The Java Virtual Machine Specification</a>
  33.  */
  34. public final class INVOKEINTERFACE extends InvokeInstruction {

  35.     private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2

  36.     /**
  37.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  38.      */
  39.     INVOKEINTERFACE() {
  40.     }

  41.     public INVOKEINTERFACE(final int index, final int nargs) {
  42.         super(Const.INVOKEINTERFACE, index);
  43.         super.setLength(5);
  44.         if (nargs < 1) {
  45.             throw new ClassGenException("Number of arguments must be > 0 " + nargs);
  46.         }
  47.         this.nargs = nargs;
  48.     }

  49.     /**
  50.      * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
  51.      * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
  52.      *
  53.      * @param v Visitor object
  54.      */
  55.     @Override
  56.     public void accept(final Visitor v) {
  57.         v.visitExceptionThrower(this);
  58.         v.visitTypedInstruction(this);
  59.         v.visitStackConsumer(this);
  60.         v.visitStackProducer(this);
  61.         v.visitLoadClass(this);
  62.         v.visitCPInstruction(this);
  63.         v.visitFieldOrMethod(this);
  64.         v.visitInvokeInstruction(this);
  65.         v.visitINVOKEINTERFACE(this);
  66.     }

  67.     @Override
  68.     public int consumeStack(final ConstantPoolGen cpg) { // nargs is given in byte-code
  69.         return nargs; // nargs includes this reference
  70.     }

  71.     /**
  72.      * Dump instruction as byte code to stream out.
  73.      *
  74.      * @param out Output stream
  75.      */
  76.     @Override
  77.     public void dump(final DataOutputStream out) throws IOException {
  78.         out.writeByte(super.getOpcode());
  79.         out.writeShort(super.getIndex());
  80.         out.writeByte(nargs);
  81.         out.writeByte(0);
  82.     }

  83.     /**
  84.      * The <B>count</B> argument according to the Java Language Specification, Second Edition.
  85.      */
  86.     public int getCount() {
  87.         return nargs;
  88.     }

  89.     @Override
  90.     public Class<?>[] getExceptions() {
  91.         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
  92.             ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
  93.     }

  94.     /**
  95.      * Read needed data (i.e., index) from file.
  96.      */
  97.     @Override
  98.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  99.         super.initFromFile(bytes, wide);
  100.         super.setLength(5);
  101.         nargs = bytes.readUnsignedByte();
  102.         bytes.readByte(); // Skip 0 byte
  103.     }

  104.     /**
  105.      * @return mnemonic for instruction with symbolic references resolved
  106.      */
  107.     @Override
  108.     public String toString(final ConstantPool cp) {
  109.         return super.toString(cp) + " " + nargs;
  110.     }
  111. }