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 */
019
020package org.apache.bcel.classfile;
021
022import java.io.DataInput;
023import java.io.DataOutputStream;
024import java.io.IOException;
025
026import org.apache.bcel.Const;
027
028/**
029 * This class represents an entry in the requires table of the Module attribute. Each entry describes a module on which
030 * the parent module depends.
031 *
032 * @see Module
033 * @since 6.4.0
034 */
035public final class ModuleRequires implements Cloneable, Node {
036
037    private final int requiresIndex; // points to CONSTANT_Module_info
038    private final int requiresFlags;
039    private final int requiresVersionIndex; // either 0 or points to CONSTANT_Utf8_info
040
041    /**
042     * Constructs object from file stream.
043     *
044     * @param file Input stream.
045     * @throws IOException Thrown if an I/O Exception occurs in readUnsignedShort.
046     */
047    ModuleRequires(final DataInput file) throws IOException {
048        requiresIndex = file.readUnsignedShort();
049        requiresFlags = file.readUnsignedShort();
050        requiresVersionIndex = file.readUnsignedShort();
051    }
052
053    /**
054     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
055     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
056     *
057     * @param v Visitor object.
058     */
059    @Override
060    public void accept(final Visitor v) {
061        v.visitModuleRequires(this);
062    }
063
064    /**
065     * Creates a deep copy of this object.
066     *
067     * @return deep copy of this object.
068     */
069    public ModuleRequires copy() {
070        try {
071            return (ModuleRequires) clone();
072        } catch (final CloneNotSupportedException e) {
073            // TODO should this throw?
074        }
075        return null;
076    }
077
078    /**
079     * Dumps table entry to file stream in binary format.
080     *
081     * @param file Output file stream.
082     * @throws IOException Thrown if an I/O Exception occurs in writeShort.
083     */
084    public void dump(final DataOutputStream file) throws IOException {
085        file.writeShort(requiresIndex);
086        file.writeShort(requiresFlags);
087        file.writeShort(requiresVersionIndex);
088    }
089
090    /**
091     * Gets the module name from the constant pool.
092     *
093     * @param constantPool Array of constants usually obtained from the ClassFile object.
094     * @return module name.
095     * @since 6.10.0
096     */
097    public String getModuleName(final ConstantPool constantPool) {
098        return constantPool.constantToString(requiresIndex, Const.CONSTANT_Module);
099    }
100
101    /**
102     * Gets the flags for this ModuleRequires.
103     *
104     * @return The requiresFlags.
105     * @since 6.10.0
106     */
107    public int getRequiresFlags() {
108        return requiresFlags;
109    }
110
111    /**
112     * Gets the required version from the constant pool.
113     *
114     * @param constantPool Array of constants usually obtained from the ClassFile object.
115     * @return required version, "0" if version index is 0.
116     * @since 6.10.0
117     */
118    public String getVersion(final ConstantPool constantPool) {
119        return requiresVersionIndex == 0 ? "0" : constantPool.getConstantString(requiresVersionIndex, Const.CONSTANT_Utf8);
120    }
121
122    /**
123     * @return String representation.
124     */
125    @Override
126    public String toString() {
127        return "requires(" + requiresIndex + ", " + String.format("%04x", requiresFlags) + ", " + requiresVersionIndex + ")";
128    }
129
130    /**
131     * Gets the resolved string representation.
132     *
133     * @param constantPool The constant pool.
134     * @return Resolved string representation.
135     */
136    public String toString(final ConstantPool constantPool) {
137        final StringBuilder buf = new StringBuilder();
138        final String moduleName = getModuleName(constantPool);
139        buf.append(moduleName);
140        buf.append(", ").append(String.format("%04x", requiresFlags));
141        final String version = getVersion(constantPool);
142        buf.append(", ").append(version);
143        return buf.toString();
144    }
145}