Source code

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