    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.generic;
    019
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022
    023import org.apache.bcel.Const;
    024import org.apache.bcel.ExceptionConst;
    025import org.apache.bcel.classfile.ConstantInvokeDynamic;
    026import org.apache.bcel.classfile.ConstantNameAndType;
    027import org.apache.bcel.classfile.ConstantPool;
    028import org.apache.bcel.util.ByteSequence;
    029
    030/**
    031 * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
    032 * of the method. Ignores the bootstrap mechanism entirely.
    033 *
    034 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
    035 *      invokedynamic instruction in The Java Virtual Machine Specification</a>
    036 * @since 6.0
    037 */
    038public class INVOKEDYNAMIC extends InvokeInstruction {
    039
    040    /**
    041     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
    042     */
    043    INVOKEDYNAMIC() {
    044    }
    045
    046    public INVOKEDYNAMIC(final int index) {
    047        super(Const.INVOKEDYNAMIC, index);
    048    }
    049
    050    /**
    051     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
    052     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
    053     *
    054     * @param v Visitor object
    055     */
    056    @Override
    057    public void accept(final Visitor v) {
    058        v.visitExceptionThrower(this);
    059        v.visitTypedInstruction(this);
    060        v.visitStackConsumer(this);
    061        v.visitStackProducer(this);
    062        v.visitLoadClass(this);
    063        v.visitCPInstruction(this);
    064        v.visitFieldOrMethod(this);
    065        v.visitInvokeInstruction(this);
    066        v.visitINVOKEDYNAMIC(this);
    067    }
    068
    069    /**
    070     * Dump instruction as byte code to stream out.
    071     *
    072     * @param out Output stream
    073     */
    074    @Override
    075    public void dump(final DataOutputStream out) throws IOException {
    076        out.writeByte(super.getOpcode());
    077        out.writeShort(super.getIndex());
    078        out.writeByte(0);
    079        out.writeByte(0);
    080    }
    081
    082    /**
    083     * Override the parent method because our classname is held elsewhere.
    084     *
    085     * Note: Contrary to this method's name it does not return the classname of the invoke target; rather it returns the
    086     * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
    087     */
    088    @Override
    089    public String getClassName(final ConstantPoolGen cpg) {
    090        final ConstantPool cp = cpg.getConstantPool();
    091        final ConstantInvokeDynamic cid = cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
    092        return cp.getConstant(cid.getNameAndTypeIndex(), ConstantNameAndType.class).getName(cp);
    093    }
    094
    095    @Override
    096    public Class<?>[] getExceptions() {
    097        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
    098            ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    099    }
    100
    101    /**
    102     * Since InvokeDynamic doesn't refer to a reference type, just return java.lang.Object, as that is the only type we can
    103     * say for sure the reference will be.
    104     *
    105     * @param cpg the ConstantPoolGen used to create the instruction
    106     * @return an ObjectType for java.lang.Object
    107     * @since 6.1
    108     */
    109    @Override
    110    public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
    111        return new ObjectType(Object.class.getName());
    112    }
    113
    114    /**
    115     * Read needed data (i.e., index) from file.
    116     */
    117    @Override
    118    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
    119        super.initFromFile(bytes, wide);
    120        super.setLength(5);
    121        bytes.readByte(); // Skip 0 byte
    122        bytes.readByte(); // Skip 0 byte
    123    }
    124
    125}