001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.Const;
025import org.apache.bcel.ExceptionConst;
026import org.apache.bcel.classfile.ConstantInvokeDynamic;
027import org.apache.bcel.classfile.ConstantNameAndType;
028import org.apache.bcel.classfile.ConstantPool;
029import org.apache.bcel.util.ByteSequence;
030
031/**
032 * Class for INVOKEDYNAMIC. Not an instance of InvokeInstruction, since that class expects to be able to get the class
033 * of the method. Ignores the bootstrap mechanism entirely.
034 *
035 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic"> The
036 *      invokedynamic instruction in The Java Virtual Machine Specification</a>
037 * @since 6.0
038 */
039public class INVOKEDYNAMIC extends InvokeInstruction {
040
041    /**
042     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
043     */
044    INVOKEDYNAMIC() {
045    }
046
047    /**
048     * Constructs an INVOKEDYNAMIC instruction.
049     *
050     * @param index index into constant pool.
051     */
052    public INVOKEDYNAMIC(final int index) {
053        super(Const.INVOKEDYNAMIC, index);
054        super.setLength(5);
055    }
056
057    /**
058     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
059     * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
060     *
061     * @param v Visitor object.
062     */
063    @Override
064    public void accept(final Visitor v) {
065        v.visitExceptionThrower(this);
066        v.visitTypedInstruction(this);
067        v.visitStackConsumer(this);
068        v.visitStackProducer(this);
069        v.visitLoadClass(this);
070        v.visitCPInstruction(this);
071        v.visitFieldOrMethod(this);
072        v.visitInvokeInstruction(this);
073        v.visitINVOKEDYNAMIC(this);
074    }
075
076    /**
077     * Dumps instruction as byte code to stream out.
078     *
079     * @param out Output stream.
080     */
081    @Override
082    public void dump(final DataOutputStream out) throws IOException {
083        out.writeByte(super.getOpcode());
084        out.writeShort(super.getIndex());
085        out.writeByte(0);
086        out.writeByte(0);
087    }
088
089    /**
090     * Gets the name of the method used to invoke the generated lambda method.
091     *
092     * Override the parent method because our class name is held elsewhere.
093     *
094     * Note: Contrary to this method's name it does not return the class name of the invoke target; rather it returns the
095     * name of the method that will be used to invoke the Lambda method generated by this invoke dynamic instruction.
096     */
097    @Override
098    public String getClassName(final ConstantPoolGen cpg) {
099        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}