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  package org.apache.bcel.generic;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.bcel.Const;
23  import org.apache.bcel.ExceptionConst;
24  import org.apache.bcel.classfile.ConstantInvokeDynamic;
25  import org.apache.bcel.classfile.ConstantNameAndType;
26  import org.apache.bcel.classfile.ConstantPool;
27  import org.apache.bcel.util.ByteSequence;
28  
29  /**
30   * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
31   * of the method. Ignores the bootstrap mechanism entirely.
32   *
33   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
34   *      invokedynamic instruction in The Java Virtual Machine Specification</a>
35   * @since 6.0
36   */
37  public class INVOKEDYNAMIC extends InvokeInstruction {
38  
39      /**
40       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
41       */
42      INVOKEDYNAMIC() {
43      }
44  
45      public INVOKEDYNAMIC(final int index) {
46          super(Const.INVOKEDYNAMIC, index);
47      }
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.visitINVOKEDYNAMIC(this);
66      }
67  
68      /**
69       * Dump instruction as byte code to stream out.
70       *
71       * @param out Output stream
72       */
73      @Override
74      public void dump(final DataOutputStream out) throws IOException {
75          out.writeByte(super.getOpcode());
76          out.writeShort(super.getIndex());
77          out.writeByte(0);
78          out.writeByte(0);
79      }
80  
81      /**
82       * Override the parent method because our class name is held elsewhere.
83       *
84       * Note: Contrary to this method's name it does not return the class name of the invoke target; rather it returns the
85       * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
86       */
87      @Override
88      public String getClassName(final ConstantPoolGen cpg) {
89          final ConstantPool cp = cpg.getConstantPool();
90          final ConstantInvokeDynamic cid = cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
91          return cp.getConstant(cid.getNameAndTypeIndex(), ConstantNameAndType.class).getName(cp);
92      }
93  
94      @Override
95      public Class<?>[] getExceptions() {
96          return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
97              ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
98      }
99  
100     /**
101      * Since InvokeDynamic doesn't refer to a reference type, just return {@link Object}, as that is the only type we can
102      * say for sure the reference will be.
103      *
104      * @param cpg the ConstantPoolGen used to create the instruction
105      * @return an ObjectType for {@link Object}
106      * @since 6.1
107      */
108     @Override
109     public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
110         return new ObjectType(Object.class.getName());
111     }
112 
113     /**
114      * Read needed data (i.e., index) from file.
115      */
116     @Override
117     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
118         super.initFromFile(bytes, wide);
119         super.setLength(5);
120         bytes.readByte(); // Skip 0 byte
121         bytes.readByte(); // Skip 0 byte
122     }
123 
124 }