View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  import org.apache.bcel.ExceptionConst;
26  import org.apache.bcel.classfile.ConstantInvokeDynamic;
27  import org.apache.bcel.classfile.ConstantNameAndType;
28  import org.apache.bcel.classfile.ConstantPool;
29  import org.apache.bcel.util.ByteSequence;
30  
31  /**
32   * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
33   * of the method. Ignores the bootstrap mechanism entirely.
34   *
35   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
36   *      invokedynamic instruction in The Java Virtual Machine Specification</a>
37   * @since 6.0
38   */
39  public class INVOKEDYNAMIC extends InvokeInstruction {
40  
41      /**
42       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
43       */
44      INVOKEDYNAMIC() {
45      }
46  
47      /**
48       * Constructs an INVOKEDYNAMIC instruction.
49       *
50       * @param index index into constant pool.
51       */
52      public INVOKEDYNAMIC(final int index) {
53          super(Const.INVOKEDYNAMIC, index);
54          super.setLength(5);
55      }
56  
57      /**
58       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
59       * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
60       *
61       * @param v Visitor object.
62       */
63      @Override
64      public void accept(final Visitor v) {
65          v.visitExceptionThrower(this);
66          v.visitTypedInstruction(this);
67          v.visitStackConsumer(this);
68          v.visitStackProducer(this);
69          v.visitLoadClass(this);
70          v.visitCPInstruction(this);
71          v.visitFieldOrMethod(this);
72          v.visitInvokeInstruction(this);
73          v.visitINVOKEDYNAMIC(this);
74      }
75  
76      /**
77       * Dumps instruction as byte code to stream out.
78       *
79       * @param out Output stream.
80       */
81      @Override
82      public void dump(final DataOutputStream out) throws IOException {
83          out.writeByte(super.getOpcode());
84          out.writeShort(super.getIndex());
85          out.writeByte(0);
86          out.writeByte(0);
87      }
88  
89      /**
90       * Gets the name of the method used to invoke the generated lambda method.
91       *
92       * Override the parent method because our class name is held elsewhere.
93       *
94       * Note: Contrary to this method's name it does not return the class name of the invoke target; rather it returns the
95       * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
96       */
97      @Override
98      public String getClassName(final ConstantPoolGen cpg) {
99          final ConstantPool cp = cpg.getConstantPool();
100         final ConstantInvokeDynamic cid = cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
101         return cp.getConstant(cid.getNameAndTypeIndex(), ConstantNameAndType.class).getName(cp);
102     }
103 
104     @Override
105     public Class<?>[] getExceptions() {
106         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
107             ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
108     }
109 
110     /**
111      * Gets the reference type, which is {@link Object} for this instruction.
112      * <p>
113      * Since InvokeDynamic doesn't refer to a reference type, just return {@link Object}, as that is the only type we can
114      * say for sure the reference will be.
115      * </p>
116      *
117      * @param cpg The ConstantPoolGen used to create the instruction.
118      * @return An ObjectType for {@link Object}.
119      * @since 6.1
120      */
121     @Override
122     public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
123         return new ObjectType(Object.class.getName());
124     }
125 
126     /**
127      * Reads needed data (that is, index) from file.
128      */
129     @Override
130     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
131         super.initFromFile(bytes, wide);
132         super.setLength(5);
133         bytes.readByte(); // Skip 0 byte
134         bytes.readByte(); // Skip 0 byte
135     }
136 
137 }