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