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.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.Const;
    025
    026/**
    027 * This class is derived from the abstract {@link Constant}
    028 * and represents a reference to a method handle.
    029 *
    030 * @see     Constant
    031 * @since 6.0
    032 */
    033public final class ConstantMethodHandle extends Constant {
    034
    035    private int reference_kind;
    036    private int reference_index;
    037
    038
    039    /**
    040     * Initialize from another object.
    041     */
    042    public ConstantMethodHandle(final ConstantMethodHandle c) {
    043        this(c.getReferenceKind(), c.getReferenceIndex());
    044    }
    045
    046
    047    /**
    048     * Initialize instance from file data.
    049     *
    050     * @param file Input stream
    051     * @throws IOException
    052     */
    053    ConstantMethodHandle(final DataInput file) throws IOException {
    054        this(file.readUnsignedByte(), file.readUnsignedShort());
    055    }
    056
    057
    058    public ConstantMethodHandle(final int reference_kind, final int reference_index) {
    059        super(Const.CONSTANT_MethodHandle);
    060        this.reference_kind = reference_kind;
    061        this.reference_index = reference_index;
    062    }
    063
    064
    065    /**
    066     * Called by objects that are traversing the nodes of the tree implicitly
    067     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    068     * fields, attributes, etc. spawns a tree of objects.
    069     *
    070     * @param v Visitor object
    071     */
    072    @Override
    073    public void accept( final Visitor v ) {
    074        v.visitConstantMethodHandle(this);
    075    }
    076
    077
    078    /**
    079     * Dump method kind and index to file stream in binary format.
    080     *
    081     * @param file Output file stream
    082     * @throws IOException
    083     */
    084    @Override
    085    public void dump( final DataOutputStream file ) throws IOException {
    086        file.writeByte(super.getTag());
    087        file.writeByte(reference_kind);
    088        file.writeShort(reference_index);
    089    }
    090
    091
    092    public int getReferenceKind() {
    093        return reference_kind;
    094    }
    095
    096
    097    public void setReferenceKind(final int reference_kind) {
    098        this.reference_kind = reference_kind;
    099    }
    100
    101
    102    public int getReferenceIndex() {
    103        return reference_index;
    104    }
    105
    106
    107    public void setReferenceIndex(final int reference_index) {
    108        this.reference_index = reference_index;
    109    }
    110
    111
    112    /**
    113     * @return String representation
    114     */
    115    @Override
    116    public String toString() {
    117        return super.toString() + "(reference_kind = " + reference_kind +
    118                ", reference_index = " + reference_index + ")";
    119    }
    120}