    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 <em>Attribute</em> and represents the list of packages that are exported or opened by the Module attribute.
    028 * There may be at most one ModulePackages attribute in a ClassFile structure.
    029 *
    030 * @see     Attribute
    031 */
    032public final class ModulePackages extends Attribute {
    033
    034    private int[] package_index_table;
    035
    036
    037    /**
    038     * Initialize from another object. Note that both objects use the same
    039     * references (shallow copy). Use copy() for a physical copy.
    040     */
    041    public ModulePackages(final ModulePackages c) {
    042        this(c.getNameIndex(), c.getLength(), c.getPackageIndexTable(), c.getConstantPool());
    043    }
    044
    045
    046    /**
    047     * @param name_index Index in constant pool
    048     * @param length Content length in bytes
    049     * @param package_index_table Table of indices in constant pool
    050     * @param constant_pool Array of constants
    051     */
    052    public ModulePackages(final int name_index, final int length, final int[] package_index_table,
    053            final ConstantPool constant_pool) {
    054        super(Const.ATTR_MODULE_PACKAGES, name_index, length, constant_pool);
    055        this.package_index_table = package_index_table != null ? package_index_table : new int[0];
    056    }
    057
    058
    059    /**
    060     * Construct object from input stream.
    061     * @param name_index Index in constant pool
    062     * @param length Content length in bytes
    063     * @param input Input stream
    064     * @param constant_pool Array of constants
    065     * @throws IOException
    066     */
    067    ModulePackages(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
    068        this(name_index, length, (int[]) null, constant_pool);
    069        final int number_of_packages = input.readUnsignedShort();
    070        package_index_table = new int[number_of_packages];
    071        for (int i = 0; i < number_of_packages; i++) {
    072            package_index_table[i] = input.readUnsignedShort();
    073        }
    074    }
    075
    076
    077    /**
    078     * Called by objects that are traversing the nodes of the tree implicitely
    079     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    080     * fields, attributes, etc. spawns a tree of objects.
    081     *
    082     * @param v Visitor object
    083     */
    084    @Override
    085    public void accept( final Visitor v ) {
    086        v.visitModulePackages(this);
    087    }
    088
    089
    090    /**
    091     * Dump ModulePackages attribute to file stream in binary format.
    092     *
    093     * @param file Output file stream
    094     * @throws IOException
    095     */
    096    @Override
    097    public void dump( final DataOutputStream file ) throws IOException {
    098        super.dump(file);
    099        file.writeShort(package_index_table.length);
    100        for (final int index : package_index_table) {
    101            file.writeShort(index);
    102        }
    103    }
    104
    105
    106    /**
    107     * @return array of indices into constant pool of package names.
    108     */
    109    public int[] getPackageIndexTable() {
    110        return package_index_table;
    111    }
    112
    113
    114    /**
    115     * @return Length of package table.
    116     */
    117    public int getNumberOfPackages() {
    118        return package_index_table == null ? 0 : package_index_table.length;
    119    }
    120
    121
    122    /**
    123     * @return string array of package names
    124     */
    125    public String[] getPackageNames() {
    126        final String[] names = new String[package_index_table.length];
    127        for (int i = 0; i < package_index_table.length; i++) {
    128            names[i] = super.getConstantPool().getConstantString(package_index_table[i],
    129                    Const.CONSTANT_Package).replace('/', '.');
    130        }
    131        return names;
    132    }
    133
    134
    135    /**
    136     * @param package_index_table the list of package indexes
    137     * Also redefines number_of_packages according to table length.
    138     */
    139    public void setPackageIndexTable( final int[] package_index_table ) {
    140        this.package_index_table = package_index_table != null ? package_index_table : new int[0];
    141    }
    142
    143
    144    /**
    145     * @return String representation, i.e., a list of packages.
    146     */
    147    @Override
    148    public String toString() {
    149        final StringBuilder buf = new StringBuilder();
    150        buf.append("ModulePackages(");
    151        buf.append(package_index_table.length);
    152        buf.append("):\n");
    153        for (final int index : package_index_table) {
    154            final String package_name = super.getConstantPool().getConstantString(index, Const.CONSTANT_Package);
    155            buf.append("  ").append(Utility.compactClassName(package_name, false)).append("\n");
    156        }
    157        return buf.substring(0, buf.length()-1); // remove the last newline
    158    }
    159
    160
    161    /**
    162     * @return deep copy of this attribute
    163     */
    164    @Override
    165    public Attribute copy( final ConstantPool _constant_pool ) {
    166        final ModulePackages c = (ModulePackages) clone();
    167        if (package_index_table != null) {
    168            c.package_index_table = new int[package_index_table.length];
    169            System.arraycopy(package_index_table, 0, c.package_index_table, 0,
    170                    package_index_table.length);
    171        }
    172        c.setConstantPool(_constant_pool);
    173        return c;
    174    }
    175}