author | vromero |
Fri, 27 May 2016 19:45:57 -0400 | |
changeset 38619 | 27c0007bb28d |
parent 37854 | a76a06106d02 |
child 38827 | 884d32899770 |
permissions | -rw-r--r-- |
36526 | 1 |
/* |
2 |
* Copyright (c) 2009, 2016, 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 |
||
26 |
||
27 |
package com.sun.tools.javac.comp; |
|
28 |
||
29 |
import java.io.IOException; |
|
30 |
import java.util.Arrays; |
|
31 |
import java.util.Collection; |
|
32 |
import java.util.Collections; |
|
33 |
import java.util.EnumSet; |
|
34 |
import java.util.HashMap; |
|
35 |
import java.util.HashSet; |
|
36 |
import java.util.LinkedHashMap; |
|
37 |
import java.util.LinkedHashSet; |
|
38 |
import java.util.Map; |
|
39 |
import java.util.Map.Entry; |
|
40 |
import java.util.Set; |
|
41 |
import java.util.function.Predicate; |
|
42 |
import java.util.regex.Matcher; |
|
43 |
import java.util.regex.Pattern; |
|
44 |
import java.util.stream.Stream; |
|
45 |
||
46 |
import javax.lang.model.SourceVersion; |
|
47 |
import javax.tools.JavaFileManager; |
|
48 |
import javax.tools.JavaFileManager.Location; |
|
49 |
import javax.tools.JavaFileObject; |
|
50 |
import javax.tools.JavaFileObject.Kind; |
|
51 |
import javax.tools.StandardLocation; |
|
52 |
||
53 |
import com.sun.tools.javac.code.Directive; |
|
54 |
import com.sun.tools.javac.code.Directive.ExportsDirective; |
|
55 |
import com.sun.tools.javac.code.Directive.RequiresDirective; |
|
56 |
import com.sun.tools.javac.code.Directive.RequiresFlag; |
|
57 |
import com.sun.tools.javac.code.Directive.UsesDirective; |
|
58 |
import com.sun.tools.javac.code.Flags; |
|
59 |
import com.sun.tools.javac.code.Kinds; |
|
60 |
import com.sun.tools.javac.code.ModuleFinder; |
|
61 |
import com.sun.tools.javac.code.Source; |
|
62 |
import com.sun.tools.javac.code.Symbol; |
|
63 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
64 |
import com.sun.tools.javac.code.Symbol.Completer; |
|
65 |
import com.sun.tools.javac.code.Symbol.CompletionFailure; |
|
66 |
import com.sun.tools.javac.code.Symbol.MethodSymbol; |
|
67 |
import com.sun.tools.javac.code.Symbol.ModuleSymbol; |
|
68 |
import com.sun.tools.javac.code.Symbol.PackageSymbol; |
|
69 |
import com.sun.tools.javac.code.Symtab; |
|
70 |
import com.sun.tools.javac.code.Type; |
|
71 |
import com.sun.tools.javac.jvm.ClassWriter; |
|
72 |
import com.sun.tools.javac.jvm.JNIWriter; |
|
73 |
import com.sun.tools.javac.main.Option; |
|
74 |
import com.sun.tools.javac.resources.CompilerProperties.Errors; |
|
75 |
import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
|
76 |
import com.sun.tools.javac.tree.JCTree; |
|
77 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
|
78 |
import com.sun.tools.javac.tree.JCTree.JCExports; |
|
79 |
import com.sun.tools.javac.tree.JCTree.JCExpression; |
|
80 |
import com.sun.tools.javac.tree.JCTree.JCModuleDecl; |
|
81 |
import com.sun.tools.javac.tree.JCTree.JCPackageDecl; |
|
82 |
import com.sun.tools.javac.tree.JCTree.JCProvides; |
|
83 |
import com.sun.tools.javac.tree.JCTree.JCRequires; |
|
84 |
import com.sun.tools.javac.tree.JCTree.JCUses; |
|
85 |
import com.sun.tools.javac.tree.TreeInfo; |
|
86 |
import com.sun.tools.javac.util.Assert; |
|
87 |
import com.sun.tools.javac.util.Context; |
|
88 |
import com.sun.tools.javac.util.JCDiagnostic; |
|
89 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
90 |
import com.sun.tools.javac.util.List; |
|
91 |
import com.sun.tools.javac.util.ListBuffer; |
|
92 |
import com.sun.tools.javac.util.Log; |
|
93 |
import com.sun.tools.javac.util.Name; |
|
94 |
import com.sun.tools.javac.util.Names; |
|
95 |
import com.sun.tools.javac.util.Options; |
|
96 |
||
97 |
import static com.sun.tools.javac.code.Flags.UNATTRIBUTED; |
|
98 |
import static com.sun.tools.javac.code.Kinds.Kind.MDL; |
|
99 |
import static com.sun.tools.javac.code.TypeTag.CLASS; |
|
100 |
||
101 |
import com.sun.tools.javac.tree.JCTree.JCDirective; |
|
102 |
import com.sun.tools.javac.tree.JCTree.Tag; |
|
103 |
||
104 |
import static com.sun.tools.javac.code.Flags.ABSTRACT; |
|
37854
a76a06106d02
8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents:
37848
diff
changeset
|
105 |
import static com.sun.tools.javac.code.Flags.ENUM; |
36526 | 106 |
import static com.sun.tools.javac.code.Flags.PUBLIC; |
107 |
import static com.sun.tools.javac.tree.JCTree.Tag.MODULEDEF; |
|
108 |
||
109 |
/** |
|
110 |
* TODO: fill in |
|
111 |
* |
|
112 |
* <p><b>This is NOT part of any supported API. |
|
113 |
* If you write code that depends on this, you do so at your own risk. |
|
114 |
* This code and its internal interfaces are subject to change or |
|
115 |
* deletion without notice.</b> |
|
116 |
*/ |
|
117 |
public class Modules extends JCTree.Visitor { |
|
118 |
private static final String ALL_SYSTEM = "ALL-SYSTEM"; |
|
119 |
private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH"; |
|
120 |
||
121 |
private final Log log; |
|
122 |
private final Names names; |
|
123 |
private final Symtab syms; |
|
124 |
private final Attr attr; |
|
125 |
private final TypeEnvs typeEnvs; |
|
126 |
private final JavaFileManager fileManager; |
|
127 |
private final ModuleFinder moduleFinder; |
|
128 |
private final boolean allowModules; |
|
129 |
||
130 |
public final boolean multiModuleMode; |
|
131 |
||
132 |
private final String moduleOverride; |
|
133 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
134 |
private final Name java_se; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
135 |
private final Name java_; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
136 |
|
36526 | 137 |
ModuleSymbol defaultModule; |
138 |
||
139 |
private final String addExportsOpt; |
|
140 |
private Map<ModuleSymbol, Set<ExportsDirective>> addExports; |
|
141 |
private final String addReadsOpt; |
|
142 |
private Map<ModuleSymbol, Set<RequiresDirective>> addReads; |
|
143 |
private final String addModsOpt; |
|
144 |
private final String limitModsOpt; |
|
145 |
||
146 |
private Set<ModuleSymbol> rootModules = Collections.emptySet(); |
|
147 |
||
148 |
public static Modules instance(Context context) { |
|
149 |
Modules instance = context.get(Modules.class); |
|
150 |
if (instance == null) |
|
151 |
instance = new Modules(context); |
|
152 |
return instance; |
|
153 |
} |
|
154 |
||
155 |
protected Modules(Context context) { |
|
156 |
context.put(Modules.class, this); |
|
157 |
log = Log.instance(context); |
|
158 |
names = Names.instance(context); |
|
159 |
syms = Symtab.instance(context); |
|
160 |
attr = Attr.instance(context); |
|
161 |
typeEnvs = TypeEnvs.instance(context); |
|
162 |
moduleFinder = ModuleFinder.instance(context); |
|
163 |
fileManager = context.get(JavaFileManager.class); |
|
164 |
allowModules = Source.instance(context).allowModules(); |
|
165 |
Options options = Options.instance(context); |
|
166 |
||
167 |
moduleOverride = options.get(Option.XMODULE); |
|
168 |
||
169 |
multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); |
|
170 |
ClassWriter classWriter = ClassWriter.instance(context); |
|
171 |
classWriter.multiModuleMode = multiModuleMode; |
|
172 |
JNIWriter jniWriter = JNIWriter.instance(context); |
|
173 |
jniWriter.multiModuleMode = multiModuleMode; |
|
174 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
175 |
java_se = names.fromString("java.se"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
176 |
java_ = names.fromString("java."); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
177 |
|
36526 | 178 |
addExportsOpt = options.get(Option.XADDEXPORTS); |
179 |
addReadsOpt = options.get(Option.XADDREADS); |
|
180 |
addModsOpt = options.get(Option.ADDMODS); |
|
181 |
limitModsOpt = options.get(Option.LIMITMODS); |
|
182 |
} |
|
183 |
||
184 |
int depth = -1; |
|
185 |
private void dprintln(String msg) { |
|
186 |
for (int i = 0; i < depth; i++) |
|
187 |
System.err.print(" "); |
|
188 |
System.err.println(msg); |
|
189 |
} |
|
190 |
||
191 |
public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) { |
|
38619 | 192 |
if (!allowModules) { |
36526 | 193 |
for (JCCompilationUnit tree: trees) { |
194 |
tree.modle = syms.noModule; |
|
195 |
} |
|
196 |
defaultModule = syms.noModule; |
|
197 |
return true; |
|
198 |
} |
|
199 |
||
200 |
int startErrors = log.nerrors; |
|
201 |
||
202 |
depth++; |
|
203 |
try { |
|
204 |
// scan trees for module defs |
|
205 |
Set<ModuleSymbol> roots = enterModules(trees, c); |
|
206 |
||
207 |
setCompilationUnitModules(trees, roots); |
|
208 |
||
209 |
if (!roots.isEmpty() && this.rootModules.isEmpty()) { |
|
210 |
this.rootModules = roots; |
|
211 |
allModules(); //ensure errors reported |
|
212 |
} |
|
213 |
||
214 |
for (ModuleSymbol msym: roots) { |
|
215 |
msym.complete(); |
|
216 |
} |
|
217 |
} finally { |
|
218 |
depth--; |
|
219 |
} |
|
220 |
||
221 |
return (log.nerrors == startErrors); |
|
222 |
} |
|
223 |
||
224 |
public Completer getCompleter() { |
|
225 |
return mainCompleter; |
|
226 |
} |
|
227 |
||
228 |
public ModuleSymbol getDefaultModule() { |
|
229 |
return defaultModule; |
|
230 |
} |
|
231 |
||
232 |
private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) { |
|
233 |
Set<ModuleSymbol> modules = new LinkedHashSet<>(); |
|
234 |
for (JCCompilationUnit tree : trees) { |
|
235 |
JavaFileObject prev = log.useSource(tree.sourcefile); |
|
236 |
try { |
|
237 |
enterModule(tree, c, modules); |
|
238 |
} finally { |
|
239 |
log.useSource(prev); |
|
240 |
} |
|
241 |
} |
|
242 |
return modules; |
|
243 |
} |
|
244 |
||
245 |
||
246 |
private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) { |
|
247 |
boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE); |
|
248 |
boolean isModuleDecl = toplevel.defs.nonEmpty() && toplevel.defs.head.hasTag(MODULEDEF); |
|
249 |
if (isModuleInfo && isModuleDecl) { |
|
250 |
JCModuleDecl decl = (JCModuleDecl) toplevel.defs.head; |
|
251 |
Name name = TreeInfo.fullName(decl.qualId); |
|
252 |
ModuleSymbol sym; |
|
253 |
if (c != null) { |
|
254 |
sym = (ModuleSymbol) c.owner; |
|
255 |
if (sym.name == null) { |
|
256 |
//ModuleFinder.findSingleModule creates a stub of a ModuleSymbol without a name, |
|
257 |
//fill the name here after the module-info.java has been parsed |
|
258 |
//also enter the ModuleSymbol among modules: |
|
259 |
syms.enterModule(sym, name); |
|
260 |
} else { |
|
261 |
// TODO: validate name |
|
262 |
} |
|
263 |
} else { |
|
264 |
sym = syms.enterModule(name); |
|
265 |
if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) { |
|
266 |
log.error(decl.pos(), Errors.DuplicateModule(sym)); |
|
267 |
return; |
|
268 |
} |
|
269 |
} |
|
270 |
sym.completer = getSourceCompleter(toplevel); |
|
271 |
sym.module_info.sourcefile = toplevel.sourcefile; |
|
272 |
decl.sym = sym; |
|
273 |
||
274 |
if (multiModuleMode || modules.isEmpty()) { |
|
275 |
modules.add(sym); |
|
276 |
} else { |
|
277 |
log.error(toplevel.pos(), Errors.TooManyModules); |
|
278 |
} |
|
279 |
||
280 |
Env<AttrContext> provisionalEnv = new Env<>(decl, null); |
|
281 |
||
282 |
provisionalEnv.toplevel = toplevel; |
|
283 |
typeEnvs.put(sym, provisionalEnv); |
|
284 |
} else if (isModuleInfo) { |
|
285 |
if (multiModuleMode) { |
|
286 |
JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head; |
|
287 |
log.error(tree.pos(), Errors.ExpectedModule); |
|
288 |
} |
|
289 |
} else if (isModuleDecl) { |
|
290 |
JCTree tree = toplevel.defs.head; |
|
291 |
log.error(tree.pos(), Errors.ModuleDeclSbInModuleInfoJava); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules) { |
|
296 |
// update the module for each compilation unit |
|
297 |
if (multiModuleMode) { |
|
298 |
checkNoAllModulePath(); |
|
299 |
for (JCCompilationUnit tree: trees) { |
|
300 |
if (tree.defs.isEmpty()) { |
|
301 |
tree.modle = syms.unnamedModule; |
|
302 |
continue; |
|
303 |
} |
|
304 |
||
305 |
JavaFileObject prev = log.useSource(tree.sourcefile); |
|
306 |
try { |
|
307 |
Location locn = getModuleLocation(tree); |
|
308 |
if (locn != null) { |
|
309 |
Name name = names.fromString(fileManager.inferModuleName(locn)); |
|
310 |
ModuleSymbol msym; |
|
311 |
if (tree.defs.head.hasTag(MODULEDEF)) { |
|
312 |
JCModuleDecl decl = (JCModuleDecl) tree.defs.head; |
|
313 |
msym = decl.sym; |
|
314 |
if (msym.name != name) { |
|
315 |
log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name)); |
|
316 |
} |
|
317 |
} else { |
|
318 |
msym = syms.enterModule(name); |
|
319 |
} |
|
320 |
if (msym.sourceLocation == null) { |
|
321 |
msym.sourceLocation = locn; |
|
322 |
if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) { |
|
323 |
msym.classLocation = fileManager.getModuleLocation( |
|
324 |
StandardLocation.CLASS_OUTPUT, msym.name.toString()); |
|
325 |
} |
|
326 |
} |
|
327 |
tree.modle = msym; |
|
328 |
rootModules.add(msym); |
|
329 |
} else { |
|
330 |
log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules); |
|
331 |
tree.modle = syms.errModule; |
|
332 |
} |
|
333 |
} catch (IOException e) { |
|
334 |
throw new Error(e); // FIXME |
|
335 |
} finally { |
|
336 |
log.useSource(prev); |
|
337 |
} |
|
338 |
} |
|
339 |
if (syms.unnamedModule.sourceLocation == null) { |
|
340 |
syms.unnamedModule.completer = getUnnamedModuleCompleter(); |
|
341 |
syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH; |
|
342 |
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; |
|
343 |
} |
|
344 |
defaultModule = syms.unnamedModule; |
|
345 |
} else { |
|
346 |
if (defaultModule == null) { |
|
347 |
switch (rootModules.size()) { |
|
348 |
case 0: |
|
349 |
defaultModule = moduleFinder.findSingleModule(); |
|
350 |
if (defaultModule == syms.unnamedModule) { |
|
351 |
if (moduleOverride != null) { |
|
352 |
checkNoAllModulePath(); |
|
353 |
defaultModule = moduleFinder.findModule(names.fromString(moduleOverride)); |
|
354 |
} else { |
|
355 |
// Question: why not do findAllModules and initVisiblePackages here? |
|
356 |
// i.e. body of unnamedModuleCompleter |
|
357 |
defaultModule.completer = getUnnamedModuleCompleter(); |
|
358 |
defaultModule.classLocation = StandardLocation.CLASS_PATH; |
|
359 |
} |
|
360 |
} else { |
|
361 |
checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath); |
|
362 |
checkNoAllModulePath(); |
|
363 |
defaultModule.complete(); |
|
364 |
// Question: why not do completeModule here? |
|
365 |
defaultModule.completer = new Completer() { |
|
366 |
@Override |
|
367 |
public void complete(Symbol sym) throws CompletionFailure { |
|
368 |
completeModule((ModuleSymbol) sym); |
|
369 |
} |
|
370 |
}; |
|
371 |
} |
|
372 |
rootModules.add(defaultModule); |
|
373 |
break; |
|
374 |
case 1: |
|
375 |
checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath); |
|
376 |
checkNoAllModulePath(); |
|
377 |
defaultModule = rootModules.iterator().next(); |
|
378 |
defaultModule.classLocation = StandardLocation.CLASS_OUTPUT; |
|
379 |
break; |
|
380 |
default: |
|
381 |
Assert.error("too many modules"); |
|
382 |
} |
|
383 |
defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; |
|
384 |
} else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) { |
|
385 |
defaultModule.complete(); |
|
386 |
defaultModule.completer = sym -> completeModule((ModuleSymbol) sym); |
|
387 |
} else { |
|
388 |
Assert.check(rootModules.isEmpty()); |
|
389 |
} |
|
390 |
||
391 |
if (defaultModule != syms.unnamedModule) { |
|
392 |
syms.unnamedModule.completer = getUnnamedModuleCompleter(); |
|
393 |
syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH; |
|
394 |
syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; |
|
395 |
} |
|
396 |
||
397 |
for (JCCompilationUnit tree: trees) { |
|
398 |
tree.modle = defaultModule; |
|
399 |
} |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
private Location getModuleLocation(JCCompilationUnit tree) throws IOException { |
|
404 |
switch (tree.defs.head.getTag()) { |
|
405 |
case MODULEDEF: |
|
406 |
return getModuleLocation(tree.sourcefile, null); |
|
407 |
||
408 |
case PACKAGEDEF: |
|
409 |
JCPackageDecl pkg = (JCPackageDecl) tree.defs.head; |
|
410 |
return getModuleLocation(tree.sourcefile, TreeInfo.fullName(pkg.pid)); |
|
411 |
||
412 |
default: |
|
413 |
// code in unnamed module |
|
414 |
return null; |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
private Location getModuleLocation(JavaFileObject fo, Name pkgName) throws IOException { |
|
419 |
// For now, just check module source path. |
|
420 |
// We may want to check source path as well. |
|
421 |
return fileManager.getModuleLocation(StandardLocation.MODULE_SOURCE_PATH, |
|
422 |
fo, (pkgName == null) ? null : pkgName.toString()); |
|
423 |
} |
|
424 |
||
425 |
private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) { |
|
426 |
if (moduleOverride != null) { |
|
427 |
JavaFileObject prev = log.useSource(trees.head.sourcefile); |
|
428 |
try { |
|
429 |
log.error(trees.head.pos(), error); |
|
430 |
} finally { |
|
431 |
log.useSource(prev); |
|
432 |
} |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
private void checkNoAllModulePath() { |
|
437 |
if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) { |
|
438 |
log.error(Errors.AddmodsAllModulePathInvalid); |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
private final Completer mainCompleter = new Completer() { |
|
443 |
@Override |
|
444 |
public void complete(Symbol sym) throws CompletionFailure { |
|
445 |
ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym); |
|
446 |
||
447 |
if (msym.kind == Kinds.Kind.ERR) { |
|
448 |
log.error(Errors.CantFindModule(msym)); |
|
449 |
//make sure the module is initialized: |
|
450 |
msym.directives = List.nil(); |
|
451 |
msym.exports = List.nil(); |
|
452 |
msym.provides = List.nil(); |
|
453 |
msym.requires = List.nil(); |
|
454 |
msym.uses = List.nil(); |
|
455 |
} else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { |
|
456 |
completeAutomaticModule(msym); |
|
457 |
} else { |
|
458 |
msym.module_info.complete(); |
|
459 |
} |
|
460 |
||
461 |
// If module-info comes from a .java file, the underlying |
|
462 |
// call of classFinder.fillIn will have called through the |
|
463 |
// source completer, to Enter, and then to Modules.enter, |
|
464 |
// which will call completeModule. |
|
465 |
// But, if module-info comes from a .class file, the underlying |
|
466 |
// call of classFinder.fillIn will just call ClassReader to read |
|
467 |
// the .class file, and so we call completeModule here. |
|
468 |
if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) { |
|
469 |
completeModule(msym); |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
@Override |
|
474 |
public String toString() { |
|
475 |
return "mainCompleter"; |
|
476 |
} |
|
477 |
}; |
|
478 |
||
479 |
private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure { |
|
480 |
try { |
|
481 |
ListBuffer<Directive> directives = new ListBuffer<>(); |
|
482 |
ListBuffer<ExportsDirective> exports = new ListBuffer<>(); |
|
483 |
Set<String> seenPackages = new HashSet<>(); |
|
484 |
||
485 |
for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) { |
|
486 |
String binName = fileManager.inferBinaryName(msym.classLocation, clazz); |
|
487 |
String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package???? |
|
488 |
if (seenPackages.add(pack)) { |
|
489 |
ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null); |
|
490 |
directives.add(d); |
|
491 |
exports.add(d); |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
ListBuffer<RequiresDirective> requires = new ListBuffer<>(); |
|
496 |
||
497 |
//ensure all modules are found: |
|
498 |
moduleFinder.findAllModules(); |
|
499 |
||
500 |
for (ModuleSymbol ms : allModules()) { |
|
501 |
if (ms == syms.unnamedModule || ms == msym) |
|
502 |
continue; |
|
503 |
RequiresDirective d = new RequiresDirective(ms, EnumSet.of(RequiresFlag.PUBLIC)); |
|
504 |
directives.add(d); |
|
505 |
requires.add(d); |
|
506 |
} |
|
507 |
||
508 |
RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule); |
|
509 |
directives.add(requiresUnnamed); |
|
510 |
requires.add(requiresUnnamed); |
|
511 |
||
512 |
msym.exports = exports.toList(); |
|
513 |
msym.provides = List.nil(); |
|
514 |
msym.requires = requires.toList(); |
|
515 |
msym.uses = List.nil(); |
|
516 |
msym.directives = directives.toList(); |
|
517 |
msym.flags_field |= Flags.ACYCLIC; |
|
518 |
} catch (IOException ex) { |
|
519 |
throw new IllegalStateException(ex); |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
private Completer getSourceCompleter(JCCompilationUnit tree) { |
|
524 |
return new Completer() { |
|
525 |
@Override |
|
526 |
public void complete(Symbol sym) throws CompletionFailure { |
|
527 |
ModuleSymbol msym = (ModuleSymbol) sym; |
|
528 |
msym.flags_field |= UNATTRIBUTED; |
|
529 |
ModuleVisitor v = new ModuleVisitor(); |
|
530 |
JavaFileObject prev = log.useSource(tree.sourcefile); |
|
531 |
try { |
|
532 |
tree.defs.head.accept(v); |
|
533 |
completeModule(msym); |
|
534 |
checkCyclicDependencies((JCModuleDecl) tree.defs.head); |
|
535 |
} finally { |
|
536 |
log.useSource(prev); |
|
537 |
msym.flags_field &= ~UNATTRIBUTED; |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
@Override |
|
542 |
public String toString() { |
|
543 |
return "SourceCompleter: " + tree.sourcefile.getName(); |
|
544 |
} |
|
545 |
||
546 |
}; |
|
547 |
} |
|
548 |
||
549 |
class ModuleVisitor extends JCTree.Visitor { |
|
550 |
private ModuleSymbol sym; |
|
551 |
private final Set<ModuleSymbol> allRequires = new HashSet<>(); |
|
552 |
private final Set<PackageSymbol> allExports = new HashSet<>(); |
|
553 |
||
554 |
@Override |
|
555 |
public void visitModuleDef(JCModuleDecl tree) { |
|
556 |
sym = Assert.checkNonNull(tree.sym); |
|
557 |
||
558 |
sym.requires = List.nil(); |
|
559 |
sym.exports = List.nil(); |
|
560 |
tree.directives.forEach(t -> t.accept(this)); |
|
561 |
sym.requires = sym.requires.reverse(); |
|
562 |
sym.exports = sym.exports.reverse(); |
|
563 |
ensureJavaBase(); |
|
564 |
} |
|
565 |
||
566 |
@Override |
|
567 |
public void visitRequires(JCRequires tree) { |
|
568 |
ModuleSymbol msym = lookupModule(tree.moduleName); |
|
569 |
if (msym.kind != MDL) { |
|
570 |
log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym)); |
|
571 |
} else if (allRequires.contains(msym)) { |
|
572 |
log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym)); |
|
573 |
} else { |
|
574 |
allRequires.add(msym); |
|
575 |
Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class); |
|
576 |
if (tree.isPublic) |
|
577 |
flags.add(RequiresFlag.PUBLIC); |
|
578 |
RequiresDirective d = new RequiresDirective(msym, flags); |
|
579 |
tree.directive = d; |
|
580 |
sym.requires = sym.requires.prepend(d); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
@Override |
|
585 |
public void visitExports(JCExports tree) { |
|
586 |
Name name = TreeInfo.fullName(tree.qualid); |
|
587 |
PackageSymbol packge = syms.enterPackage(sym, name); |
|
588 |
attr.setPackageSymbols(tree.qualid, packge); |
|
589 |
if (!allExports.add(packge)) { |
|
590 |
log.error(tree.qualid.pos(), Errors.DuplicateExports(packge)); |
|
591 |
} |
|
592 |
||
593 |
List<ModuleSymbol> toModules = null; |
|
594 |
if (tree.moduleNames != null) { |
|
595 |
Set<ModuleSymbol> to = new HashSet<>(); |
|
596 |
for (JCExpression n: tree.moduleNames) { |
|
597 |
ModuleSymbol msym = lookupModule(n); |
|
598 |
if (msym.kind != MDL) { |
|
599 |
log.error(n.pos(), Errors.ModuleNotFound(msym)); |
|
600 |
} else if (!to.add(msym)) { |
|
601 |
log.error(n.pos(), Errors.DuplicateExports(msym)); |
|
602 |
} |
|
603 |
} |
|
604 |
toModules = List.from(to); |
|
605 |
} |
|
606 |
||
607 |
if (toModules == null || !toModules.isEmpty()) { |
|
608 |
ExportsDirective d = new ExportsDirective(packge, toModules); |
|
609 |
tree.directive = d; |
|
610 |
sym.exports = sym.exports.prepend(d); |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
@Override |
|
615 |
public void visitProvides(JCProvides tree) { } |
|
616 |
||
617 |
@Override |
|
618 |
public void visitUses(JCUses tree) { } |
|
619 |
||
620 |
private void ensureJavaBase() { |
|
621 |
if (sym.name == names.java_base) |
|
622 |
return; |
|
623 |
||
624 |
for (RequiresDirective d: sym.requires) { |
|
625 |
if (d.module.name == names.java_base) |
|
626 |
return; |
|
627 |
} |
|
628 |
||
629 |
ModuleSymbol java_base = syms.enterModule(names.java_base); |
|
630 |
Directive.RequiresDirective d = |
|
631 |
new Directive.RequiresDirective(java_base, |
|
632 |
EnumSet.of(Directive.RequiresFlag.MANDATED)); |
|
633 |
sym.requires = sym.requires.prepend(d); |
|
634 |
} |
|
635 |
||
636 |
private ModuleSymbol lookupModule(JCExpression moduleName) { |
|
637 |
try { |
|
638 |
Name name = TreeInfo.fullName(moduleName); |
|
639 |
ModuleSymbol msym = moduleFinder.findModule(name); |
|
640 |
TreeInfo.setSymbol(moduleName, msym); |
|
641 |
return msym; |
|
642 |
} catch (Throwable t) { |
|
643 |
System.err.println("Module " + sym + "; lookup export " + moduleName); |
|
644 |
throw t; |
|
645 |
} |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
public Completer getUsesProvidesCompleter() { |
|
650 |
return sym -> { |
|
651 |
ModuleSymbol msym = (ModuleSymbol) sym; |
|
652 |
Env<AttrContext> env = typeEnvs.get(msym); |
|
653 |
UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env); |
|
654 |
JavaFileObject prev = log.useSource(env.toplevel.sourcefile); |
|
655 |
try { |
|
656 |
env.toplevel.defs.head.accept(v); |
|
657 |
} finally { |
|
658 |
log.useSource(prev); |
|
659 |
} |
|
660 |
}; |
|
661 |
} |
|
662 |
||
663 |
class UsesProvidesVisitor extends JCTree.Visitor { |
|
664 |
private final ModuleSymbol msym; |
|
665 |
private final Env<AttrContext> env; |
|
666 |
||
667 |
private final Set<Directive.UsesDirective> allUses = new HashSet<>(); |
|
668 |
private final Set<Directive.ProvidesDirective> allProvides = new HashSet<>(); |
|
669 |
||
670 |
public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) { |
|
671 |
this.msym = msym; |
|
672 |
this.env = env; |
|
673 |
} |
|
674 |
||
675 |
@Override @SuppressWarnings("unchecked") |
|
676 |
public void visitModuleDef(JCModuleDecl tree) { |
|
677 |
msym.directives = List.nil(); |
|
678 |
msym.provides = List.nil(); |
|
679 |
msym.uses = List.nil(); |
|
680 |
tree.directives.forEach(t -> t.accept(this)); |
|
681 |
msym.directives = msym.directives.reverse(); |
|
682 |
msym.provides = msym.provides.reverse(); |
|
683 |
msym.uses = msym.uses.reverse(); |
|
684 |
||
685 |
if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED)) |
|
686 |
msym.directives = msym.directives.prepend(msym.requires.head); |
|
687 |
||
688 |
msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); |
|
689 |
||
690 |
checkForCorrectness(); |
|
691 |
} |
|
692 |
||
693 |
@Override |
|
694 |
public void visitExports(JCExports tree) { |
|
695 |
if (tree.directive.packge.members().isEmpty()) { |
|
696 |
log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge)); |
|
697 |
} |
|
698 |
msym.directives = msym.directives.prepend(tree.directive); |
|
699 |
} |
|
700 |
||
701 |
MethodSymbol noArgsConstructor(ClassSymbol tsym) { |
|
702 |
for (Symbol sym : tsym.members().getSymbolsByName(names.init)) { |
|
703 |
MethodSymbol mSym = (MethodSymbol)sym; |
|
704 |
if (mSym.params().isEmpty()) { |
|
705 |
return mSym; |
|
706 |
} |
|
707 |
} |
|
708 |
return null; |
|
709 |
} |
|
710 |
||
711 |
Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>(); |
|
712 |
||
713 |
@Override |
|
714 |
public void visitProvides(JCProvides tree) { |
|
715 |
Type st = attr.attribType(tree.serviceName, env, syms.objectType); |
|
716 |
Type it = attr.attribType(tree.implName, env, st); |
|
717 |
ClassSymbol service = (ClassSymbol) st.tsym; |
|
718 |
ClassSymbol impl = (ClassSymbol) it.tsym; |
|
719 |
if ((impl.flags() & ABSTRACT) != 0) { |
|
720 |
log.error(tree.implName.pos(), Errors.ServiceImplementationIsAbstract(impl)); |
|
721 |
} else if (impl.isInner()) { |
|
722 |
log.error(tree.implName.pos(), Errors.ServiceImplementationIsInner(impl)); |
|
723 |
} else if (service.isInner()) { |
|
724 |
log.error(tree.serviceName.pos(), Errors.ServiceDefinitionIsInner(service)); |
|
725 |
} else { |
|
726 |
MethodSymbol constr = noArgsConstructor(impl); |
|
727 |
if (constr == null) { |
|
728 |
log.error(tree.implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl)); |
|
729 |
} else if ((constr.flags() & PUBLIC) == 0) { |
|
730 |
log.error(tree.implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl)); |
|
731 |
} |
|
732 |
} |
|
733 |
if (st.hasTag(CLASS) && it.hasTag(CLASS)) { |
|
734 |
Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impl); |
|
735 |
if (!allProvides.add(d)) { |
|
736 |
log.error(tree.pos(), Errors.DuplicateProvides(service, impl)); |
|
737 |
} |
|
738 |
msym.provides = msym.provides.prepend(d); |
|
739 |
msym.directives = msym.directives.prepend(d); |
|
740 |
directiveToTreeMap.put(d, tree); |
|
741 |
} |
|
742 |
} |
|
743 |
||
744 |
@Override |
|
745 |
public void visitRequires(JCRequires tree) { |
|
746 |
msym.directives = msym.directives.prepend(tree.directive); |
|
747 |
} |
|
748 |
||
749 |
@Override |
|
750 |
public void visitUses(JCUses tree) { |
|
751 |
Type st = attr.attribType(tree.qualid, env, syms.objectType); |
|
37854
a76a06106d02
8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents:
37848
diff
changeset
|
752 |
Symbol sym = TreeInfo.symbol(tree.qualid); |
a76a06106d02
8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents:
37848
diff
changeset
|
753 |
if ((sym.flags() & ENUM) != 0) { |
a76a06106d02
8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents:
37848
diff
changeset
|
754 |
log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym)); |
a76a06106d02
8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents:
37848
diff
changeset
|
755 |
} else if (st.hasTag(CLASS)) { |
36526 | 756 |
ClassSymbol service = (ClassSymbol) st.tsym; |
757 |
Directive.UsesDirective d = new Directive.UsesDirective(service); |
|
758 |
if (!allUses.add(d)) { |
|
759 |
log.error(tree.pos(), Errors.DuplicateUses(service)); |
|
760 |
} |
|
761 |
msym.uses = msym.uses.prepend(d); |
|
762 |
msym.directives = msym.directives.prepend(d); |
|
763 |
} |
|
764 |
} |
|
765 |
||
766 |
private void checkForCorrectness() { |
|
767 |
for (Directive.ProvidesDirective provides : allProvides) { |
|
768 |
JCProvides tree = directiveToTreeMap.get(provides); |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
769 |
/* The implementation must be defined in the same module as the provides directive |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
770 |
* (else, error) |
36526 | 771 |
*/ |
772 |
PackageSymbol implementationDefiningPackage = provides.impl.packge(); |
|
773 |
if (implementationDefiningPackage.modle != msym) { |
|
774 |
log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle)); |
|
775 |
} |
|
776 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
777 |
/* There is no inherent requirement that module that provides a service should actually |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
778 |
* use it itself. However, it is a pointless declaration if the service package is not |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
779 |
* exported and there is no uses for the service. |
36526 | 780 |
*/ |
781 |
PackageSymbol interfaceDeclaringPackage = provides.service.packge(); |
|
782 |
boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym; |
|
783 |
boolean isInterfaceExportedFromAReadableModule = |
|
784 |
msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage; |
|
785 |
if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) { |
|
786 |
// ok the interface is declared in this module. Let's check if it's exported |
|
787 |
boolean warn = true; |
|
788 |
for (ExportsDirective export : msym.exports) { |
|
789 |
if (interfaceDeclaringPackage == export.packge) { |
|
790 |
warn = false; |
|
791 |
break; |
|
792 |
} |
|
793 |
} |
|
794 |
if (warn) { |
|
795 |
for (UsesDirective uses : msym.uses) { |
|
796 |
if (provides.service == uses.service) { |
|
797 |
warn = false; |
|
798 |
break; |
|
799 |
} |
|
800 |
} |
|
801 |
} |
|
802 |
if (warn) { |
|
803 |
log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service)); |
|
804 |
} |
|
805 |
} |
|
806 |
} |
|
807 |
} |
|
808 |
} |
|
809 |
||
810 |
private Set<ModuleSymbol> allModulesCache; |
|
811 |
||
812 |
private Set<ModuleSymbol> allModules() { |
|
813 |
if (allModulesCache != null) |
|
814 |
return allModulesCache; |
|
815 |
||
816 |
Set<ModuleSymbol> observable; |
|
817 |
||
818 |
if (limitModsOpt == null) { |
|
819 |
observable = null; |
|
820 |
} else { |
|
821 |
Set<ModuleSymbol> limitMods = new HashSet<>(); |
|
822 |
for (String limit : limitModsOpt.split(",")) { |
|
823 |
limitMods.add(syms.enterModule(names.fromString(limit))); |
|
824 |
} |
|
825 |
observable = computeTransitiveClosure(limitMods, null); |
|
826 |
observable.addAll(rootModules); |
|
827 |
} |
|
828 |
||
829 |
Predicate<ModuleSymbol> observablePred = sym -> observable == null || observable.contains(sym); |
|
830 |
Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0; |
|
831 |
Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>(); |
|
832 |
||
833 |
if (rootModules.contains(syms.unnamedModule)) { |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
834 |
ModuleSymbol javaSE = syms.getModule(java_se); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
835 |
Predicate<ModuleSymbol> jdkModulePred; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
836 |
|
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
837 |
if (javaSE != null && (observable == null || observable.contains(javaSE))) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
838 |
jdkModulePred = sym -> { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
839 |
sym.complete(); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
840 |
return !sym.name.startsWith(java_) |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
841 |
&& sym.exports.stream().anyMatch(e -> e.modules == null); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
842 |
}; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
843 |
enabledRoot.add(javaSE); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
844 |
} else { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
845 |
jdkModulePred = sym -> true; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
846 |
} |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
847 |
|
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
848 |
for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
849 |
if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym)) { |
36526 | 850 |
enabledRoot.add(sym); |
851 |
} |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
enabledRoot.addAll(rootModules); |
|
856 |
||
857 |
if (addModsOpt != null) { |
|
858 |
for (String added : addModsOpt.split(",")) { |
|
859 |
Stream<ModuleSymbol> modules; |
|
860 |
switch (added) { |
|
861 |
case ALL_SYSTEM: |
|
862 |
modules = syms.getAllModules() |
|
863 |
.stream() |
|
864 |
.filter(systemModulePred.and(observablePred)); |
|
865 |
break; |
|
866 |
case ALL_MODULE_PATH: |
|
867 |
modules = syms.getAllModules() |
|
868 |
.stream() |
|
869 |
.filter(systemModulePred.negate().and(observablePred)); |
|
870 |
break; |
|
871 |
default: |
|
872 |
modules = Stream.of(syms.enterModule(names.fromString(added))); |
|
873 |
break; |
|
874 |
} |
|
875 |
modules.forEach(sym -> { |
|
876 |
enabledRoot.add(sym); |
|
877 |
if (observable != null) |
|
878 |
observable.add(sym); |
|
879 |
}); |
|
880 |
} |
|
881 |
} |
|
882 |
||
883 |
Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable); |
|
884 |
||
885 |
result.add(syms.unnamedModule); |
|
886 |
||
887 |
if (!rootModules.isEmpty()) |
|
888 |
allModulesCache = result; |
|
889 |
||
890 |
return result; |
|
891 |
} |
|
892 |
||
893 |
public void enableAllModules() { |
|
894 |
allModulesCache = new HashSet<>(); |
|
895 |
||
896 |
moduleFinder.findAllModules(); |
|
897 |
||
898 |
for (ModuleSymbol msym : syms.getAllModules()) { |
|
899 |
allModulesCache.add(msym); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
private Set<ModuleSymbol> computeTransitiveClosure(Iterable<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) { |
|
904 |
List<ModuleSymbol> todo = List.nil(); |
|
905 |
||
906 |
for (ModuleSymbol ms : base) { |
|
907 |
todo = todo.prepend(ms); |
|
908 |
} |
|
909 |
||
910 |
Set<ModuleSymbol> result = new LinkedHashSet<>(); |
|
911 |
result.add(syms.java_base); |
|
912 |
||
913 |
while (todo.nonEmpty()) { |
|
914 |
ModuleSymbol current = todo.head; |
|
915 |
todo = todo.tail; |
|
916 |
if (observable != null && !observable.contains(current)) |
|
917 |
continue; |
|
918 |
if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0)) |
|
919 |
continue; |
|
920 |
current.complete(); |
|
921 |
for (RequiresDirective rd : current.requires) { |
|
922 |
todo = todo.prepend(rd.module); |
|
923 |
} |
|
924 |
} |
|
925 |
||
926 |
return result; |
|
927 |
} |
|
928 |
||
929 |
public ModuleSymbol getObservableModule(Name name) { |
|
930 |
ModuleSymbol mod = syms.getModule(name); |
|
931 |
||
932 |
if (allModules().contains(mod)) { |
|
933 |
return mod; |
|
934 |
} |
|
935 |
||
936 |
return null; |
|
937 |
} |
|
938 |
||
939 |
private Completer getUnnamedModuleCompleter() { |
|
940 |
moduleFinder.findAllModules(); |
|
941 |
return new Symbol.Completer() { |
|
942 |
@Override |
|
943 |
public void complete(Symbol sym) throws CompletionFailure { |
|
944 |
ModuleSymbol msym = (ModuleSymbol) sym; |
|
945 |
Set<ModuleSymbol> allModules = allModules(); |
|
946 |
for (ModuleSymbol m : allModules) { |
|
947 |
m.complete(); |
|
948 |
} |
|
949 |
initVisiblePackages(msym, allModules); |
|
950 |
} |
|
951 |
||
952 |
@Override |
|
953 |
public String toString() { |
|
954 |
return "unnamedModule Completer"; |
|
955 |
} |
|
956 |
}; |
|
957 |
} |
|
958 |
||
959 |
private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresPublicCache = new HashMap<>(); |
|
960 |
||
961 |
private void completeModule(ModuleSymbol msym) { |
|
962 |
Assert.checkNonNull(msym.requires); |
|
963 |
||
964 |
initAddReads(); |
|
965 |
||
966 |
msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); |
|
967 |
||
968 |
List<RequiresDirective> requires = msym.requires; |
|
969 |
List<RequiresDirective> previous = null; |
|
970 |
||
971 |
while (requires.nonEmpty()) { |
|
972 |
if (!allModules().contains(requires.head.module)) { |
|
973 |
Env<AttrContext> env = typeEnvs.get(msym); |
|
974 |
if (env != null) { |
|
975 |
JavaFileObject origSource = log.useSource(env.toplevel.sourcefile); |
|
976 |
try { |
|
977 |
log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module)); |
|
978 |
} finally { |
|
979 |
log.useSource(origSource); |
|
980 |
} |
|
981 |
} else { |
|
982 |
Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0); |
|
983 |
} |
|
984 |
if (previous != null) { |
|
985 |
previous.tail = requires.tail; |
|
986 |
} else { |
|
987 |
msym.requires.tail = requires.tail; |
|
988 |
} |
|
989 |
} else { |
|
990 |
previous = requires; |
|
991 |
} |
|
992 |
requires = requires.tail; |
|
993 |
} |
|
994 |
||
995 |
Set<ModuleSymbol> readable = new LinkedHashSet<>(); |
|
996 |
Set<ModuleSymbol> requiresPublic = new HashSet<>(); |
|
997 |
if ((msym.flags() & Flags.AUTOMATIC_MODULE) == 0) { |
|
998 |
for (RequiresDirective d : msym.requires) { |
|
999 |
d.module.complete(); |
|
1000 |
readable.add(d.module); |
|
1001 |
Set<ModuleSymbol> s = retrieveRequiresPublic(d.module); |
|
1002 |
Assert.checkNonNull(s, () -> "no entry in cache for " + d.module); |
|
1003 |
readable.addAll(s); |
|
1004 |
if (d.flags.contains(RequiresFlag.PUBLIC)) { |
|
1005 |
requiresPublic.add(d.module); |
|
1006 |
requiresPublic.addAll(s); |
|
1007 |
} |
|
1008 |
} |
|
1009 |
} else { |
|
1010 |
//the module graph may contain cycles involving automatic modules |
|
1011 |
//handle automatic modules separatelly: |
|
1012 |
Set<ModuleSymbol> s = retrieveRequiresPublic(msym); |
|
1013 |
||
1014 |
readable.addAll(s); |
|
1015 |
requiresPublic.addAll(s); |
|
1016 |
||
1017 |
//ensure the unnamed module is added (it is not requires public): |
|
1018 |
readable.add(syms.unnamedModule); |
|
1019 |
} |
|
1020 |
requiresPublicCache.put(msym, requiresPublic); |
|
1021 |
initVisiblePackages(msym, readable); |
|
1022 |
for (ExportsDirective d: msym.exports) { |
|
1023 |
d.packge.modle = msym; |
|
1024 |
} |
|
1025 |
||
1026 |
} |
|
1027 |
||
1028 |
private Set<ModuleSymbol> retrieveRequiresPublic(ModuleSymbol msym) { |
|
1029 |
Set<ModuleSymbol> requiresPublic = requiresPublicCache.get(msym); |
|
1030 |
||
1031 |
if (requiresPublic == null) { |
|
1032 |
//the module graph may contain cycles involving automatic modules or -XaddReads edges |
|
1033 |
requiresPublic = new HashSet<>(); |
|
1034 |
||
1035 |
Set<ModuleSymbol> seen = new HashSet<>(); |
|
1036 |
List<ModuleSymbol> todo = List.of(msym); |
|
1037 |
||
1038 |
while (todo.nonEmpty()) { |
|
1039 |
ModuleSymbol current = todo.head; |
|
1040 |
todo = todo.tail; |
|
1041 |
if (!seen.add(current)) |
|
1042 |
continue; |
|
1043 |
requiresPublic.add(current); |
|
1044 |
current.complete(); |
|
1045 |
Iterable<? extends RequiresDirective> requires; |
|
1046 |
if (current != syms.unnamedModule) { |
|
1047 |
Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym); |
|
1048 |
requires = current.requires; |
|
1049 |
for (RequiresDirective rd : requires) { |
|
1050 |
if (rd.isPublic()) |
|
1051 |
todo = todo.prepend(rd.module); |
|
1052 |
} |
|
1053 |
} else { |
|
1054 |
for (ModuleSymbol mod : allModules()) { |
|
1055 |
todo = todo.prepend(mod); |
|
1056 |
} |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
requiresPublic.remove(msym); |
|
1061 |
} |
|
1062 |
||
1063 |
return requiresPublic; |
|
1064 |
} |
|
1065 |
||
1066 |
private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) { |
|
1067 |
initAddExports(); |
|
1068 |
||
1069 |
msym.visiblePackages = new LinkedHashMap<>(); |
|
1070 |
||
1071 |
Map<Name, ModuleSymbol> seen = new HashMap<>(); |
|
1072 |
||
1073 |
for (ModuleSymbol rm : readable) { |
|
1074 |
if (rm == syms.unnamedModule) |
|
1075 |
continue; |
|
1076 |
addVisiblePackages(msym, seen, rm, rm.exports); |
|
1077 |
} |
|
1078 |
||
1079 |
for (Entry<ModuleSymbol, Set<ExportsDirective>> addExportsEntry : addExports.entrySet()) |
|
1080 |
addVisiblePackages(msym, seen, addExportsEntry.getKey(), addExportsEntry.getValue()); |
|
1081 |
} |
|
1082 |
||
1083 |
private void addVisiblePackages(ModuleSymbol msym, |
|
1084 |
Map<Name, ModuleSymbol> seenPackages, |
|
1085 |
ModuleSymbol exportsFrom, |
|
1086 |
Collection<ExportsDirective> exports) { |
|
1087 |
for (ExportsDirective d : exports) { |
|
1088 |
if (d.modules == null || d.modules.contains(msym)) { |
|
1089 |
Name packageName = d.packge.fullname; |
|
1090 |
ModuleSymbol previousModule = seenPackages.get(packageName); |
|
1091 |
||
1092 |
if (previousModule != null && previousModule != exportsFrom) { |
|
1093 |
Env<AttrContext> env = typeEnvs.get(msym); |
|
1094 |
JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile) |
|
1095 |
: null; |
|
1096 |
DiagnosticPosition pos = env != null ? env.tree.pos() : null; |
|
1097 |
try { |
|
1098 |
log.error(pos, Errors.PackageClashFromRequires(msym, packageName, |
|
1099 |
previousModule, exportsFrom)); |
|
1100 |
} finally { |
|
1101 |
if (env != null) |
|
1102 |
log.useSource(origSource); |
|
1103 |
} |
|
1104 |
continue; |
|
1105 |
} |
|
1106 |
||
1107 |
seenPackages.put(packageName, exportsFrom); |
|
1108 |
msym.visiblePackages.put(d.packge.fullname, d.packge); |
|
1109 |
} |
|
1110 |
} |
|
1111 |
} |
|
1112 |
||
1113 |
private void initAddExports() { |
|
1114 |
if (addExports != null) |
|
1115 |
return; |
|
1116 |
||
1117 |
addExports = new LinkedHashMap<>(); |
|
1118 |
||
1119 |
if (addExportsOpt == null) |
|
1120 |
return; |
|
1121 |
||
1122 |
// System.err.println("Modules.addExports:\n " + addExportsOpt.replace("\0", "\n ")); |
|
1123 |
||
1124 |
Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)"); |
|
1125 |
for (String s: addExportsOpt.split("\0+")) { |
|
1126 |
if (s.isEmpty()) |
|
1127 |
continue; |
|
1128 |
Matcher em = ep.matcher(s); |
|
1129 |
if (!em.matches()) { |
|
1130 |
continue; |
|
1131 |
} |
|
1132 |
||
1133 |
// Terminology comes from |
|
1134 |
// -XaddExports:module/package=target,... |
|
1135 |
// Compare to |
|
1136 |
// module module { exports package to target, ... } |
|
1137 |
String moduleName = em.group(1); |
|
1138 |
String packageName = em.group(2); |
|
1139 |
String targetNames = em.group(3); |
|
1140 |
||
1141 |
ModuleSymbol msym = syms.enterModule(names.fromString(moduleName)); |
|
1142 |
PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName)); |
|
1143 |
p.modle = msym; // TODO: do we need this? |
|
1144 |
||
1145 |
List<ModuleSymbol> targetModules = List.nil(); |
|
1146 |
for (String toModule : targetNames.split("[ ,]+")) { |
|
1147 |
ModuleSymbol m; |
|
1148 |
if (toModule.equals("ALL-UNNAMED")) { |
|
1149 |
m = syms.unnamedModule; |
|
1150 |
} else { |
|
1151 |
if (!SourceVersion.isName(toModule)) { |
|
1152 |
// TODO: error: invalid module name |
|
1153 |
continue; |
|
1154 |
} |
|
1155 |
m = syms.enterModule(names.fromString(toModule)); |
|
1156 |
} |
|
1157 |
targetModules = targetModules.prepend(m); |
|
1158 |
} |
|
1159 |
||
1160 |
Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>()); |
|
1161 |
ExportsDirective d = new ExportsDirective(p, targetModules); |
|
1162 |
extra.add(d); |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
private void initAddReads() { |
|
1167 |
if (addReads != null) |
|
1168 |
return; |
|
1169 |
||
1170 |
addReads = new LinkedHashMap<>(); |
|
1171 |
||
1172 |
if (addReadsOpt == null) |
|
1173 |
return; |
|
1174 |
||
1175 |
// System.err.println("Modules.addReads:\n " + addReadsOpt.replace("\0", "\n ")); |
|
1176 |
||
1177 |
Pattern rp = Pattern.compile("([^=]+)=(.*)"); |
|
1178 |
for (String s : addReadsOpt.split("\0+")) { |
|
1179 |
if (s.isEmpty()) |
|
1180 |
continue; |
|
1181 |
Matcher rm = rp.matcher(s); |
|
1182 |
if (!rm.matches()) { |
|
1183 |
continue; |
|
1184 |
} |
|
1185 |
||
1186 |
// Terminology comes from |
|
1187 |
// -XaddReads:target-module=source-module,... |
|
1188 |
// Compare to |
|
1189 |
// module target-module { requires source-module; ... } |
|
1190 |
String targetName = rm.group(1); |
|
1191 |
String sources = rm.group(2); |
|
1192 |
||
1193 |
ModuleSymbol msym = syms.enterModule(names.fromString(targetName)); |
|
1194 |
for (String source : sources.split("[ ,]+")) { |
|
1195 |
ModuleSymbol sourceModule; |
|
1196 |
if (source.equals("ALL-UNNAMED")) { |
|
1197 |
sourceModule = syms.unnamedModule; |
|
1198 |
} else { |
|
1199 |
if (!SourceVersion.isName(source)) { |
|
1200 |
// TODO: error: invalid module name |
|
1201 |
continue; |
|
1202 |
} |
|
1203 |
sourceModule = syms.enterModule(names.fromString(source)); |
|
1204 |
} |
|
1205 |
addReads.computeIfAbsent(msym, m -> new HashSet<>()) |
|
1206 |
.add(new RequiresDirective(sourceModule, EnumSet.of(RequiresFlag.EXTRA))); |
|
1207 |
} |
|
1208 |
} |
|
1209 |
} |
|
1210 |
||
1211 |
private void checkCyclicDependencies(JCModuleDecl mod) { |
|
1212 |
for (JCDirective d : mod.directives) { |
|
1213 |
if (!d.hasTag(Tag.REQUIRES)) |
|
1214 |
continue; |
|
1215 |
JCRequires rd = (JCRequires) d; |
|
1216 |
Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>(); |
|
1217 |
List<ModuleSymbol> queue = List.of(rd.directive.module); |
|
1218 |
while (queue.nonEmpty()) { |
|
1219 |
ModuleSymbol current = queue.head; |
|
1220 |
queue = queue.tail; |
|
1221 |
if (!nonSyntheticDeps.add(current)) |
|
1222 |
continue; |
|
1223 |
if ((current.flags() & Flags.ACYCLIC) != 0) |
|
1224 |
continue; |
|
1225 |
current.complete(); |
|
1226 |
Assert.checkNonNull(current.requires, () -> current.toString()); |
|
1227 |
for (RequiresDirective dep : current.requires) { |
|
1228 |
if (!dep.flags.contains(RequiresFlag.EXTRA)) |
|
1229 |
queue = queue.prepend(dep.module); |
|
1230 |
} |
|
1231 |
} |
|
1232 |
if (nonSyntheticDeps.contains(mod.sym)) { |
|
1233 |
log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module)); |
|
1234 |
} |
|
1235 |
mod.sym.flags_field |= Flags.ACYCLIC; |
|
1236 |
} |
|
1237 |
} |
|
1238 |
||
1239 |
// DEBUG |
|
1240 |
private String toString(ModuleSymbol msym) { |
|
1241 |
return msym.name + "[" |
|
1242 |
+ "kind:" + msym.kind + ";" |
|
1243 |
+ "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";" |
|
1244 |
+ "info:" + toString(msym.module_info.sourcefile) + "," |
|
1245 |
+ toString(msym.module_info.classfile) + "," |
|
1246 |
+ msym.module_info.completer |
|
1247 |
+ "]"; |
|
1248 |
} |
|
1249 |
||
1250 |
// DEBUG |
|
1251 |
String toString(Location locn) { |
|
1252 |
return (locn == null) ? "--" : locn.getName(); |
|
1253 |
} |
|
1254 |
||
1255 |
// DEBUG |
|
1256 |
String toString(JavaFileObject fo) { |
|
1257 |
return (fo == null) ? "--" : fo.getName(); |
|
1258 |
} |
|
1259 |
||
1260 |
public void newRound() { |
|
1261 |
} |
|
1262 |
} |