Module_attribute.java revision 3792:d516975e8110
154359Sroberto/*
254359Sroberto * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
354359Sroberto * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
454359Sroberto *
554359Sroberto * This code is free software; you can redistribute it and/or modify it
654359Sroberto * under the terms of the GNU General Public License version 2 only, as
754359Sroberto * published by the Free Software Foundation.  Oracle designates this
854359Sroberto * particular file as subject to the "Classpath" exception as provided
954359Sroberto * by Oracle in the LICENSE file that accompanied this code.
1054359Sroberto *
1154359Sroberto * This code is distributed in the hope that it will be useful, but WITHOUT
12132451Sroberto * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13132451Sroberto * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14285612Sdelphij * version 2 for more details (a copy is included in the LICENSE file that
1582498Sroberto * accompanied this code).
1654359Sroberto *
1754359Sroberto * You should have received a copy of the GNU General Public License version
1854359Sroberto * 2 along with this work; if not, write to the Free Software Foundation,
1954359Sroberto * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2054359Sroberto *
2154359Sroberto * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2254359Sroberto * or visit www.oracle.com if you need additional information or have any
2354359Sroberto * questions.
2454359Sroberto */
2554359Sroberto
2654359Srobertopackage com.sun.tools.classfile;
2754359Sroberto
2854359Srobertoimport java.io.IOException;
2954359Sroberto
3054359Sroberto/**
3182498Sroberto * See Jigsaw.
32132451Sroberto *
33132451Sroberto *  <p><b>This is NOT part of any supported API.
34132451Sroberto *  If you write code that depends on this, you do so at your own risk.
35132451Sroberto *  This code and its internal interfaces are subject to change or
36132451Sroberto *  deletion without notice.</b>
37132451Sroberto */
3882498Srobertopublic class Module_attribute extends Attribute {
39132451Sroberto    public static final int ACC_TRANSITIVE      =   0x10;
40132451Sroberto    public static final int ACC_STATIC_PHASE    =   0x20;
41132451Sroberto    public static final int ACC_OPEN            =   0x20;
4282498Sroberto    public static final int ACC_SYNTHETIC       = 0x1000;
43182007Sroberto    public static final int ACC_MANDATED        = 0x8000;
44182007Sroberto
45182007Sroberto    Module_attribute(ClassReader cr, int name_index, int length) throws IOException {
46182007Sroberto        super(name_index, length);
47182007Sroberto
48182007Sroberto        module_name = cr.readUnsignedShort();
49182007Sroberto        module_flags = cr.readUnsignedShort();
50182007Sroberto
51182007Sroberto        requires_count = cr.readUnsignedShort();
52182007Sroberto        requires = new RequiresEntry[requires_count];
53182007Sroberto        for (int i = 0; i < requires_count; i++)
5454359Sroberto            requires[i] = new RequiresEntry(cr);
55285612Sdelphij
56285612Sdelphij        exports_count = cr.readUnsignedShort();
57285612Sdelphij        exports = new ExportsEntry[exports_count];
58285612Sdelphij        for (int i = 0; i < exports_count; i++)
59285612Sdelphij            exports[i] = new ExportsEntry(cr);
60285612Sdelphij
61285612Sdelphij        opens_count = cr.readUnsignedShort();
62285612Sdelphij        opens = new OpensEntry[opens_count];
63285612Sdelphij        for (int i = 0; i < opens_count; i++)
64285612Sdelphij            opens[i] = new OpensEntry(cr);
65285612Sdelphij
66285612Sdelphij        uses_count = cr.readUnsignedShort();
67285612Sdelphij        uses_index = new int[uses_count];
68285612Sdelphij        for (int i = 0; i < uses_count; i++)
69285612Sdelphij            uses_index[i] = cr.readUnsignedShort();
70285612Sdelphij
71285612Sdelphij        provides_count = cr.readUnsignedShort();
72285612Sdelphij        provides = new ProvidesEntry[provides_count];
73285612Sdelphij        for (int i = 0; i < provides_count; i++)
74285612Sdelphij            provides[i] = new ProvidesEntry(cr);
75285612Sdelphij    }
76285612Sdelphij
77285612Sdelphij    public Module_attribute(int name_index,
78285612Sdelphij            int module_name,
79285612Sdelphij            int module_flags,
80285612Sdelphij            RequiresEntry[] requires,
81285612Sdelphij            ExportsEntry[] exports,
82285612Sdelphij            OpensEntry[] opens,
83285612Sdelphij            int[] uses,
84285612Sdelphij            ProvidesEntry[] provides) {
85285612Sdelphij        super(name_index, 2);
86285612Sdelphij        this.module_name = module_name;
87285612Sdelphij        this.module_flags = module_flags;
88285612Sdelphij        requires_count = requires.length;
89285612Sdelphij        this.requires = requires;
90285612Sdelphij        exports_count = exports.length;
91285612Sdelphij        this.exports = exports;
92285612Sdelphij        opens_count = opens.length;
93285612Sdelphij        this.opens = opens;
94285612Sdelphij        uses_count = uses.length;
95285612Sdelphij        this.uses_index = uses;
96285612Sdelphij        provides_count = provides.length;
97182007Sroberto        this.provides = provides;
98182007Sroberto    }
99182007Sroberto
100182007Sroberto    public String getUses(int index, ConstantPool constant_pool) throws ConstantPoolException {
101182007Sroberto        int i = uses_index[index];
102182007Sroberto        return constant_pool.getClassInfo(i).getName();
103182007Sroberto    }
104182007Sroberto
105182007Sroberto    @Override
106182007Sroberto    public <R, D> R accept(Visitor<R, D> visitor, D data) {
107182007Sroberto        return visitor.visitModule(this, data);
108182007Sroberto    }
109182007Sroberto
110182007Sroberto    public final int module_name;
111182007Sroberto    public final int module_flags;
112182007Sroberto    public final int requires_count;
113182007Sroberto    public final RequiresEntry[] requires;
114182007Sroberto    public final int exports_count;
11554359Sroberto    public final ExportsEntry[] exports;
11682498Sroberto    public final int opens_count;
11782498Sroberto    public final OpensEntry[] opens;
11854359Sroberto    public final int uses_count;
11954359Sroberto    public final int[] uses_index;
12082498Sroberto    public final int provides_count;
12182498Sroberto    public final ProvidesEntry[] provides;
12254359Sroberto
12354359Sroberto    public static class RequiresEntry {
12454359Sroberto        RequiresEntry(ClassReader cr) throws IOException {
12582498Sroberto            requires_index = cr.readUnsignedShort();
12682498Sroberto            requires_flags = cr.readUnsignedShort();
12782498Sroberto        }
12882498Sroberto
12954359Sroberto        public RequiresEntry(int index, int flags) {
13054359Sroberto            this.requires_index = index;
13154359Sroberto            this.requires_flags = flags;
13254359Sroberto        }
13354359Sroberto
13454359Sroberto        public String getRequires(ConstantPool constant_pool) throws ConstantPoolException {
13582498Sroberto            return constant_pool.getUTF8Value(requires_index);
13654359Sroberto        }
13754359Sroberto
13854359Sroberto        public static final int length = 4;
13954359Sroberto
14054359Sroberto        public final int requires_index;
14154359Sroberto        public final int requires_flags;
14254359Sroberto    }
14354359Sroberto
14454359Sroberto    public static class ExportsEntry {
14556746Sroberto        ExportsEntry(ClassReader cr) throws IOException {
14656746Sroberto            exports_index = cr.readUnsignedShort();
14756746Sroberto            exports_flags = cr.readUnsignedShort();
14882498Sroberto            exports_to_count = cr.readUnsignedShort();
14956746Sroberto            exports_to_index = new int[exports_to_count];
15082498Sroberto            for (int i = 0; i < exports_to_count; i++)
15182498Sroberto                exports_to_index[i] = cr.readUnsignedShort();
15256746Sroberto        }
15356746Sroberto
15454359Sroberto        public ExportsEntry(int index, int flags, int[] to) {
15554359Sroberto            this.exports_index = index;
15654359Sroberto            this.exports_flags = flags;
15754359Sroberto            this.exports_to_count = to.length;
158182007Sroberto            this.exports_to_index = to;
15954359Sroberto        }
16082498Sroberto
16182498Sroberto        public int length() {
16282498Sroberto            return 4 + 2 * exports_to_index.length;
16382498Sroberto        }
164285612Sdelphij
16582498Sroberto        public final int exports_index;
16682498Sroberto        public final int exports_flags;
16754359Sroberto        public final int exports_to_count;
168285612Sdelphij        public final int[] exports_to_index;
16954359Sroberto    }
17054359Sroberto
17156746Sroberto    public static class OpensEntry {
17256746Sroberto        OpensEntry(ClassReader cr) throws IOException {
17356746Sroberto            opens_index = cr.readUnsignedShort();
17456746Sroberto            opens_flags = cr.readUnsignedShort();
17556746Sroberto            opens_to_count = cr.readUnsignedShort();
176182007Sroberto            opens_to_index = new int[opens_to_count];
17756746Sroberto            for (int i = 0; i < opens_to_count; i++)
17856746Sroberto                opens_to_index[i] = cr.readUnsignedShort();
17954359Sroberto        }
18054359Sroberto
181182007Sroberto        public OpensEntry(int index, int flags, int[] to) {
18254359Sroberto            this.opens_index = index;
18354359Sroberto            this.opens_flags = flags;
184285612Sdelphij            this.opens_to_count = to.length;
185285612Sdelphij            this.opens_to_index = to;
186285612Sdelphij        }
187285612Sdelphij
188285612Sdelphij        public int length() {
189285612Sdelphij            return 4 + 2 * opens_to_index.length;
190285612Sdelphij        }
191285612Sdelphij
192285612Sdelphij        public final int opens_index;
193285612Sdelphij        public final int opens_flags;
194285612Sdelphij        public final int opens_to_count;
19556746Sroberto        public final int[] opens_to_index;
19654359Sroberto    }
19754359Sroberto
198132451Sroberto    public static class ProvidesEntry {
199132451Sroberto        ProvidesEntry(ClassReader cr) throws IOException {
200132451Sroberto            provides_index = cr.readUnsignedShort();
20154359Sroberto            with_count = cr.readUnsignedShort();
20254359Sroberto            with_index = new int[with_count];
20382498Sroberto            for (int i = 0; i < with_count; i++)
20454359Sroberto                with_index[i] = cr.readUnsignedShort();
20554359Sroberto        }
20654359Sroberto
20754359Sroberto        public ProvidesEntry(int provides, int[] with) {
20854359Sroberto            this.provides_index = provides;
20954359Sroberto            this.with_count = with.length;
21082498Sroberto            this.with_index = with;
21154359Sroberto        }
21254359Sroberto
21354359Sroberto        public static final int length = 4;
21454359Sroberto
21554359Sroberto        public final int provides_index;
216132451Sroberto        public final int with_count;
217132451Sroberto        public final int[] with_index;
218132451Sroberto    }
219132451Sroberto}
220132451Sroberto