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 
26 /**
27  * INVOKEVIRTUAL - Invoke instance method; dispatch based on class
28  *
29  * <PRE>Stack: ..., objectref, [arg1, [arg2 ...]] -&gt; ...</PRE>
30  *
31  * @version $Id$
32  * @see
33  * <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokevirtual">
34  * The invokevirtual instruction in The Java Virtual Machine Specification</a>
35  */
36 public class INVOKEVIRTUAL extends InvokeInstruction {
37 
38     /**
39      * Empty constructor needed for Instruction.readInstruction.
40      * Not to be used otherwise.
41      */
INVOKEVIRTUAL()42     INVOKEVIRTUAL() {
43     }
44 
45 
INVOKEVIRTUAL(final int index)46     public INVOKEVIRTUAL(final int index) {
47         super(Const.INVOKEVIRTUAL, index);
48     }
49 
50 
51     /**
52      * Dump instruction as byte code to stream out.
53      * @param out Output stream
54      */
55     @Override
dump( final DataOutputStream out )56     public void dump( final DataOutputStream out ) throws IOException {
57         out.writeByte(super.getOpcode());
58         out.writeShort(super.getIndex());
59     }
60 
61     @Override
getExceptions()62     public Class<?>[] getExceptions() {
63         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_FIELD_AND_METHOD_RESOLUTION,
64             ExceptionConst.NULL_POINTER_EXCEPTION,
65             ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR,
66             ExceptionConst.ABSTRACT_METHOD_ERROR,
67             ExceptionConst.UNSATISFIED_LINK_ERROR);
68     }
69 
70 
71     /**
72      * Call corresponding visitor method(s). The order is:
73      * Call visitor methods of implemented interfaces first, then
74      * call methods according to the class hierarchy in descending order,
75      * i.e., the most specific visitXXX() call comes last.
76      *
77      * @param v Visitor object
78      */
79     @Override
accept( final Visitor v )80     public void accept( final Visitor v ) {
81         v.visitExceptionThrower(this);
82         v.visitTypedInstruction(this);
83         v.visitStackConsumer(this);
84         v.visitStackProducer(this);
85         v.visitLoadClass(this);
86         v.visitCPInstruction(this);
87         v.visitFieldOrMethod(this);
88         v.visitInvokeInstruction(this);
89         v.visitINVOKEVIRTUAL(this);
90     }
91 }
92