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.ConstantInvokeDynamic;
26  import org.apache.bcel.classfile.ConstantNameAndType;
27  import org.apache.bcel.classfile.ConstantPool;
28  import org.apache.bcel.util.ByteSequence;
29  
30  /**
31   * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class
32   * expects to be able to get the class of the method. Ignores the bootstrap
33   * mechanism entirely.
34   *
35   * @see
36   * <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic">
37   * The invokedynamic instruction in The Java Virtual Machine Specification</a>
38   * @since 6.0
39   */
40  public class INVOKEDYNAMIC extends InvokeInstruction {
41  
42      /**
43       * Empty constructor needed for Instruction.readInstruction.
44       * Not to be used otherwise.
45       */
46      INVOKEDYNAMIC() {
47      }
48  
49  
50      public INVOKEDYNAMIC(final int index) {
51          super(Const.INVOKEDYNAMIC, index);
52      }
53  
54  
55      /**
56       * Dump instruction as byte code to stream out.
57       * @param out Output stream
58       */
59      @Override
60      public void dump( final DataOutputStream out ) throws IOException {
61          out.writeByte(super.getOpcode());
62          out.writeShort(super.getIndex());
63          out.writeByte(0);
64          out.writeByte(0);
65         }
66  
67  
68      /**
69       * Read needed data (i.e., index) from file.
70       */
71      @Override
72      protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
73          super.initFromFile(bytes, wide);
74          super.setLength(5);
75          bytes.readByte(); // Skip 0 byte
76          bytes.readByte(); // Skip 0 byte
77      }
78  
79  
80      /**
81       * @return mnemonic for instruction with symbolic references resolved
82       */
83      @Override
84      public String toString( final ConstantPool cp ) {
85          return super.toString(cp);
86      }
87  
88  
89      @Override
90      public Class<?>[] getExceptions() {
91          return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION,
92              ExceptionConst.UNSATISFIED_LINK_ERROR,
93              ExceptionConst.ABSTRACT_METHOD_ERROR,
94              ExceptionConst.ILLEGAL_ACCESS_ERROR,
95              ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
96      }
97  
98  
99      /**
100      * Call corresponding visitor method(s). The order is:
101      * Call visitor methods of implemented interfaces first, then
102      * call methods according to the class hierarchy in descending order,
103      * i.e., the most specific visitXXX() call comes last.
104      *
105      * @param v Visitor object
106      */
107     @Override
108     public void accept( final Visitor v ) {
109         v.visitExceptionThrower(this);
110         v.visitTypedInstruction(this);
111         v.visitStackConsumer(this);
112         v.visitStackProducer(this);
113         v.visitLoadClass(this);
114         v.visitCPInstruction(this);
115         v.visitFieldOrMethod(this);
116         v.visitInvokeInstruction(this);
117         v.visitINVOKEDYNAMIC(this);
118     }
119 
120     /**
121      * Override the parent method because our classname is held elsewhere.
122      */
123     @Override
124     public String getClassName( final ConstantPoolGen cpg ) {
125         final ConstantPool cp = cpg.getConstantPool();
126         final ConstantInvokeDynamicorg/apache/bcel/classfile/ConstantInvokeDynamic.html#ConstantInvokeDynamic">ConstantInvokeDynamic cid = (ConstantInvokeDynamic) cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic);
127         return ((ConstantNameAndType) cp.getConstant(cid.getNameAndTypeIndex())).getName(cp);
128     }
129 
130 
131     /**
132      * Since InvokeDynamic doesn't refer to a reference type, just return java.lang.Object,
133      * as that is the only type we can say for sure the reference will be.
134      *
135      * @param cpg
136      *            the ConstantPoolGen used to create the instruction
137      * @return an ObjectType for java.lang.Object
138      * @since 6.1
139      */
140     @Override
141     public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
142         return new ObjectType(Object.class.getName());
143     }
144 }