    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.classfile;
    020
    021import java.io.DataInput;
    022import java.io.DataOutputStream;
    023import java.io.IOException;
    024
    025import org.apache.bcel.Const;
    026
    027/**
    028 * This class is derived from the abstract {@link Constant} and represents a reference to a module.
    029 *
    030 * <p>
    031 * Note: Early access Java 9 support- currently subject to change
    032 * </p>
    033 *
    034 * @see Constant
    035 * @since 6.1
    036 */
    037public final class ConstantModule extends Constant implements ConstantObject {
    038
    039    private int nameIndex;
    040
    041    /**
    042     * Initialize from another object.
    043     *
    044     * @param c Source to copy.
    045     */
    046    public ConstantModule(final ConstantModule c) {
    047        this(c.getNameIndex());
    048    }
    049
    050    /**
    051     * Initialize instance from file data.
    052     *
    053     * @param file Input stream.
    054     * @throws IOException if an I/O error occurs.
    055     */
    056    ConstantModule(final DataInput file) throws IOException {
    057        this(file.readUnsignedShort());
    058    }
    059
    060    /**
    061     * Constructs a ConstantModule.
    062     *
    063     * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8.
    064     */
    065    public ConstantModule(final int nameIndex) {
    066        super(Const.CONSTANT_Module);
    067        this.nameIndex = nameIndex;
    068    }
    069
    070    /**
    071     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    072     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    073     *
    074     * @param v Visitor object.
    075     */
    076    @Override
    077    public void accept(final Visitor v) {
    078        v.visitConstantModule(this);
    079    }
    080
    081    /**
    082     * Dumps constant module to file stream in binary format.
    083     *
    084     * @param file Output file stream.
    085     * @throws IOException if an I/O error occurs.
    086     */
    087    @Override
    088    public void dump(final DataOutputStream file) throws IOException {
    089        file.writeByte(super.getTag());
    090        file.writeShort(nameIndex);
    091    }
    092
    093    /**
    094     * Gets the dereferenced string.
    095     *
    096     * @param cp the constant pool.
    097     * @return dereferenced string.
    098     */
    099    public String getBytes(final ConstantPool cp) {
    100        return (String) getConstantValue(cp);
    101    }
    102
    103    /**
    104     * Gets the String object.
    105     *
    106     * @param cp the constant pool.
    107     * @return String object.
    108     */
    109    @Override
    110    public Object getConstantValue(final ConstantPool cp) {
    111        return cp.getConstantUtf8(nameIndex).getBytes();
    112    }
    113
    114    /**
    115     * Gets the name index in constant pool of module name.
    116     *
    117     * @return Name index in constant pool of module name.
    118     */
    119    public int getNameIndex() {
    120        return nameIndex;
    121    }
    122
    123    /**
    124     * Sets the name index in the constant pool.
    125     *
    126     * @param nameIndex the name index in the constant pool of this Constant Module.
    127     */
    128    public void setNameIndex(final int nameIndex) {
    129        this.nameIndex = nameIndex;
    130    }
    131
    132    /**
    133     * @return String representation.
    134     */
    135    @Override
    136    public String toString() {
    137        return super.toString() + "(nameIndex = " + nameIndex + ")";
    138    }
    139}