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