    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
    032 * expects to be able to get the class of the method. Ignores the bootstrap
    033 * mechanism entirely.
    034 *
    035 * @version $Id: InvokeInstruction.java 1152072 2011-07-29 01:54:05Z dbrosius $
    036 * @see
    037 * <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokedynamic">
    038 * The invokedynamic instruction in The Java Virtual Machine Specification</a>
    039 * @since 6.0
    040 */
    041public class INVOKEDYNAMIC extends InvokeInstruction {
    042
    043    /**
    044     * Empty constructor needed for Instruction.readInstruction.
    045     * Not to be used otherwise.
    046     */
    047    INVOKEDYNAMIC() {
    048    }
    049
    050
    051    public INVOKEDYNAMIC(final int index) {
    052        super(Const.INVOKEDYNAMIC, index);
    053    }
    054
    055
    056    /**
    057     * Dump instruction as byte code to stream out.
    058     * @param out Output stream
    059     */
    060    @Override
    061    public void dump( final DataOutputStream out ) throws IOException {
    062        out.writeByte(super.getOpcode());
    063        out.writeShort(super.getIndex());
    064        out.writeByte(0);
    065        out.writeByte(0);
    066       }
    067
    068
    069    /**
    070     * Read needed data (i.e., index) from file.
    071     */
    072    @Override
    073    protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
    074        super.initFromFile(bytes, wide);
    075        super.setLength(5);
    076        bytes.readByte(); // Skip 0 byte
    077        bytes.readByte(); // Skip 0 byte
    078    }
    079
    080
    081    /**
    082     * @return mnemonic for instruction with symbolic references resolved
    083     */
    084    @Override
    085    public String toString( final ConstantPool cp ) {
    086        return super.toString(cp);
    087    }
    088
    089
    090    @Override
    091    public Class<?>[] getExceptions() {
    092        return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_INTERFACE_METHOD_RESOLUTION,
    093            ExceptionConst.UNSATISFIED_LINK_ERROR,
    094            ExceptionConst.ABSTRACT_METHOD_ERROR,
    095            ExceptionConst.ILLEGAL_ACCESS_ERROR,
    096            ExceptionConst.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    097    }
    098
    099
    100    /**
    101     * Call corresponding visitor method(s). The order is:
    102     * Call visitor methods of implemented interfaces first, then
    103     * call methods according to the class hierarchy in descending order,
    104     * i.e., the most specific visitXXX() call comes last.
    105     *
    106     * @param v Visitor object
    107     */
    108    @Override
    109    public void accept( final Visitor v ) {
    110        v.visitExceptionThrower(this);
    111        v.visitTypedInstruction(this);
    112        v.visitStackConsumer(this);
    113        v.visitStackProducer(this);
    114        v.visitLoadClass(this);
    115        v.visitCPInstruction(this);
    116        v.visitFieldOrMethod(this);
    117        v.visitInvokeInstruction(this);
    118        v.visitINVOKEDYNAMIC(this);
    119    }
    120
    121    /**
    122     * Override the parent method because our classname is held elsewhere.
    123     */
    124    @Override
    125    public String getClassName( final ConstantPoolGen cpg ) {
    126        final ConstantPool cp = cpg.getConstantPool();
    127        final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) cp.getConstant(super.getIndex(), Const.CONSTANT_InvokeDynamic);
    128        return ((ConstantNameAndType) cp.getConstant(cid.getNameAndTypeIndex())).getName(cp);
    129    }
    130
    131
    132    /**
    133     * Since InvokeDynamic doesn't refer to a reference type, just return java.lang.Object,
    134     * as that is the only type we can say for sure the reference will be.
    135     *
    136     * @param cpg
    137     *            the ConstantPoolGen used to create the instruction
    138     * @return an ObjectType for java.lang.Object
    139     * @since 6.1
    140     */
    141    @Override
    142    public ReferenceType getReferenceType(final ConstantPoolGen cpg) {
    143        return new ObjectType(Object.class.getName());
    144    }
    145}