    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 exports table of the Module attribute.
    028 * Each entry describes a package which may open the parent module.
    029 *
    030 * @see   Module
    031 * @since 6.4.0
    032 */
    033public final class ModuleExports implements Cloneable, Node {
    034
    035    private final int exports_index;  // points to CONSTANT_Package_info
    036    private final int exports_flags;
    037    private final int exports_to_count;
    038    private final int[] exports_to_index;  // 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    ModuleExports(final DataInput file) throws IOException {
    048        exports_index = file.readUnsignedShort();
    049        exports_flags = file.readUnsignedShort();
    050        exports_to_count = file.readUnsignedShort();
    051        exports_to_index = new int[exports_to_count];
    052        for (int i = 0; i < exports_to_count; i++) {
    053            exports_to_index[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.visitModuleExports(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(exports_index);
    080        file.writeShort(exports_flags);
    081        file.writeShort(exports_to_count);
    082        for (final int entry : exports_to_index) {
    083            file.writeShort(entry);
    084        }
    085    }
    086
    087
    088    /**
    089     * @return String representation
    090     */
    091    @Override
    092    public String toString() {
    093        return "exports(" + exports_index + ", " + exports_flags + ", " + exports_to_count + ", ...)";
    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(exports_index, Const.CONSTANT_Package);
    103        buf.append(Utility.compactClassName(package_name, false));
    104        buf.append(", ").append(String.format("%04x", exports_flags));
    105        buf.append(", to(").append(exports_to_count).append("):\n");
    106        for (final int index : exports_to_index) {
    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 ModuleExports copy() {
    118        try {
    119            return (ModuleExports) clone();
    120        } catch (final CloneNotSupportedException e) {
    121            // TODO should this throw?
    122        }
    123        return null;
    124    }
    125}