Module_attribute.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.classfile;
27
28import java.io.IOException;
29
30/**
31 * See Jigsaw.
32 *
33 *  <p><b>This is NOT part of any supported API.
34 *  If you write code that depends on this, you do so at your own risk.
35 *  This code and its internal interfaces are subject to change or
36 *  deletion without notice.</b>
37 */
38public class Module_attribute extends Attribute {
39    public static final int ACC_PUBLIC    =   0x20;
40    public static final int ACC_SYNTHETIC = 0x1000;
41    public static final int ACC_MANDATED  = 0x8000;
42
43    Module_attribute(ClassReader cr, int name_index, int length) throws IOException {
44        super(name_index, length);
45        requires_count = cr.readUnsignedShort();
46        requires = new RequiresEntry[requires_count];
47        for (int i = 0; i < requires_count; i++)
48            requires[i] = new RequiresEntry(cr);
49        exports_count = cr.readUnsignedShort();
50        exports = new ExportsEntry[exports_count];
51        for (int i = 0; i < exports_count; i++)
52            exports[i] = new ExportsEntry(cr);
53        uses_count = cr.readUnsignedShort();
54        uses_index = new int[uses_count];
55        for (int i = 0; i < uses_count; i++)
56            uses_index[i] = cr.readUnsignedShort();
57        provides_count = cr.readUnsignedShort();
58        provides = new ProvidesEntry[provides_count];
59        for (int i = 0; i < provides_count; i++)
60            provides[i] = new ProvidesEntry(cr);
61    }
62
63    public Module_attribute(int name_index,
64            RequiresEntry[] requires,
65            ExportsEntry[] exports,
66            int[] uses,
67            ProvidesEntry[] provides) {
68        super(name_index, 2);
69        requires_count = requires.length;
70        this.requires = requires;
71        exports_count = exports.length;
72        this.exports = exports;
73        uses_count = uses.length;
74        this.uses_index = uses;
75        provides_count = provides.length;
76        this.provides = provides;
77
78    }
79
80    public String getUses(int index, ConstantPool constant_pool) throws ConstantPoolException {
81        int i = uses_index[index];
82        return constant_pool.getClassInfo(i).getName();
83    }
84
85    @Override
86    public <R, D> R accept(Visitor<R, D> visitor, D data) {
87        return visitor.visitModule(this, data);
88    }
89
90    public final int requires_count;
91    public final RequiresEntry[] requires;
92    public final int exports_count;
93    public final ExportsEntry[] exports;
94    public final int uses_count;
95    public final int[] uses_index;
96    public final int provides_count;
97    public final ProvidesEntry[] provides;
98
99    public static class RequiresEntry {
100        RequiresEntry(ClassReader cr) throws IOException {
101            requires_index = cr.readUnsignedShort();
102            requires_flags = cr.readUnsignedShort();
103        }
104
105        public RequiresEntry(int index, int flags) {
106            this.requires_index = index;
107            this.requires_flags = flags;
108        }
109
110        public String getRequires(ConstantPool constant_pool) throws ConstantPoolException {
111            return constant_pool.getUTF8Value(requires_index);
112        }
113
114        public static final int length = 4;
115
116        public final int requires_index;
117        public final int requires_flags;
118    }
119
120    public static class ExportsEntry {
121        ExportsEntry(ClassReader cr) throws IOException {
122            exports_index = cr.readUnsignedShort();
123            exports_to_count = cr.readUnsignedShort();
124            exports_to_index = new int[exports_to_count];
125            for (int i = 0; i < exports_to_count; i++)
126                exports_to_index[i] = cr.readUnsignedShort();
127        }
128
129        public ExportsEntry(int index, int[] to) {
130            this.exports_index = index;
131            this.exports_to_count = to.length;
132            this.exports_to_index = to;
133        }
134
135        public int length() {
136            return 4 + 2 * exports_to_index.length;
137        }
138
139        public final int exports_index;
140        public final int exports_to_count;
141        public final int[] exports_to_index;
142    }
143
144    public static class ProvidesEntry {
145        ProvidesEntry(ClassReader cr) throws IOException {
146            provides_index = cr.readUnsignedShort();
147            with_index = cr.readUnsignedShort();
148        }
149
150        public ProvidesEntry(int provides, int with) {
151            this.provides_index = provides;
152            this.with_index = with;
153        }
154
155        public static final int length = 4;
156
157        public final int provides_index;
158        public final int with_index;
159    }
160}
161