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 */
017package org.apache.bcel.generic;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022import org.apache.bcel.Const;
023import org.apache.bcel.ExceptionConst;
024import org.apache.bcel.classfile.ConstantInvokeDynamic;
025import org.apache.bcel.classfile.ConstantNameAndType;
026import org.apache.bcel.classfile.ConstantPool;
027import org.apache.bcel.util.ByteSequence;
028
029/**
030 * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
031 * of the method. Ignores the bootstrap mechanism entirely.
032 *
033 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
034 *      invokedynamic instruction in The Java Virtual Machine Specification</a>
035 * @since 6.0
036 */
037public class INVOKEDYNAMIC extends InvokeInstruction {
038
039    /**
040     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
041     */
042    INVOKEDYNAMIC() {
043    }
044
045    public INVOKEDYNAMIC(final int index) {
046        super(Const.INVOKEDYNAMIC, index);
047    }
048
049    /**
050     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
051     * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
052     *
053     * @param v Visitor object
054     */
055    @Override
056    public void accept(final Visitor v) {
057        v.visitExceptionThrower(this);
058        v.visitTypedInstruction(this);
059        v.visitStackConsumer(this);
060        v.visitStackProducer(this);
061        v.visitLoadClass(this);
062        v.visitCPInstruction(this);
063        v.visitFieldOrMethod(this);
064        v.visitInvokeInstruction(this);
065        v.visitINVOKEDYNAMIC(this);
066    }
067
068    /**
069     * Dump instruction as byte code to stream out.
070     *
071     * @param out Output stream
072     */
073    @Override
074    public void dump(final DataOutputStream out) throws IOException {
075        out.writeByte(super.getOpcode());
076        out.writeShort(super.getIndex());
077        out.writeByte(0);
078        out.writeByte(0);
079    }
080
081    /**
082     * Override the parent method because our class name is held elsewhere.
083     *
084     * Note: Contrary to this method's name it does not return the class name of the invoke target; rather it returns the
085     * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
086     */
087    @Override
088    public String getClassName(final ConstantPoolGen cpg) {
089        final ConstantPool cp = cpg.getConstantPool();
090        final ConstantInvokeDynamic cid = cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic, ConstantInvokeDynamic.class);
091        return cp.getConstant(cid.getNameAndTypeIndex(), ConstantNameAndType.class).getName(cp);
092    }
093
094    @Override
095    public Class<?>[] getExceptions() {
096        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION, ExceptionConst.UNSATISFIED_LINK_ERROR,
097            ExceptionConst.ABSTRACT_METHOD_ERROR, ExceptionConst.ILLEGAL_ACCESS_ERROR, ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
098    }
099
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}