    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 opens table of the Module attribute.
    028 * Each entry describes a package which the parent module opens.
    029 *
    030 * @see   Module
    031 * @since 6.4.0
    032 */
    033public final class ModuleOpens implements Cloneable, Node {
    034
    035    private final int opensIndex;  // points to CONSTANT_Package_info
    036    private final int opensFlags;
    037    private final int opensToCount;
    038    private final int[] opensToIndex;  // points to CONSTANT_Module_info
    039
    040
    041    /**
    042     * Construct object from file stream.
    043     *
    044     * @param file Input stream
    045     * @throws IOException if an I/O Exception occurs in readUnsignedShort
    046     */
    047    ModuleOpens(final DataInput file) throws IOException {
    048        opensIndex = file.readUnsignedShort();
    049        opensFlags = file.readUnsignedShort();
    050        opensToCount = file.readUnsignedShort();
    051        opensToIndex = new int[opensToCount];
    052        for (int i = 0; i < opensToCount; i++) {
    053            opensToIndex[i] = file.readUnsignedShort();
    054        }
    055    }
    056
    057
    058    /**
    059     * Called by objects that are traversing the nodes of the tree implicitely
    060     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    061     * fields, attributes, etc. spawns a tree of objects.
    062     *
    063     * @param v Visitor object
    064     */
    065    @Override
    066    public void accept( final Visitor v ) {
    067        v.visitModuleOpens(this);
    068    }
    069
    070    // TODO add more getters and setters?
    071
    072    /**
    073     * Dump table entry to file stream in binary format.
    074     *
    075     * @param file Output file stream
    076     * @throws IOException if an I/O Exception occurs in writeShort
    077     */
    078    public void dump( final DataOutputStream file ) throws IOException {
    079        file.writeShort(opensIndex);
    080        file.writeShort(opensFlags);
    081        file.writeShort(opensToCount);
    082        for (final int entry : opensToIndex) {
    083            file.writeShort(entry);
    084        }
    085    }
    086
    087
    088    /**
    089     * @return String representation
    090     */
    091    @Override
    092    public String toString() {
    093        return "opens(" + opensIndex + ", " + opensFlags + ", " + opensToCount + ", ...)";
    094    }
    095
    096
    097    /**
    098     * @return Resolved string representation
    099     */
    100    public String toString( final ConstantPool constant_pool ) {
    101        final StringBuilder buf = new StringBuilder();
    102        final String package_name = constant_pool.constantToString(opensIndex, Const.CONSTANT_Package);
    103        buf.append(Utility.compactClassName(package_name, false));
    104        buf.append(", ").append(String.format("%04x", opensFlags));
    105        buf.append(", to(").append(opensToCount).append("):\n");
    106        for (final int index : opensToIndex) {
    107            final String module_name = constant_pool.getConstantString(index, Const.CONSTANT_Module);
    108            buf.append("      ").append(Utility.compactClassName(module_name, false)).append("\n");
    109        }
    110        return buf.substring(0, buf.length()-1); // remove the last newline
    111    }
    112
    113
    114    /**
    115     * @return deep copy of this object
    116     */
    117    public ModuleOpens copy() {
    118        try {
    119            return (ModuleOpens) clone();
    120        } catch (final CloneNotSupportedException e) {
    121            // TODO should this throw?
    122        }
    123        return null;
    124    }
    125}