• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @version $Id$
33  * @see
34  * <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokeinterface">
35  * The 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     /**
43      * Empty constructor needed for Instruction.readInstruction.
44      * Not to be used otherwise.
45      */
INVOKEINTERFACE()46     INVOKEINTERFACE() {
47     }
48 
49 
INVOKEINTERFACE(final int index, final int nargs)50     public INVOKEINTERFACE(final int index, final int nargs) {
51         super(Const.INVOKEINTERFACE, index);
52         super.setLength(5);
53         if (nargs < 1) {
54             throw new ClassGenException("Number of arguments must be > 0 " + nargs);
55         }
56         this.nargs = nargs;
57     }
58 
59 
60     /**
61      * Dump instruction as byte code to stream out.
62      * @param out Output stream
63      */
64     @Override
dump( final DataOutputStream out )65     public void dump( final DataOutputStream out ) throws IOException {
66         out.writeByte(super.getOpcode());
67         out.writeShort(super.getIndex());
68         out.writeByte(nargs);
69         out.writeByte(0);
70     }
71 
72 
73     /**
74      * The <B>count</B> argument according to the Java Language Specification,
75      * Second Edition.
76      */
getCount()77     public int getCount() {
78         return nargs;
79     }
80 
81 
82     /**
83      * Read needed data (i.e., index) from file.
84      */
85     @Override
initFromFile( final ByteSequence bytes, final boolean wide )86     protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
87         super.initFromFile(bytes, wide);
88         super.setLength(5);
89         nargs = bytes.readUnsignedByte();
90         bytes.readByte(); // Skip 0 byte
91     }
92 
93 
94     /**
95      * @return mnemonic for instruction with symbolic references resolved
96      */
97     @Override
toString( final ConstantPool cp )98     public String toString( final ConstantPool cp ) {
99         return super.toString(cp) + " " + nargs;
100     }
101 
102 
103     @Override
consumeStack( final ConstantPoolGen cpg )104     public int consumeStack( final ConstantPoolGen cpg ) { // nargs is given in byte-code
105         return nargs; // nargs includes this reference
106     }
107 
108 
109     @Override
getExceptions()110     public Class<?>[] getExceptions() {
111         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION,
112             ExceptionConst.UNSATISFIED_LINK_ERROR,
113             ExceptionConst.ABSTRACT_METHOD_ERROR,
114             ExceptionConst.ILLEGAL_ACCESS_ERROR,
115             ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
116     }
117 
118 
119     /**
120      * Call corresponding visitor method(s). The order is:
121      * Call visitor methods of implemented interfaces first, then
122      * call methods according to the class hierarchy in descending order,
123      * i.e., the most specific visitXXX() call comes last.
124      *
125      * @param v Visitor object
126      */
127     @Override
accept( final Visitor v )128     public void accept( final Visitor v ) {
129         v.visitExceptionThrower(this);
130         v.visitTypedInstruction(this);
131         v.visitStackConsumer(this);
132         v.visitStackProducer(this);
133         v.visitLoadClass(this);
134         v.visitCPInstruction(this);
135         v.visitFieldOrMethod(this);
136         v.visitInvokeInstruction(this);
137         v.visitINVOKEINTERFACE(this);
138     }
139 }
140