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 modules required, exported, opened or provided by a module.
028 * There may be at most one Module attribute in a ClassFile structure.
029 *
030 * @see   Attribute
031 * @since 6.4.0
032 */
033public final class Module extends Attribute {
034
035    private final int module_name_index;
036    private final int module_flags;
037    private final int module_version_index;
038
039    private ModuleRequires[] requires_table;
040    private ModuleExports[] exports_table;
041    private ModuleOpens[] opens_table;
042    private final int uses_count;
043    private final int[] uses_index;
044    private ModuleProvides[] provides_table;
045
046    /**
047     * Construct object from input stream.
048     * @param name_index Index in constant pool
049     * @param length Content length in bytes
050     * @param input Input stream
051     * @param constant_pool Array of constants
052     * @throws IOException
053     */
054    Module(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
055        super(Const.ATTR_MODULE, name_index, length, constant_pool);
056
057        module_name_index = input.readUnsignedShort();
058        module_flags = input.readUnsignedShort();
059        module_version_index = input.readUnsignedShort();
060
061        final int requires_count = input.readUnsignedShort();
062        requires_table = new ModuleRequires[requires_count];
063        for (int i = 0; i < requires_count; i++) {
064            requires_table[i] = new ModuleRequires(input);
065        }
066
067        final int exports_count = input.readUnsignedShort();
068        exports_table = new ModuleExports[exports_count];
069        for (int i = 0; i < exports_count; i++) {
070            exports_table[i] = new ModuleExports(input);
071        }
072
073        final int opens_count = input.readUnsignedShort();
074        opens_table = new ModuleOpens[opens_count];
075        for (int i = 0; i < opens_count; i++) {
076            opens_table[i] = new ModuleOpens(input);
077        }
078
079        uses_count = input.readUnsignedShort();
080        uses_index = new int[uses_count];
081        for (int i = 0; i < uses_count; i++) {
082            uses_index[i] = input.readUnsignedShort();
083        }
084
085        final int provides_count = input.readUnsignedShort();
086        provides_table = new ModuleProvides[provides_count];
087        for (int i = 0; i < provides_count; i++) {
088            provides_table[i] = new ModuleProvides(input);
089        }
090    }
091
092
093    /**
094     * Called by objects that are traversing the nodes of the tree implicitely
095     * defined by the contents of a Java class. I.e., the hierarchy of methods,
096     * fields, attributes, etc. spawns a tree of objects.
097     *
098     * @param v Visitor object
099     */
100    @Override
101    public void accept( final Visitor v ) {
102        v.visitModule(this);
103    }
104
105    // TODO add more getters and setters?
106
107    /**
108     * @return table of required modules
109     * @see ModuleRequires
110     */
111    public ModuleRequires[] getRequiresTable() {
112        return requires_table;
113    }
114
115
116    /**
117     * @return table of exported interfaces
118     * @see ModuleExports
119     */
120    public ModuleExports[] getExportsTable() {
121        return exports_table;
122    }
123
124
125    /**
126     * @return table of provided interfaces
127     * @see ModuleOpens
128     */
129    public ModuleOpens[] getOpensTable() {
130        return opens_table;
131    }
132
133
134    /**
135     * @return table of provided interfaces
136     * @see ModuleProvides
137     */
138    public ModuleProvides[] getProvidesTable() {
139        return provides_table;
140    }
141
142
143    /**
144     * Dump Module attribute to file stream in binary format.
145     *
146     * @param file Output file stream
147     * @throws IOException
148     */
149    @Override
150    public void dump( final DataOutputStream file ) throws IOException {
151        super.dump(file);
152
153        file.writeShort(module_name_index);
154        file.writeShort(module_flags);
155        file.writeShort(module_version_index);
156
157        file.writeShort(requires_table.length);
158        for (final ModuleRequires entry : requires_table) {
159            entry.dump(file);
160        }
161
162        file.writeShort(exports_table.length);
163        for (final ModuleExports entry : exports_table) {
164            entry.dump(file);
165        }
166
167        file.writeShort(opens_table.length);
168        for (final ModuleOpens entry : opens_table) {
169            entry.dump(file);
170        }
171
172        file.writeShort(uses_index.length);
173        for (final int entry : uses_index) {
174            file.writeShort(entry);
175        }
176
177        file.writeShort(provides_table.length);
178        for (final ModuleProvides entry : provides_table) {
179            entry.dump(file);
180        }
181    }
182
183
184    /**
185     * @return String representation, i.e., a list of packages.
186     */
187    @Override
188    public String toString() {
189        final ConstantPool cp = super.getConstantPool();
190        final StringBuilder buf = new StringBuilder();
191        buf.append("Module:\n");
192        buf.append("  name:    ") .append(cp.getConstantString(module_name_index, Const.CONSTANT_Module).replace('/', '.')).append("\n");
193        buf.append("  flags:   ") .append(String.format("%04x", module_flags)).append("\n");
194        final String version = module_version_index == 0 ? "0" : cp.getConstantString(module_version_index, Const.CONSTANT_Utf8);
195        buf.append("  version: ") .append(version).append("\n");
196
197        buf.append("  requires(").append(requires_table.length).append("):\n");
198        for (final ModuleRequires module : requires_table) {
199            buf.append("    ").append(module.toString(cp)).append("\n");
200        }
201
202        buf.append("  exports(").append(exports_table.length).append("):\n");
203        for (final ModuleExports module : exports_table) {
204            buf.append("    ").append(module.toString(cp)).append("\n");
205        }
206
207        buf.append("  opens(").append(opens_table.length).append("):\n");
208        for (final ModuleOpens module : opens_table) {
209            buf.append("    ").append(module.toString(cp)).append("\n");
210        }
211
212        buf.append("  uses(").append(uses_index.length).append("):\n");
213        for (final int index : uses_index) {
214            final String class_name = cp.getConstantString(index, Const.CONSTANT_Class);
215            buf.append("    ").append(Utility.compactClassName(class_name, false)).append("\n");
216        }
217
218        buf.append("  provides(").append(provides_table.length).append("):\n");
219        for (final ModuleProvides module : provides_table) {
220            buf.append("    ").append(module.toString(cp)).append("\n");
221        }
222
223        return buf.substring(0, buf.length()-1); // remove the last newline
224    }
225
226
227    /**
228     * @return deep copy of this attribute
229     */
230    @Override
231    public Attribute copy( final ConstantPool _constant_pool ) {
232        final Module c = (Module) clone();
233
234        c.requires_table = new ModuleRequires[requires_table.length];
235        for (int i = 0; i < requires_table.length; i++) {
236            c.requires_table[i] = requires_table[i].copy();
237        }
238
239        c.exports_table = new ModuleExports[exports_table.length];
240        for (int i = 0; i < exports_table.length; i++) {
241            c.exports_table[i] = exports_table[i].copy();
242        }
243
244        c.opens_table = new ModuleOpens[opens_table.length];
245        for (int i = 0; i < opens_table.length; i++) {
246            c.opens_table[i] = opens_table[i].copy();
247        }
248
249        c.provides_table = new ModuleProvides[provides_table.length];
250        for (int i = 0; i < provides_table.length; i++) {
251            c.provides_table[i] = provides_table[i].copy();
252        }
253
254        c.setConstantPool(_constant_pool);
255        return c;
256    }
257}