-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathModuleInfo.java
More file actions
178 lines (154 loc) · 5.95 KB
/
ModuleInfo.java
File metadata and controls
178 lines (154 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: Apache-2.0
package org.gradlex.javamodule.moduleinfo;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gradle.api.model.ObjectFactory;
import org.jspecify.annotations.Nullable;
/**
* Data class to hold the information that should be added as module-info.class to an existing Jar file.
*/
@SuppressWarnings("unused")
public class ModuleInfo extends ModuleSpec {
@Nullable
private final String moduleVersion;
boolean openModule = true;
final Map<String, Set<String>> exports = new LinkedHashMap<>();
final Map<String, Set<String>> opens = new LinkedHashMap<>();
final Set<String> requires = new LinkedHashSet<>();
final Set<String> requiresTransitive = new LinkedHashSet<>();
final Set<String> requiresStatic = new LinkedHashSet<>();
final Set<String> requiresStaticTransitive = new LinkedHashSet<>();
final Map<String, Set<String>> ignoreServiceProviders = new LinkedHashMap<>();
final Set<String> uses = new LinkedHashSet<>();
final Set<String> exportAllPackagesExceptions = new LinkedHashSet<>();
boolean exportAllPackages;
boolean requireAllDefinedDependencies;
boolean patchRealModule;
boolean preserveExisting;
ModuleInfo(String identifier, String moduleName, @Nullable String moduleVersion, ObjectFactory objectFactory) {
super(identifier, moduleName);
this.moduleVersion = moduleVersion;
}
/**
* Should this be a 'module' instead of an 'open module'?
*/
public void closeModule() {
openModule = false;
}
/**
* Calling this method at least once automatically makes this a "closed" module: 'module' instead of 'open module'.
*
* @param opens corresponds to the directive in a 'module-info.java' file
* @param to modules this package should be opened to.
*/
public void opens(String opens, String... to) {
closeModule();
addOrThrow(this.opens, opens, to);
}
/**
* @param exports corresponds to the directive in a 'module-info.java' file
* @param to modules this package should be exported to.
*/
public void exports(String exports, String... to) {
addOrThrow(this.exports, exports, to);
}
/**
* @param requires corresponds to the directive in a 'module-info.java' file
*/
public void requires(String requires) {
addOrThrow(this.requires, requires);
}
/**
* @param requiresTransitive corresponds to the directive in a 'module-info.java' file
*/
public void requiresTransitive(String requiresTransitive) {
addOrThrow(this.requiresTransitive, requiresTransitive);
}
/**
* @param requiresStatic corresponds to the directive in a 'module-info.java' file
*/
public void requiresStatic(String requiresStatic) {
addOrThrow(this.requiresStatic, requiresStatic);
}
/**
* @param requiresStaticTransitive corresponds to the directive in a 'module-info.java' file
*/
public void requiresStaticTransitive(String requiresStaticTransitive) {
addOrThrow(this.requiresStaticTransitive, requiresStaticTransitive);
}
/**
* @param uses corresponds to the directive in a 'module-info.java' file
*/
public void uses(String uses) {
addOrThrow(this.uses, uses);
}
/**
* @param provider do not transfer service provider to the 'module-info.class'
* @param implementations the array of specific implementations to skip
*/
public void ignoreServiceProvider(String provider, String... implementations) {
addOrThrow(this.ignoreServiceProviders, provider, implementations);
}
/**
* @return configured version of the Module
*/
@Nullable
public String getModuleVersion() {
return moduleVersion;
}
/**
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
*/
public void exportAllPackages() {
exportAllPackagesExcept(Collections.emptyList());
}
/**
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
* @param exceptions A list of packages not to export
*/
public void exportAllPackagesExcept(String... exceptions) {
exportAllPackagesExcept(Arrays.asList(exceptions));
}
/**
* Automatically export all packages of the Jar. Can be used instead of individual 'exports()' statements.
* @param exceptions A list of packages not to export
*/
public void exportAllPackagesExcept(List<String> exceptions) {
this.exportAllPackages = true;
exportAllPackagesExceptions.addAll(exceptions);
}
/**
* Automatically add 'requires' statements for all dependencies defined in the metadata of the component.
*/
public void requireAllDefinedDependencies() {
this.requireAllDefinedDependencies = true;
}
/**
* Allow patching real (JARs with module-info.class) modules by overriding the existing module-info.class.
*/
public void patchRealModule() {
this.patchRealModule = true;
}
/**
* Allow patching real (JARs with module-info.class) by extending the existing module-info.class.
*/
public void preserveExisting() {
this.patchRealModule = true;
this.preserveExisting = true;
}
private static void addOrThrow(Set<String> target, String element) {
if (!target.add(element)) {
throw new IllegalArgumentException("The element '" + element + "' is already specified");
}
}
private static void addOrThrow(Map<String, Set<String>> target, String key, String... elements) {
if (target.put(key, new LinkedHashSet<>(Arrays.asList(elements))) != null) {
throw new IllegalArgumentException("The element '" + key + "' is already specified");
}
}
}