1 /* 2 * Copyright (c) 2009, 2017, 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.Set; 40 import java.util.function.Consumer; 41 import java.util.function.Predicate; 42 import java.util.regex.Matcher; 43 import java.util.regex.Pattern; 44 import java.util.stream.Collectors; 45 import java.util.stream.Stream; 46 47 import javax.lang.model.SourceVersion; 48 import javax.tools.JavaFileManager; 49 import javax.tools.JavaFileManager.Location; 50 import javax.tools.JavaFileObject; 51 import javax.tools.JavaFileObject.Kind; 52 import javax.tools.StandardLocation; 53 54 import com.sun.source.tree.ModuleTree.ModuleKind; 55 import com.sun.tools.javac.code.ClassFinder; 56 import com.sun.tools.javac.code.DeferredLintHandler; 57 import com.sun.tools.javac.code.Directive; 58 import com.sun.tools.javac.code.Directive.ExportsDirective; 59 import com.sun.tools.javac.code.Directive.ExportsFlag; 60 import com.sun.tools.javac.code.Directive.OpensDirective; 61 import com.sun.tools.javac.code.Directive.OpensFlag; 62 import com.sun.tools.javac.code.Directive.RequiresDirective; 63 import com.sun.tools.javac.code.Directive.RequiresFlag; 64 import com.sun.tools.javac.code.Directive.UsesDirective; 65 import com.sun.tools.javac.code.Flags; 66 import com.sun.tools.javac.code.Lint.LintCategory; 67 import com.sun.tools.javac.code.ModuleFinder; 68 import com.sun.tools.javac.code.Source; 69 import com.sun.tools.javac.code.Symbol; 70 import com.sun.tools.javac.code.Symbol.ClassSymbol; 71 import com.sun.tools.javac.code.Symbol.Completer; 72 import com.sun.tools.javac.code.Symbol.CompletionFailure; 73 import com.sun.tools.javac.code.Symbol.MethodSymbol; 74 import com.sun.tools.javac.code.Symbol.ModuleFlags; 75 import com.sun.tools.javac.code.Symbol.ModuleSymbol; 76 import com.sun.tools.javac.code.Symbol.PackageSymbol; 77 import com.sun.tools.javac.code.Symtab; 78 import com.sun.tools.javac.code.Type; 79 import com.sun.tools.javac.code.Types; 80 import com.sun.tools.javac.jvm.ClassWriter; 81 import com.sun.tools.javac.jvm.JNIWriter; 82 import com.sun.tools.javac.main.Option; 83 import com.sun.tools.javac.resources.CompilerProperties.Errors; 84 import com.sun.tools.javac.resources.CompilerProperties.Warnings; 85 import com.sun.tools.javac.tree.JCTree; 86 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; 87 import com.sun.tools.javac.tree.JCTree.JCDirective; 88 import com.sun.tools.javac.tree.JCTree.JCExports; 89 import com.sun.tools.javac.tree.JCTree.JCExpression; 90 import com.sun.tools.javac.tree.JCTree.JCModuleDecl; 91 import com.sun.tools.javac.tree.JCTree.JCOpens; 92 import com.sun.tools.javac.tree.JCTree.JCPackageDecl; 93 import com.sun.tools.javac.tree.JCTree.JCProvides; 94 import com.sun.tools.javac.tree.JCTree.JCRequires; 95 import com.sun.tools.javac.tree.JCTree.JCUses; 96 import com.sun.tools.javac.tree.JCTree.Tag; 97 import com.sun.tools.javac.tree.TreeInfo; 98 import com.sun.tools.javac.util.Abort; 99 import com.sun.tools.javac.util.Assert; 100 import com.sun.tools.javac.util.Context; 101 import com.sun.tools.javac.util.JCDiagnostic; 102 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 103 import com.sun.tools.javac.util.List; 104 import com.sun.tools.javac.util.ListBuffer; 105 import com.sun.tools.javac.util.Log; 106 import com.sun.tools.javac.util.Name; 107 import com.sun.tools.javac.util.Names; 108 import com.sun.tools.javac.util.Options; 109 import com.sun.tools.javac.util.Position; 110 111 import static com.sun.tools.javac.code.Flags.ABSTRACT; 112 import static com.sun.tools.javac.code.Flags.ENUM; 113 import static com.sun.tools.javac.code.Flags.PUBLIC; 114 import static com.sun.tools.javac.code.Flags.UNATTRIBUTED; 115 import static com.sun.tools.javac.code.Kinds.Kind.ERR; 116 import static com.sun.tools.javac.code.Kinds.Kind.MDL; 117 import static com.sun.tools.javac.code.Kinds.Kind.MTH; 118 import com.sun.tools.javac.code.Symbol.ModuleResolutionFlags; 119 import static com.sun.tools.javac.code.TypeTag.CLASS; 120 121 /** 122 * TODO: fill in 123 * 124 * <p><b>This is NOT part of any supported API. 125 * If you write code that depends on this, you do so at your own risk. 126 * This code and its internal interfaces are subject to change or 127 * deletion without notice.</b> 128 */ 129 public class Modules extends JCTree.Visitor { 130 private static final String ALL_SYSTEM = "ALL-SYSTEM"; 131 private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH"; 132 133 private final Log log; 134 private final Names names; 135 private final Symtab syms; 136 private final Attr attr; 137 private final Check chk; 138 private final DeferredLintHandler deferredLintHandler; 139 private final TypeEnvs typeEnvs; 140 private final Types types; 141 private final JavaFileManager fileManager; 142 private final ModuleFinder moduleFinder; 143 private final Source source; 144 private final boolean allowModules; 145 146 public final boolean multiModuleMode; 147 148 private final String legacyModuleOverride; 149 150 private final Name java_se; 151 private final Name java_; 152 153 ModuleSymbol defaultModule; 154 155 private final String addExportsOpt; 156 private Map<ModuleSymbol, Set<ExportsDirective>> addExports; 157 private final String addReadsOpt; 158 private Map<ModuleSymbol, Set<RequiresDirective>> addReads; 159 private final String addModsOpt; 160 private final Set<String> extraAddMods = new HashSet<>(); 161 private final String limitModsOpt; 162 private final Set<String> extraLimitMods = new HashSet<>(); 163 private final String moduleVersionOpt; 164 165 private final boolean lintOptions; 166 167 private Set<ModuleSymbol> rootModules = null; 168 private final Set<ModuleSymbol> warnedMissing = new HashSet<>(); 169 170 public static Modules instance(Context context) { 171 Modules instance = context.get(Modules.class); 172 if (instance == null) 173 instance = new Modules(context); 174 return instance; 175 } 176 177 protected Modules(Context context) { 178 context.put(Modules.class, this); 179 log = Log.instance(context); 180 names = Names.instance(context); 181 syms = Symtab.instance(context); 182 attr = Attr.instance(context); 183 chk = Check.instance(context); 184 deferredLintHandler = DeferredLintHandler.instance(context); 185 typeEnvs = TypeEnvs.instance(context); 186 moduleFinder = ModuleFinder.instance(context); 187 types = Types.instance(context); 188 fileManager = context.get(JavaFileManager.class); 189 source = Source.instance(context); 190 allowModules = source.allowModules(); 191 Options options = Options.instance(context); 192 193 lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); 194 195 legacyModuleOverride = options.get(Option.XMODULE); 196 197 multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); 198 ClassWriter classWriter = ClassWriter.instance(context); 199 classWriter.multiModuleMode = multiModuleMode; 200 JNIWriter jniWriter = JNIWriter.instance(context); 201 jniWriter.multiModuleMode = multiModuleMode; 202 203 java_se = names.fromString("java.se"); 204 java_ = names.fromString("java."); 205 206 addExportsOpt = options.get(Option.ADD_EXPORTS); 207 addReadsOpt = options.get(Option.ADD_READS); 208 addModsOpt = options.get(Option.ADD_MODULES); 209 limitModsOpt = options.get(Option.LIMIT_MODULES); 210 moduleVersionOpt = options.get(Option.MODULE_VERSION); 211 } 212 213 int depth = -1; 214 private void dprintln(String msg) { 215 for (int i = 0; i < depth; i++) 216 System.err.print(" "); 217 System.err.println(msg); 218 } 219 220 public void addExtraAddModules(String... extras) { 221 extraAddMods.addAll(Arrays.asList(extras)); 222 } 223 224 public void addExtraLimitModules(String... extras) { 225 extraLimitMods.addAll(Arrays.asList(extras)); 226 } 227 228 boolean inInitModules; 229 public void initModules(List<JCCompilationUnit> trees) { 230 Assert.check(!inInitModules); 231 try { 232 inInitModules = true; 233 Assert.checkNull(rootModules); 234 enter(trees, modules -> { 235 Assert.checkNull(rootModules); 236 Assert.checkNull(allModules); 237 this.rootModules = modules; 238 setupAllModules(); //initialize the module graph 239 Assert.checkNonNull(allModules); 240 inInitModules = false; 241 }, null); 242 } finally { 243 inInitModules = false; 244 } 245 } 246 247 public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) { 248 Assert.check(rootModules != null || inInitModules || !allowModules); 249 return enter(trees, modules -> {}, c); 250 } 251 252 private boolean enter(List<JCCompilationUnit> trees, Consumer<Set<ModuleSymbol>> init, ClassSymbol c) { 253 if (!allowModules) { 254 for (JCCompilationUnit tree: trees) { 255 tree.modle = syms.noModule; 256 } 257 defaultModule = syms.noModule; 258 return true; 259 } 260 261 int startErrors = log.nerrors; 262 263 depth++; 264 try { 265 // scan trees for module defs 266 Set<ModuleSymbol> roots = enterModules(trees, c); 267 268 setCompilationUnitModules(trees, roots, c); 269 270 init.accept(roots); 271 272 for (ModuleSymbol msym: roots) { 273 msym.complete(); 274 } 275 } catch (CompletionFailure ex) { 276 log.error(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE, Position.NOPOS, "cant.access", ex.sym, ex.getDetailValue()); 277 if (ex instanceof ClassFinder.BadClassFile) throw new Abort(); 278 } finally { 279 depth--; 280 } 281 282 return (log.nerrors == startErrors); 283 } 284 285 public Completer getCompleter() { 286 return mainCompleter; 287 } 288 289 public ModuleSymbol getDefaultModule() { 290 return defaultModule; 291 } 292 293 public boolean modulesInitialized() { 294 return allModules != null; 295 } 296 297 private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) { 298 Set<ModuleSymbol> modules = new LinkedHashSet<>(); 299 for (JCCompilationUnit tree : trees) { 300 JavaFileObject prev = log.useSource(tree.sourcefile); 301 try { 302 enterModule(tree, c, modules); 303 } finally { 304 log.useSource(prev); 305 } 306 } 307 return modules; 308 } 309 310 311 private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) { 312 boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE); 313 boolean isModuleDecl = toplevel.getModuleDecl() != null; 314 if (isModuleDecl) { 315 JCModuleDecl decl = toplevel.getModuleDecl(); 316 if (!isModuleInfo) { 317 log.error(decl.pos(), Errors.ModuleDeclSbInModuleInfoJava); 318 } 319 Name name = TreeInfo.fullName(decl.qualId); 320 ModuleSymbol sym; 321 if (c != null) { 322 sym = (ModuleSymbol) c.owner; 323 Assert.checkNonNull(sym.name); 324 Name treeName = TreeInfo.fullName(decl.qualId); 325 if (sym.name != treeName) { 326 log.error(decl.pos(), Errors.ModuleNameMismatch(name, sym.name)); 327 } 328 } else { 329 sym = syms.enterModule(name); 330 if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) { 331 log.error(decl.pos(), Errors.DuplicateModule(sym)); 332 return; 333 } 334 } 335 sym.completer = getSourceCompleter(toplevel); 336 sym.module_info.sourcefile = toplevel.sourcefile; 337 decl.sym = sym; 338 339 if (multiModuleMode || modules.isEmpty()) { 340 modules.add(sym); 341 } else { 342 log.error(toplevel.pos(), Errors.TooManyModules); 343 } 344 345 Env<AttrContext> provisionalEnv = new Env<>(decl, null); 346 347 provisionalEnv.toplevel = toplevel; 348 typeEnvs.put(sym, provisionalEnv); 349 } else if (isModuleInfo) { 350 if (multiModuleMode) { 351 JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head; 352 log.error(tree.pos(), Errors.ExpectedModule); 353 } 354 } 355 } 356 357 private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules, ClassSymbol c) { 358 // update the module for each compilation unit 359 if (multiModuleMode) { 360 checkNoAllModulePath(); 361 for (JCCompilationUnit tree: trees) { 362 if (tree.defs.isEmpty()) { 363 tree.modle = syms.unnamedModule; 364 continue; 365 } 366 367 JavaFileObject prev = log.useSource(tree.sourcefile); 368 try { 369 Location msplocn = getModuleLocation(tree); 370 Location plocn = fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH) ? 371 fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH, 372 tree.sourcefile, getPackageName(tree)) : 373 null; 374 375 if (msplocn != null) { 376 if (plocn != null) { 377 log.error(tree.pos(), Errors.FilePatchedAndMsp); 378 } 379 Name name = names.fromString(fileManager.inferModuleName(msplocn)); 380 ModuleSymbol msym; 381 JCModuleDecl decl = tree.getModuleDecl(); 382 if (decl != null) { 383 msym = decl.sym; 384 if (msym.name != name) { 385 log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name)); 386 } 387 } else { 388 if (tree.getPackage() == null) { 389 log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules); 390 } 391 msym = syms.enterModule(name); 392 } 393 if (msym.sourceLocation == null) { 394 msym.sourceLocation = msplocn; 395 if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) { 396 msym.classLocation = fileManager.getLocationForModule( 397 StandardLocation.CLASS_OUTPUT, msym.name.toString()); 398 } 399 } 400 tree.modle = msym; 401 rootModules.add(msym); 402 } else if (plocn != null) { 403 Name name = names.fromString(fileManager.inferModuleName(plocn)); 404 ModuleSymbol msym = moduleFinder.findModule(name); 405 tree.modle = msym; 406 rootModules.add(msym); 407 } else if (c != null && c.packge().modle == syms.unnamedModule) { 408 tree.modle = syms.unnamedModule; 409 } else { 410 if (tree.getModuleDecl() != null) { 411 log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath); 412 } else { 413 log.error(tree.pos(), Errors.NotInModuleOnModuleSourcePath); 414 } 415 tree.modle = syms.errModule; 416 } 417 } catch (IOException e) { 418 throw new Error(e); // FIXME 419 } finally { 420 log.useSource(prev); 421 } 422 } 423 if (syms.unnamedModule.sourceLocation == null) { 424 syms.unnamedModule.completer = getUnnamedModuleCompleter(); 425 syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH; 426 syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; 427 } 428 defaultModule = syms.unnamedModule; 429 } else { 430 ModuleSymbol module = null; 431 if (defaultModule == null) { 432 String moduleOverride = singleModuleOverride(trees); 433 switch (rootModules.size()) { 434 case 0: 435 defaultModule = moduleFinder.findSingleModule(); 436 if (defaultModule == syms.unnamedModule) { 437 if (moduleOverride != null) { 438 checkNoAllModulePath(); 439 defaultModule = moduleFinder.findModule(names.fromString(moduleOverride)); 440 if (legacyModuleOverride != null) { 441 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 442 } 443 } else { 444 // Question: why not do findAllModules and initVisiblePackages here? 445 // i.e. body of unnamedModuleCompleter 446 defaultModule.completer = getUnnamedModuleCompleter(); 447 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 448 defaultModule.classLocation = StandardLocation.CLASS_PATH; 449 } 450 } else { 451 checkSpecifiedModule(trees, moduleOverride, Errors.ModuleInfoWithPatchedModuleClasspath); 452 checkNoAllModulePath(); 453 defaultModule.complete(); 454 // Question: why not do completeModule here? 455 defaultModule.completer = sym -> completeModule((ModuleSymbol) sym); 456 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 457 } 458 rootModules.add(defaultModule); 459 break; 460 case 1: 461 checkSpecifiedModule(trees, moduleOverride, Errors.ModuleInfoWithPatchedModuleSourcepath); 462 checkNoAllModulePath(); 463 defaultModule = rootModules.iterator().next(); 464 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 465 defaultModule.classLocation = StandardLocation.CLASS_OUTPUT; 466 break; 467 default: 468 Assert.error("too many modules"); 469 } 470 } else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) { 471 defaultModule.complete(); 472 defaultModule.completer = sym -> completeModule((ModuleSymbol) sym); 473 } else { 474 Assert.check(rootModules.isEmpty()); 475 String moduleOverride = singleModuleOverride(trees); 476 if (moduleOverride != null) { 477 module = moduleFinder.findModule(names.fromString(moduleOverride)); 478 } else { 479 module = defaultModule; 480 } 481 rootModules.add(module); 482 } 483 484 if (defaultModule != syms.unnamedModule) { 485 syms.unnamedModule.completer = getUnnamedModuleCompleter(); 486 syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; 487 } 488 489 if (module == null) { 490 module = defaultModule; 491 } 492 493 for (JCCompilationUnit tree: trees) { 494 tree.modle = module; 495 } 496 } 497 } 498 499 private String singleModuleOverride(List<JCCompilationUnit> trees) { 500 if (!fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) { 501 return legacyModuleOverride; 502 } 503 504 Set<String> override = new LinkedHashSet<>(); 505 for (JCCompilationUnit tree : trees) { 506 JavaFileObject fo = tree.sourcefile; 507 508 try { 509 Location loc = 510 fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH, 511 fo, getPackageName(tree)); 512 513 if (loc != null) { 514 try { 515 override.add(fileManager.inferModuleName(loc)); 516 } catch (IOException ex) { 517 throw new Error(ex); 518 } 519 } 520 } catch (IOException ex) { 521 ex.printStackTrace(); 522 } 523 } 524 525 switch (override.size()) { 526 case 0: return legacyModuleOverride; 527 case 1: return override.iterator().next(); 528 default: 529 log.error(Errors.TooManyPatchedModules(override)); 530 return null; 531 } 532 } 533 534 private String getPackageName(JCCompilationUnit tree) { 535 if (tree.getModuleDecl() != null) { 536 return null; 537 } else { 538 JCPackageDecl pkg = tree.getPackage(); 539 return (pkg == null) ? "" : TreeInfo.fullName(pkg.pid).toString(); 540 } 541 } 542 543 /** 544 * Determine the location for the module on the module source path 545 * or source output directory which contains a given CompilationUnit. 546 * If the source output directory is unset, the class output directory 547 * will be checked instead. 548 * {@code null} is returned if no such module can be found. 549 * @param tree the compilation unit tree 550 * @return the location for the enclosing module 551 * @throws IOException if there is a problem while searching for the module. 552 */ 553 private Location getModuleLocation(JCCompilationUnit tree) throws IOException { 554 String pkgName = getPackageName(tree); 555 JavaFileObject fo = tree.sourcefile; 556 557 Location loc = 558 fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, 559 fo, (pkgName == null) ? null : pkgName); 560 if (loc == null) { 561 Location sourceOutput = fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT) ? 562 StandardLocation.SOURCE_OUTPUT : StandardLocation.CLASS_OUTPUT; 563 loc = 564 fileManager.getLocationForModule(sourceOutput, 565 fo, (pkgName == null) ? null : pkgName); 566 } 567 return loc; 568 } 569 570 private void checkSpecifiedModule(List<JCCompilationUnit> trees, String moduleOverride, JCDiagnostic.Error error) { 571 if (moduleOverride != null) { 572 JavaFileObject prev = log.useSource(trees.head.sourcefile); 573 try { 574 log.error(trees.head.pos(), error); 575 } finally { 576 log.useSource(prev); 577 } 578 } 579 } 580 581 private void checkNoAllModulePath() { 582 if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) { 583 log.error(Errors.AddmodsAllModulePathInvalid); 584 } 585 } 586 587 private final Completer mainCompleter = new Completer() { 588 @Override 589 public void complete(Symbol sym) throws CompletionFailure { 590 ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym); 591 592 if (msym.kind == ERR) { 593 //make sure the module is initialized: 594 msym.directives = List.nil(); 595 msym.exports = List.nil(); 596 msym.provides = List.nil(); 597 msym.requires = List.nil(); 598 msym.uses = List.nil(); 599 } else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { 600 setupAutomaticModule(msym); 601 } else { 602 msym.module_info.complete(); 603 } 604 605 // If module-info comes from a .java file, the underlying 606 // call of classFinder.fillIn will have called through the 607 // source completer, to Enter, and then to Modules.enter, 608 // which will call completeModule. 609 // But, if module-info comes from a .class file, the underlying 610 // call of classFinder.fillIn will just call ClassReader to read 611 // the .class file, and so we call completeModule here. 612 if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) { 613 completeModule(msym); 614 } 615 } 616 617 @Override 618 public String toString() { 619 return "mainCompleter"; 620 } 621 }; 622 623 private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure { 624 try { 625 ListBuffer<Directive> directives = new ListBuffer<>(); 626 ListBuffer<ExportsDirective> exports = new ListBuffer<>(); 627 Set<String> seenPackages = new HashSet<>(); 628 629 for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) { 630 String binName = fileManager.inferBinaryName(msym.classLocation, clazz); 631 String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package???? 632 if (seenPackages.add(pack)) { 633 ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null); 634 //TODO: opens? 635 directives.add(d); 636 exports.add(d); 637 } 638 } 639 640 msym.exports = exports.toList(); 641 msym.provides = List.nil(); 642 msym.requires = List.nil(); 643 msym.uses = List.nil(); 644 msym.directives = directives.toList(); 645 msym.flags_field |= Flags.ACYCLIC; 646 } catch (IOException ex) { 647 throw new IllegalStateException(ex); 648 } 649 } 650 651 private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure { 652 ListBuffer<Directive> directives = new ListBuffer<>(); 653 654 directives.addAll(msym.directives); 655 656 ListBuffer<RequiresDirective> requires = new ListBuffer<>(); 657 658 for (ModuleSymbol ms : allModules()) { 659 if (ms == syms.unnamedModule || ms == msym) 660 continue; 661 Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ? 662 EnumSet.of(RequiresFlag.TRANSITIVE) : EnumSet.noneOf(RequiresFlag.class); 663 RequiresDirective d = new RequiresDirective(ms, flags); 664 directives.add(d); 665 requires.add(d); 666 } 667 668 RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule); 669 directives.add(requiresUnnamed); 670 requires.add(requiresUnnamed); 671 672 msym.requires = requires.toList(); 673 msym.directives = directives.toList(); 674 } 675 676 private Completer getSourceCompleter(JCCompilationUnit tree) { 677 return new Completer() { 678 @Override 679 public void complete(Symbol sym) throws CompletionFailure { 680 ModuleSymbol msym = (ModuleSymbol) sym; 681 msym.flags_field |= UNATTRIBUTED; 682 ModuleVisitor v = new ModuleVisitor(); 683 JavaFileObject prev = log.useSource(tree.sourcefile); 684 JCModuleDecl moduleDecl = tree.getModuleDecl(); 685 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos()); 686 687 try { 688 moduleDecl.accept(v); 689 completeModule(msym); 690 checkCyclicDependencies(moduleDecl); 691 } finally { 692 log.useSource(prev); 693 deferredLintHandler.setPos(prevLintPos); 694 msym.flags_field &= ~UNATTRIBUTED; 695 } 696 } 697 698 @Override 699 public String toString() { 700 return "SourceCompleter: " + tree.sourcefile.getName(); 701 } 702 703 }; 704 } 705 706 public boolean isRootModule(ModuleSymbol module) { 707 Assert.checkNonNull(rootModules); 708 return rootModules.contains(module); 709 } 710 711 class ModuleVisitor extends JCTree.Visitor { 712 private ModuleSymbol sym; 713 private final Set<ModuleSymbol> allRequires = new HashSet<>(); 714 private final Map<PackageSymbol,List<ExportsDirective>> allExports = new HashMap<>(); 715 private final Map<PackageSymbol,List<OpensDirective>> allOpens = new HashMap<>(); 716 717 @Override 718 public void visitModuleDef(JCModuleDecl tree) { 719 sym = Assert.checkNonNull(tree.sym); 720 721 if (tree.getModuleType() == ModuleKind.OPEN) { 722 sym.flags.add(ModuleFlags.OPEN); 723 } 724 sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED); 725 726 sym.requires = List.nil(); 727 sym.exports = List.nil(); 728 sym.opens = List.nil(); 729 tree.directives.forEach(t -> t.accept(this)); 730 sym.requires = sym.requires.reverse(); 731 sym.exports = sym.exports.reverse(); 732 sym.opens = sym.opens.reverse(); 733 ensureJavaBase(); 734 } 735 736 @Override 737 public void visitRequires(JCRequires tree) { 738 ModuleSymbol msym = lookupModule(tree.moduleName); 739 if (msym.kind != MDL) { 740 log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym)); 741 warnedMissing.add(msym); 742 } else if (allRequires.contains(msym)) { 743 log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym)); 744 } else { 745 allRequires.add(msym); 746 Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class); 747 if (tree.isTransitive) 748 flags.add(RequiresFlag.TRANSITIVE); 749 if (tree.isStaticPhase) 750 flags.add(RequiresFlag.STATIC_PHASE); 751 RequiresDirective d = new RequiresDirective(msym, flags); 752 tree.directive = d; 753 sym.requires = sym.requires.prepend(d); 754 } 755 } 756 757 @Override 758 public void visitExports(JCExports tree) { 759 Name name = TreeInfo.fullName(tree.qualid); 760 PackageSymbol packge = syms.enterPackage(sym, name); 761 attr.setPackageSymbols(tree.qualid, packge); 762 763 List<ExportsDirective> exportsForPackage = allExports.computeIfAbsent(packge, p -> List.nil()); 764 for (ExportsDirective d : exportsForPackage) { 765 reportExportsConflict(tree, packge); 766 } 767 768 List<ModuleSymbol> toModules = null; 769 if (tree.moduleNames != null) { 770 Set<ModuleSymbol> to = new LinkedHashSet<>(); 771 for (JCExpression n: tree.moduleNames) { 772 ModuleSymbol msym = lookupModule(n); 773 chk.checkModuleExists(n.pos(), msym); 774 for (ExportsDirective d : exportsForPackage) { 775 checkDuplicateExportsToModule(n, msym, d); 776 } 777 if (!to.add(msym)) { 778 reportExportsConflictToModule(n, msym); 779 } 780 } 781 toModules = List.from(to); 782 } 783 784 if (toModules == null || !toModules.isEmpty()) { 785 Set<ExportsFlag> flags = EnumSet.noneOf(ExportsFlag.class); 786 ExportsDirective d = new ExportsDirective(packge, toModules, flags); 787 sym.exports = sym.exports.prepend(d); 788 tree.directive = d; 789 790 allExports.put(packge, exportsForPackage.prepend(d)); 791 } 792 } 793 794 private void reportExportsConflict(JCExports tree, PackageSymbol packge) { 795 log.error(tree.qualid.pos(), Errors.ConflictingExports(packge)); 796 } 797 798 private void checkDuplicateExportsToModule(JCExpression name, ModuleSymbol msym, 799 ExportsDirective d) { 800 if (d.modules != null) { 801 for (ModuleSymbol other : d.modules) { 802 if (msym == other) { 803 reportExportsConflictToModule(name, msym); 804 } 805 } 806 } 807 } 808 809 private void reportExportsConflictToModule(JCExpression name, ModuleSymbol msym) { 810 log.error(name.pos(), Errors.ConflictingExportsToModule(msym)); 811 } 812 813 @Override 814 public void visitOpens(JCOpens tree) { 815 Name name = TreeInfo.fullName(tree.qualid); 816 PackageSymbol packge = syms.enterPackage(sym, name); 817 attr.setPackageSymbols(tree.qualid, packge); 818 819 if (sym.flags.contains(ModuleFlags.OPEN)) { 820 log.error(tree.pos(), Errors.NoOpensUnlessStrong); 821 } 822 List<OpensDirective> opensForPackage = allOpens.computeIfAbsent(packge, p -> List.nil()); 823 for (OpensDirective d : opensForPackage) { 824 reportOpensConflict(tree, packge); 825 } 826 827 List<ModuleSymbol> toModules = null; 828 if (tree.moduleNames != null) { 829 Set<ModuleSymbol> to = new LinkedHashSet<>(); 830 for (JCExpression n: tree.moduleNames) { 831 ModuleSymbol msym = lookupModule(n); 832 chk.checkModuleExists(n.pos(), msym); 833 for (OpensDirective d : opensForPackage) { 834 checkDuplicateOpensToModule(n, msym, d); 835 } 836 if (!to.add(msym)) { 837 reportOpensConflictToModule(n, msym); 838 } 839 } 840 toModules = List.from(to); 841 } 842 843 if (toModules == null || !toModules.isEmpty()) { 844 Set<OpensFlag> flags = EnumSet.noneOf(OpensFlag.class); 845 OpensDirective d = new OpensDirective(packge, toModules, flags); 846 sym.opens = sym.opens.prepend(d); 847 tree.directive = d; 848 849 allOpens.put(packge, opensForPackage.prepend(d)); 850 } 851 } 852 853 private void reportOpensConflict(JCOpens tree, PackageSymbol packge) { 854 log.error(tree.qualid.pos(), Errors.ConflictingOpens(packge)); 855 } 856 857 private void checkDuplicateOpensToModule(JCExpression name, ModuleSymbol msym, 858 OpensDirective d) { 859 if (d.modules != null) { 860 for (ModuleSymbol other : d.modules) { 861 if (msym == other) { 862 reportOpensConflictToModule(name, msym); 863 } 864 } 865 } 866 } 867 868 private void reportOpensConflictToModule(JCExpression name, ModuleSymbol msym) { 869 log.error(name.pos(), Errors.ConflictingOpensToModule(msym)); 870 } 871 872 @Override 873 public void visitProvides(JCProvides tree) { } 874 875 @Override 876 public void visitUses(JCUses tree) { } 877 878 private void ensureJavaBase() { 879 if (sym.name == names.java_base) 880 return; 881 882 for (RequiresDirective d: sym.requires) { 883 if (d.module.name == names.java_base) 884 return; 885 } 886 887 ModuleSymbol java_base = syms.enterModule(names.java_base); 888 Directive.RequiresDirective d = 889 new Directive.RequiresDirective(java_base, 890 EnumSet.of(Directive.RequiresFlag.MANDATED)); 891 sym.requires = sym.requires.prepend(d); 892 } 893 894 private ModuleSymbol lookupModule(JCExpression moduleName) { 895 Name name = TreeInfo.fullName(moduleName); 896 ModuleSymbol msym = moduleFinder.findModule(name); 897 TreeInfo.setSymbol(moduleName, msym); 898 return msym; 899 } 900 } 901 902 public Completer getUsesProvidesCompleter() { 903 return sym -> { 904 ModuleSymbol msym = (ModuleSymbol) sym; 905 906 msym.complete(); 907 908 Env<AttrContext> env = typeEnvs.get(msym); 909 UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env); 910 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); 911 JCModuleDecl decl = env.toplevel.getModuleDecl(); 912 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos()); 913 914 try { 915 decl.accept(v); 916 } finally { 917 log.useSource(prev); 918 deferredLintHandler.setPos(prevLintPos); 919 } 920 }; 921 } 922 923 class UsesProvidesVisitor extends JCTree.Visitor { 924 private final ModuleSymbol msym; 925 private final Env<AttrContext> env; 926 927 private final Set<ClassSymbol> allUses = new HashSet<>(); 928 private final Map<ClassSymbol, Set<ClassSymbol>> allProvides = new HashMap<>(); 929 930 public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) { 931 this.msym = msym; 932 this.env = env; 933 } 934 935 @Override @SuppressWarnings("unchecked") 936 public void visitModuleDef(JCModuleDecl tree) { 937 msym.directives = List.nil(); 938 msym.provides = List.nil(); 939 msym.uses = List.nil(); 940 tree.directives.forEach(t -> t.accept(this)); 941 msym.directives = msym.directives.reverse(); 942 msym.provides = msym.provides.reverse(); 943 msym.uses = msym.uses.reverse(); 944 945 if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED)) 946 msym.directives = msym.directives.prepend(msym.requires.head); 947 948 msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); 949 950 checkForCorrectness(); 951 } 952 953 @Override 954 public void visitExports(JCExports tree) { 955 if (tree.directive.packge.members().isEmpty()) { 956 log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge)); 957 } 958 msym.directives = msym.directives.prepend(tree.directive); 959 } 960 961 @Override 962 public void visitOpens(JCOpens tree) { 963 chk.checkPackageExistsForOpens(tree.qualid, tree.directive.packge); 964 msym.directives = msym.directives.prepend(tree.directive); 965 } 966 967 MethodSymbol noArgsConstructor(ClassSymbol tsym) { 968 for (Symbol sym : tsym.members().getSymbolsByName(names.init)) { 969 MethodSymbol mSym = (MethodSymbol)sym; 970 if (mSym.params().isEmpty()) { 971 return mSym; 972 } 973 } 974 return null; 975 } 976 977 MethodSymbol factoryMethod(ClassSymbol tsym) { 978 for (Symbol sym : tsym.members().getSymbolsByName(names.provider, sym -> sym.kind == MTH)) { 979 MethodSymbol mSym = (MethodSymbol)sym; 980 if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) { 981 return mSym; 982 } 983 } 984 return null; 985 } 986 987 Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>(); 988 989 @Override 990 public void visitProvides(JCProvides tree) { 991 Type st = attr.attribType(tree.serviceName, env, syms.objectType); 992 ClassSymbol service = (ClassSymbol) st.tsym; 993 if (allProvides.containsKey(service)) { 994 log.error(tree.serviceName.pos(), Errors.RepeatedProvidesForService(service)); 995 } 996 ListBuffer<ClassSymbol> impls = new ListBuffer<>(); 997 for (JCExpression implName : tree.implNames) { 998 Type it = attr.attribType(implName, env, syms.objectType); 999 ClassSymbol impl = (ClassSymbol) it.tsym; 1000 //find provider factory: 1001 MethodSymbol factory = factoryMethod(impl); 1002 if (factory != null) { 1003 Type returnType = factory.type.getReturnType(); 1004 if (!types.isSubtype(returnType, st)) { 1005 log.error(implName.pos(), Errors.ServiceImplementationProviderReturnMustBeSubtypeOfServiceInterface); 1006 } 1007 } else { 1008 if (!types.isSubtype(it, st)) { 1009 log.error(implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface); 1010 } else if ((impl.flags() & ABSTRACT) != 0) { 1011 log.error(implName.pos(), Errors.ServiceImplementationIsAbstract(impl)); 1012 } else if (impl.isInner()) { 1013 log.error(implName.pos(), Errors.ServiceImplementationIsInner(impl)); 1014 } else { 1015 MethodSymbol constr = noArgsConstructor(impl); 1016 if (constr == null) { 1017 log.error(implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl)); 1018 } else if ((constr.flags() & PUBLIC) == 0) { 1019 log.error(implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl)); 1020 } 1021 } 1022 } 1023 if (it.hasTag(CLASS)) { 1024 if (allProvides.computeIfAbsent(service, s -> new HashSet<>()).add(impl)) { 1025 impls.append(impl); 1026 } else { 1027 log.error(implName.pos(), Errors.DuplicateProvides(service, impl)); 1028 } 1029 } 1030 } 1031 if (st.hasTag(CLASS) && !impls.isEmpty()) { 1032 Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impls.toList()); 1033 msym.provides = msym.provides.prepend(d); 1034 msym.directives = msym.directives.prepend(d); 1035 directiveToTreeMap.put(d, tree); 1036 } 1037 } 1038 1039 @Override 1040 public void visitRequires(JCRequires tree) { 1041 if (tree.directive != null && allModules().contains(tree.directive.module)) { 1042 chk.checkDeprecated(tree.moduleName.pos(), msym, tree.directive.module); 1043 msym.directives = msym.directives.prepend(tree.directive); 1044 } 1045 } 1046 1047 @Override 1048 public void visitUses(JCUses tree) { 1049 Type st = attr.attribType(tree.qualid, env, syms.objectType); 1050 Symbol sym = TreeInfo.symbol(tree.qualid); 1051 if ((sym.flags() & ENUM) != 0) { 1052 log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym)); 1053 } else if (st.hasTag(CLASS)) { 1054 ClassSymbol service = (ClassSymbol) st.tsym; 1055 if (allUses.add(service)) { 1056 Directive.UsesDirective d = new Directive.UsesDirective(service); 1057 msym.uses = msym.uses.prepend(d); 1058 msym.directives = msym.directives.prepend(d); 1059 } else { 1060 log.error(tree.pos(), Errors.DuplicateUses(service)); 1061 } 1062 } 1063 } 1064 1065 private void checkForCorrectness() { 1066 for (Directive.ProvidesDirective provides : msym.provides) { 1067 JCProvides tree = directiveToTreeMap.get(provides); 1068 for (ClassSymbol impl : provides.impls) { 1069 /* The implementation must be defined in the same module as the provides directive 1070 * (else, error) 1071 */ 1072 PackageSymbol implementationDefiningPackage = impl.packge(); 1073 if (implementationDefiningPackage.modle != msym) { 1074 // TODO: should use tree for the implentation name, not the entire provides tree 1075 // TODO: should improve error message to identify the implementation type 1076 log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle)); 1077 } 1078 1079 /* There is no inherent requirement that module that provides a service should actually 1080 * use it itself. However, it is a pointless declaration if the service package is not 1081 * exported and there is no uses for the service. 1082 */ 1083 PackageSymbol interfaceDeclaringPackage = provides.service.packge(); 1084 boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym; 1085 boolean isInterfaceExportedFromAReadableModule = 1086 msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage; 1087 if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) { 1088 // ok the interface is declared in this module. Let's check if it's exported 1089 boolean warn = true; 1090 for (ExportsDirective export : msym.exports) { 1091 if (interfaceDeclaringPackage == export.packge) { 1092 warn = false; 1093 break; 1094 } 1095 } 1096 if (warn) { 1097 for (UsesDirective uses : msym.uses) { 1098 if (provides.service == uses.service) { 1099 warn = false; 1100 break; 1101 } 1102 } 1103 } 1104 if (warn) { 1105 log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service)); 1106 } 1107 } 1108 } 1109 } 1110 } 1111 } 1112 1113 private Set<ModuleSymbol> allModules; 1114 1115 public Set<ModuleSymbol> allModules() { 1116 Assert.checkNonNull(allModules); 1117 return allModules; 1118 } 1119 1120 private void setupAllModules() { 1121 Assert.checkNonNull(rootModules); 1122 Assert.checkNull(allModules); 1123 1124 Set<ModuleSymbol> observable; 1125 1126 if (limitModsOpt == null && extraLimitMods.isEmpty()) { 1127 observable = null; 1128 } else { 1129 Set<ModuleSymbol> limitMods = new HashSet<>(); 1130 if (limitModsOpt != null) { 1131 for (String limit : limitModsOpt.split(",")) { 1132 if (!isValidName(limit)) 1133 continue; 1134 limitMods.add(syms.enterModule(names.fromString(limit))); 1135 } 1136 } 1137 for (String limit : extraLimitMods) { 1138 limitMods.add(syms.enterModule(names.fromString(limit))); 1139 } 1140 observable = computeTransitiveClosure(limitMods, null); 1141 observable.addAll(rootModules); 1142 if (lintOptions) { 1143 for (ModuleSymbol msym : limitMods) { 1144 if (!observable.contains(msym)) { 1145 log.warning(LintCategory.OPTIONS, 1146 Warnings.ModuleForOptionNotFound(Option.LIMIT_MODULES, msym)); 1147 } 1148 } 1149 } 1150 } 1151 1152 Predicate<ModuleSymbol> observablePred = sym -> 1153 (observable == null) ? (moduleFinder.findModule(sym).kind != ERR) : observable.contains(sym); 1154 Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0; 1155 Predicate<ModuleSymbol> noIncubatorPred = sym -> { 1156 sym.complete(); 1157 return !sym.resolutionFlags.contains(ModuleResolutionFlags.DO_NOT_RESOLVE_BY_DEFAULT); 1158 }; 1159 Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>(); 1160 1161 if (rootModules.contains(syms.unnamedModule)) { 1162 ModuleSymbol javaSE = syms.getModule(java_se); 1163 Predicate<ModuleSymbol> jdkModulePred; 1164 1165 if (javaSE != null && (observable == null || observable.contains(javaSE))) { 1166 jdkModulePred = sym -> { 1167 sym.complete(); 1168 return !sym.name.startsWith(java_) 1169 && sym.exports.stream().anyMatch(e -> e.modules == null); 1170 }; 1171 enabledRoot.add(javaSE); 1172 } else { 1173 jdkModulePred = sym -> true; 1174 } 1175 1176 for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) { 1177 if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym) && noIncubatorPred.test(sym)) { 1178 enabledRoot.add(sym); 1179 } 1180 } 1181 } 1182 1183 enabledRoot.addAll(rootModules); 1184 1185 if (addModsOpt != null || !extraAddMods.isEmpty()) { 1186 Set<String> fullAddMods = new HashSet<>(); 1187 fullAddMods.addAll(extraAddMods); 1188 1189 if (addModsOpt != null) { 1190 fullAddMods.addAll(Arrays.asList(addModsOpt.split(","))); 1191 } 1192 1193 for (String added : fullAddMods) { 1194 Stream<ModuleSymbol> modules; 1195 switch (added) { 1196 case ALL_SYSTEM: 1197 modules = new HashSet<>(syms.getAllModules()) 1198 .stream() 1199 .filter(systemModulePred.and(observablePred).and(noIncubatorPred)); 1200 break; 1201 case ALL_MODULE_PATH: 1202 modules = new HashSet<>(syms.getAllModules()) 1203 .stream() 1204 .filter(systemModulePred.negate().and(observablePred)); 1205 break; 1206 default: 1207 if (!isValidName(added)) 1208 continue; 1209 modules = Stream.of(syms.enterModule(names.fromString(added))); 1210 break; 1211 } 1212 modules.forEach(sym -> { 1213 enabledRoot.add(sym); 1214 if (observable != null) 1215 observable.add(sym); 1216 }); 1217 } 1218 } 1219 1220 Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable); 1221 1222 result.add(syms.unnamedModule); 1223 1224 String incubatingModules = result.stream() 1225 .filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING)) 1226 .map(msym -> msym.name.toString()) 1227 .collect(Collectors.joining(",")); 1228 1229 if (!incubatingModules.isEmpty()) { 1230 log.warning(Warnings.IncubatingModules(incubatingModules)); 1231 } 1232 1233 allModules = result; 1234 1235 //add module versions from options, if any: 1236 if (moduleVersionOpt != null) { 1237 Name version = names.fromString(moduleVersionOpt); 1238 rootModules.forEach(m -> m.version = version); 1239 } 1240 } 1241 1242 public boolean isInModuleGraph(ModuleSymbol msym) { 1243 return allModules == null || allModules.contains(msym); 1244 } 1245 1246 private Set<ModuleSymbol> computeTransitiveClosure(Set<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) { 1247 List<ModuleSymbol> primaryTodo = List.nil(); 1248 List<ModuleSymbol> secondaryTodo = List.nil(); 1249 1250 for (ModuleSymbol ms : base) { 1251 primaryTodo = primaryTodo.prepend(ms); 1252 } 1253 1254 Set<ModuleSymbol> result = new LinkedHashSet<>(); 1255 result.add(syms.java_base); 1256 1257 while (primaryTodo.nonEmpty() || secondaryTodo.nonEmpty()) { 1258 ModuleSymbol current; 1259 boolean isPrimaryTodo; 1260 if (primaryTodo.nonEmpty()) { 1261 current = primaryTodo.head; 1262 primaryTodo = primaryTodo.tail; 1263 isPrimaryTodo = true; 1264 } else { 1265 current = secondaryTodo.head; 1266 secondaryTodo = secondaryTodo.tail; 1267 isPrimaryTodo = false; 1268 } 1269 if (observable != null && !observable.contains(current)) 1270 continue; 1271 if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0)) 1272 continue; 1273 current.complete(); 1274 if (current.kind == ERR && isPrimaryTodo && warnedMissing.add(current)) { 1275 log.error(Errors.ModuleNotFound(current)); 1276 } 1277 for (RequiresDirective rd : current.requires) { 1278 if (rd.module == syms.java_base) continue; 1279 if ((rd.isTransitive() && isPrimaryTodo) || base.contains(current)) { 1280 primaryTodo = primaryTodo.prepend(rd.module); 1281 } else { 1282 secondaryTodo = secondaryTodo.prepend(rd.module); 1283 } 1284 } 1285 } 1286 1287 return result; 1288 } 1289 1290 public ModuleSymbol getObservableModule(Name name) { 1291 ModuleSymbol mod = syms.getModule(name); 1292 1293 if (allModules().contains(mod)) { 1294 return mod; 1295 } 1296 1297 return null; 1298 } 1299 1300 private Completer getUnnamedModuleCompleter() { 1301 moduleFinder.findAllModules(); 1302 return new Symbol.Completer() { 1303 @Override 1304 public void complete(Symbol sym) throws CompletionFailure { 1305 if (inInitModules) { 1306 sym.completer = this; 1307 return ; 1308 } 1309 ModuleSymbol msym = (ModuleSymbol) sym; 1310 Set<ModuleSymbol> allModules = new HashSet<>(allModules()); 1311 allModules.remove(syms.unnamedModule); 1312 for (ModuleSymbol m : allModules) { 1313 m.complete(); 1314 } 1315 initVisiblePackages(msym, allModules); 1316 } 1317 1318 @Override 1319 public String toString() { 1320 return "unnamedModule Completer"; 1321 } 1322 }; 1323 } 1324 1325 private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresTransitiveCache = new HashMap<>(); 1326 1327 private void completeModule(ModuleSymbol msym) { 1328 if (inInitModules) { 1329 msym.completer = sym -> completeModule(msym); 1330 return ; 1331 } 1332 1333 if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { 1334 completeAutomaticModule(msym); 1335 } 1336 1337 Assert.checkNonNull(msym.requires); 1338 1339 initAddReads(); 1340 1341 msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); 1342 1343 List<RequiresDirective> requires = msym.requires; 1344 1345 while (requires.nonEmpty()) { 1346 if (!allModules().contains(requires.head.module)) { 1347 Env<AttrContext> env = typeEnvs.get(msym); 1348 if (env != null) { 1349 JavaFileObject origSource = log.useSource(env.toplevel.sourcefile); 1350 try { 1351 log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module)); 1352 } finally { 1353 log.useSource(origSource); 1354 } 1355 } else { 1356 Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0); 1357 } 1358 msym.requires = List.filter(msym.requires, requires.head); 1359 } 1360 requires = requires.tail; 1361 } 1362 1363 Set<ModuleSymbol> readable = new LinkedHashSet<>(); 1364 Set<ModuleSymbol> requiresTransitive = new HashSet<>(); 1365 1366 for (RequiresDirective d : msym.requires) { 1367 d.module.complete(); 1368 readable.add(d.module); 1369 Set<ModuleSymbol> s = retrieveRequiresTransitive(d.module); 1370 Assert.checkNonNull(s, () -> "no entry in cache for " + d.module); 1371 readable.addAll(s); 1372 if (d.flags.contains(RequiresFlag.TRANSITIVE)) { 1373 requiresTransitive.add(d.module); 1374 requiresTransitive.addAll(s); 1375 } 1376 } 1377 1378 requiresTransitiveCache.put(msym, requiresTransitive); 1379 initVisiblePackages(msym, readable); 1380 for (ExportsDirective d: msym.exports) { 1381 if (d.packge != null) { 1382 d.packge.modle = msym; 1383 } 1384 } 1385 1386 } 1387 1388 private Set<ModuleSymbol> retrieveRequiresTransitive(ModuleSymbol msym) { 1389 Set<ModuleSymbol> requiresTransitive = requiresTransitiveCache.get(msym); 1390 1391 if (requiresTransitive == null) { 1392 //the module graph may contain cycles involving automatic modules or --add-reads edges 1393 requiresTransitive = new HashSet<>(); 1394 1395 Set<ModuleSymbol> seen = new HashSet<>(); 1396 List<ModuleSymbol> todo = List.of(msym); 1397 1398 while (todo.nonEmpty()) { 1399 ModuleSymbol current = todo.head; 1400 todo = todo.tail; 1401 if (!seen.add(current)) 1402 continue; 1403 requiresTransitive.add(current); 1404 current.complete(); 1405 Iterable<? extends RequiresDirective> requires; 1406 if (current != syms.unnamedModule) { 1407 Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym); 1408 requires = current.requires; 1409 for (RequiresDirective rd : requires) { 1410 if (rd.isTransitive()) 1411 todo = todo.prepend(rd.module); 1412 } 1413 } else { 1414 for (ModuleSymbol mod : allModules()) { 1415 todo = todo.prepend(mod); 1416 } 1417 } 1418 } 1419 1420 requiresTransitive.remove(msym); 1421 } 1422 1423 return requiresTransitive; 1424 } 1425 1426 private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) { 1427 initAddExports(); 1428 1429 msym.visiblePackages = new LinkedHashMap<>(); 1430 msym.readModules = new HashSet<>(readable); 1431 1432 Map<Name, ModuleSymbol> seen = new HashMap<>(); 1433 1434 for (ModuleSymbol rm : readable) { 1435 if (rm == syms.unnamedModule) 1436 continue; 1437 addVisiblePackages(msym, seen, rm, rm.exports); 1438 } 1439 1440 addExports.forEach((exportsFrom, exports) -> { 1441 addVisiblePackages(msym, seen, exportsFrom, exports); 1442 }); 1443 } 1444 1445 private void addVisiblePackages(ModuleSymbol msym, 1446 Map<Name, ModuleSymbol> seenPackages, 1447 ModuleSymbol exportsFrom, 1448 Collection<ExportsDirective> exports) { 1449 for (ExportsDirective d : exports) { 1450 if (d.modules == null || d.modules.contains(msym)) { 1451 Name packageName = d.packge.fullname; 1452 ModuleSymbol previousModule = seenPackages.get(packageName); 1453 1454 if (previousModule != null && previousModule != exportsFrom) { 1455 Env<AttrContext> env = typeEnvs.get(msym); 1456 JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile) 1457 : null; 1458 DiagnosticPosition pos = env != null ? env.tree.pos() : null; 1459 try { 1460 log.error(pos, Errors.PackageClashFromRequires(msym, packageName, 1461 previousModule, exportsFrom)); 1462 } finally { 1463 if (env != null) 1464 log.useSource(origSource); 1465 } 1466 continue; 1467 } 1468 1469 seenPackages.put(packageName, exportsFrom); 1470 msym.visiblePackages.put(d.packge.fullname, d.packge); 1471 } 1472 } 1473 } 1474 1475 private void initAddExports() { 1476 if (addExports != null) 1477 return; 1478 1479 addExports = new LinkedHashMap<>(); 1480 Set<ModuleSymbol> unknownModules = new HashSet<>(); 1481 1482 if (addExportsOpt == null) 1483 return; 1484 1485 Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)"); 1486 for (String s: addExportsOpt.split("\0+")) { 1487 if (s.isEmpty()) 1488 continue; 1489 Matcher em = ep.matcher(s); 1490 if (!em.matches()) { 1491 continue; 1492 } 1493 1494 // Terminology comes from 1495 // --add-exports module/package=target,... 1496 // Compare to 1497 // module module { exports package to target, ... } 1498 String moduleName = em.group(1); 1499 String packageName = em.group(2); 1500 String targetNames = em.group(3); 1501 1502 if (!isValidName(moduleName)) 1503 continue; 1504 1505 ModuleSymbol msym = syms.enterModule(names.fromString(moduleName)); 1506 if (!isKnownModule(msym, unknownModules)) 1507 continue; 1508 1509 if (!isValidName(packageName)) 1510 continue; 1511 PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName)); 1512 p.modle = msym; // TODO: do we need this? 1513 1514 List<ModuleSymbol> targetModules = List.nil(); 1515 for (String toModule : targetNames.split("[ ,]+")) { 1516 ModuleSymbol m; 1517 if (toModule.equals("ALL-UNNAMED")) { 1518 m = syms.unnamedModule; 1519 } else { 1520 if (!isValidName(toModule)) 1521 continue; 1522 m = syms.enterModule(names.fromString(toModule)); 1523 if (!isKnownModule(m, unknownModules)) 1524 continue; 1525 } 1526 targetModules = targetModules.prepend(m); 1527 } 1528 1529 Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>()); 1530 ExportsDirective d = new ExportsDirective(p, targetModules); 1531 extra.add(d); 1532 } 1533 } 1534 1535 private boolean isKnownModule(ModuleSymbol msym, Set<ModuleSymbol> unknownModules) { 1536 if (allModules.contains(msym)) { 1537 return true; 1538 } 1539 1540 if (!unknownModules.contains(msym)) { 1541 if (lintOptions) { 1542 log.warning(LintCategory.OPTIONS, 1543 Warnings.ModuleForOptionNotFound(Option.ADD_EXPORTS, msym)); 1544 } 1545 unknownModules.add(msym); 1546 } 1547 return false; 1548 } 1549 1550 private void initAddReads() { 1551 if (addReads != null) 1552 return; 1553 1554 addReads = new LinkedHashMap<>(); 1555 1556 if (addReadsOpt == null) 1557 return; 1558 1559 Pattern rp = Pattern.compile("([^=]+)=(.*)"); 1560 for (String s : addReadsOpt.split("\0+")) { 1561 if (s.isEmpty()) 1562 continue; 1563 Matcher rm = rp.matcher(s); 1564 if (!rm.matches()) { 1565 continue; 1566 } 1567 1568 // Terminology comes from 1569 // --add-reads source-module=target-module,... 1570 // Compare to 1571 // module source-module { requires target-module; ... } 1572 String sourceName = rm.group(1); 1573 String targetNames = rm.group(2); 1574 1575 if (!isValidName(sourceName)) 1576 continue; 1577 1578 ModuleSymbol msym = syms.enterModule(names.fromString(sourceName)); 1579 if (!allModules.contains(msym)) { 1580 if (lintOptions) { 1581 log.warning(Warnings.ModuleForOptionNotFound(Option.ADD_READS, msym)); 1582 } 1583 continue; 1584 } 1585 1586 for (String targetName : targetNames.split("[ ,]+", -1)) { 1587 ModuleSymbol targetModule; 1588 if (targetName.equals("ALL-UNNAMED")) { 1589 targetModule = syms.unnamedModule; 1590 } else { 1591 if (!isValidName(targetName)) 1592 continue; 1593 targetModule = syms.enterModule(names.fromString(targetName)); 1594 if (!allModules.contains(targetModule)) { 1595 if (lintOptions) { 1596 log.warning(LintCategory.OPTIONS, Warnings.ModuleForOptionNotFound(Option.ADD_READS, targetModule)); 1597 } 1598 continue; 1599 } 1600 } 1601 addReads.computeIfAbsent(msym, m -> new HashSet<>()) 1602 .add(new RequiresDirective(targetModule, EnumSet.of(RequiresFlag.EXTRA))); 1603 } 1604 } 1605 } 1606 1607 private void checkCyclicDependencies(JCModuleDecl mod) { 1608 for (JCDirective d : mod.directives) { 1609 JCRequires rd; 1610 if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null) 1611 continue; 1612 Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>(); 1613 List<ModuleSymbol> queue = List.of(rd.directive.module); 1614 while (queue.nonEmpty()) { 1615 ModuleSymbol current = queue.head; 1616 queue = queue.tail; 1617 if (!nonSyntheticDeps.add(current)) 1618 continue; 1619 current.complete(); 1620 if ((current.flags() & Flags.ACYCLIC) != 0) 1621 continue; 1622 Assert.checkNonNull(current.requires, current::toString); 1623 for (RequiresDirective dep : current.requires) { 1624 if (!dep.flags.contains(RequiresFlag.EXTRA)) 1625 queue = queue.prepend(dep.module); 1626 } 1627 } 1628 if (nonSyntheticDeps.contains(mod.sym)) { 1629 log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module)); 1630 } 1631 mod.sym.flags_field |= Flags.ACYCLIC; 1632 } 1633 } 1634 1635 private boolean isValidName(CharSequence name) { 1636 return SourceVersion.isName(name, Source.toSourceVersion(source)); 1637 } 1638 1639 // DEBUG 1640 private String toString(ModuleSymbol msym) { 1641 return msym.name + "[" 1642 + "kind:" + msym.kind + ";" 1643 + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";" 1644 + "info:" + toString(msym.module_info.sourcefile) + "," 1645 + toString(msym.module_info.classfile) + "," 1646 + msym.module_info.completer 1647 + "]"; 1648 } 1649 1650 // DEBUG 1651 String toString(Location locn) { 1652 return (locn == null) ? "--" : locn.getName(); 1653 } 1654 1655 // DEBUG 1656 String toString(JavaFileObject fo) { 1657 return (fo == null) ? "--" : fo.getName(); 1658 } 1659 1660 public void newRound() { 1661 allModules = null; 1662 rootModules = null; 1663 warnedMissing.clear(); 1664 } 1665 }