INVOKEDYNAMIC.java

  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. import java.io.DataOutputStream;
  19. import java.io.IOException;

  20. import org.apache.bcel.Const;
  21. import org.apache.bcel.ExceptionConst;
  22. import org.apache.bcel.classfile.ConstantInvokeDynamic;
  23. import org.apache.bcel.classfile.ConstantNameAndType;
  24. import org.apache.bcel.classfile.ConstantPool;
  25. import org.apache.bcel.util.ByteSequence;

  26. /**
  27.  * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
  28.  * of the method. Ignores the bootstrap mechanism entirely.
  29.  *
  30.  * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
  31.  *      invokedynamic instruction in The Java Virtual Machine Specification</a>
  32.  * @since 6.0
  33.  */
  34. public class INVOKEDYNAMIC extends InvokeInstruction {

  35.     /**
  36.      * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
  37.      */
  38.     INVOKEDYNAMIC() {
  39.     }

  40.     public INVOKEDYNAMIC(final int index) {
  41.         super(Const.INVOKEDYNAMIC, index);
  42.     }

  43.     /**
  44.      * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
  45.      * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
  46.      *
  47.      * @param v Visitor object
  48.      */
  49.     @Override
  50.     public void accept(final Visitor v) {
  51.         v.visitExceptionThrower(this);
  52.         v.visitTypedInstruction(this);
  53.         v.visitStackConsumer(this);
  54.         v.visitStackProducer(this);
  55.         v.visitLoadClass(this);
  56.         v.visitCPInstruction(this);
  57.         v.visitFieldOrMethod(this);
  58.         v.visitInvokeInstruction(this);
  59.         v.visitINVOKEDYNAMIC(this);
  60.     }

  61.     /**
  62.      * Dump instruction as byte code to stream out.
  63.      *
  64.      * @param out Output stream
  65.      */
  66.     @Override
  67.     public void dump(final DataOutputStream out) throws IOException {
  68.         out.writeByte(super.getOpcode());
  69.         out.writeShort(super.getIndex());
  70.         out.writeByte(0);
  71.         out.writeByte(0);
  72.     }

  73.     /**
  74.      * Override the parent method because our class name is held elsewhere.
  75.      *
  76.      * Note: Contrary to this method's name it does not return the class name of the invoke target; rather it returns the
  77.      * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
  78.      */
  79.     @Override
  80.     public String getClassName(final ConstantPoolGen cpg) {
  81.         final ConstantPool cp = cpg.getConstantPool();
  82.         final ConstantInvokeDynamic cid = cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
  83.         return cp.getConstant(cid.getNameAndTypeIndex(), ConstantNameAndType.class).getName(cp);
  84.     }

  85.     @Override
  86.     public Class<?>[] getExceptions() {
  87.         return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
  88.             ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
  89.     }

  90.     /**
  91.      * Since InvokeDynamic doesn't refer to a reference type, just return {@link Object}, as that is the only type we can
  92.      * say for sure the reference will be.
  93.      *
  94.      * @param cpg the ConstantPoolGen used to create the instruction
  95.      * @return an ObjectType for {@link Object}
  96.      * @since 6.1
  97.      */
  98.     @Override
  99.     public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
  100.         return new ObjectType(Object.class.getName());
  101.     }

  102.     /**
  103.      * Read needed data (i.e., index) from file.
  104.      */
  105.     @Override
  106.     protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
  107.         super.initFromFile(bytes, wide);
  108.         super.setLength(5);
  109.         bytes.readByte(); // Skip 0 byte
  110.         bytes.readByte(); // Skip 0 byte
  111.     }

  112. }