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 moduleOverride; 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 moduleOverride = 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 locn = getModuleLocation(tree); 370 if (locn != null) { 371 Name name = names.fromString(fileManager.inferModuleName(locn)); 372 ModuleSymbol msym; 373 JCModuleDecl decl = tree.getModuleDecl(); 374 if (decl != null) { 375 msym = decl.sym; 376 if (msym.name != name) { 377 log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name)); 378 } 379 } else { 380 if (tree.getPackage() == null) { 381 log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules); 382 } 383 msym = syms.enterModule(name); 384 } 385 if (msym.sourceLocation == null) { 386 msym.sourceLocation = locn; 387 if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) { 388 msym.classLocation = fileManager.getLocationForModule( 389 StandardLocation.CLASS_OUTPUT, msym.name.toString()); 390 } 391 } 392 tree.modle = msym; 393 rootModules.add(msym); 394 } else if (c != null && c.packge().modle == syms.unnamedModule) { 395 tree.modle = syms.unnamedModule; 396 } else { 397 if (tree.getModuleDecl() != null) { 398 log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath); 399 } else { 400 log.error(tree.pos(), Errors.NotInModuleOnModuleSourcePath); 401 } 402 tree.modle = syms.errModule; 403 } 404 } catch (IOException e) { 405 throw new Error(e); // FIXME 406 } finally { 407 log.useSource(prev); 408 } 409 } 410 if (syms.unnamedModule.sourceLocation == null) { 411 syms.unnamedModule.completer = getUnnamedModuleCompleter(); 412 syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH; 413 syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; 414 } 415 defaultModule = syms.unnamedModule; 416 } else { 417 if (defaultModule == null) { 418 switch (rootModules.size()) { 419 case 0: 420 defaultModule = moduleFinder.findSingleModule(); 421 if (defaultModule == syms.unnamedModule) { 422 if (moduleOverride != null) { 423 checkNoAllModulePath(); 424 defaultModule = moduleFinder.findModule(names.fromString(moduleOverride)); 425 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 426 } else { 427 // Question: why not do findAllModules and initVisiblePackages here? 428 // i.e. body of unnamedModuleCompleter 429 defaultModule.completer = getUnnamedModuleCompleter(); 430 defaultModule.classLocation = StandardLocation.CLASS_PATH; 431 } 432 } else { 433 checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath); 434 checkNoAllModulePath(); 435 defaultModule.complete(); 436 // Question: why not do completeModule here? 437 defaultModule.completer = sym -> completeModule((ModuleSymbol) sym); 438 } 439 rootModules.add(defaultModule); 440 break; 441 case 1: 442 checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath); 443 checkNoAllModulePath(); 444 defaultModule = rootModules.iterator().next(); 445 defaultModule.classLocation = StandardLocation.CLASS_OUTPUT; 446 break; 447 default: 448 Assert.error("too many modules"); 449 } 450 defaultModule.sourceLocation = StandardLocation.SOURCE_PATH; 451 } else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) { 452 defaultModule.complete(); 453 defaultModule.completer = sym -> completeModule((ModuleSymbol) sym); 454 } else { 455 Assert.check(rootModules.isEmpty()); 456 rootModules.add(defaultModule); 457 } 458 459 if (defaultModule != syms.unnamedModule) { 460 syms.unnamedModule.completer = getUnnamedModuleCompleter(); 461 syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH; 462 } 463 464 for (JCCompilationUnit tree: trees) { 465 tree.modle = defaultModule; 466 } 467 } 468 } 469 470 /** 471 * Determine the location for the module on the module source path 472 * or source output directory which contains a given CompilationUnit. 473 * If the source output directory is unset, the class output directory 474 * will be checked instead. 475 * {@code null} is returned if no such module can be found. 476 * @param tree the compilation unit tree 477 * @return the location for the enclosing module 478 * @throws IOException if there is a problem while searching for the module. 479 */ 480 private Location getModuleLocation(JCCompilationUnit tree) throws IOException { 481 Name pkgName; 482 if (tree.getModuleDecl() != null) { 483 pkgName = null; 484 } else { 485 JCPackageDecl pkg = tree.getPackage(); 486 pkgName = (pkg == null) ? names.empty : TreeInfo.fullName(pkg.pid); 487 } 488 489 JavaFileObject fo = tree.sourcefile; 490 491 // For now, just check module source path. 492 // We may want to check source path as well. 493 Location loc = 494 fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, 495 fo, (pkgName == null) ? null : pkgName.toString()); 496 if (loc == null) { 497 Location sourceOutput = fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT) ? 498 StandardLocation.SOURCE_OUTPUT : StandardLocation.CLASS_OUTPUT; 499 loc = 500 fileManager.getLocationForModule(sourceOutput, 501 fo, (pkgName == null) ? null : pkgName.toString()); 502 } 503 return loc; 504 } 505 506 private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) { 507 if (moduleOverride != null) { 508 JavaFileObject prev = log.useSource(trees.head.sourcefile); 509 try { 510 log.error(trees.head.pos(), error); 511 } finally { 512 log.useSource(prev); 513 } 514 } 515 } 516 517 private void checkNoAllModulePath() { 518 if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) { 519 log.error(Errors.AddmodsAllModulePathInvalid); 520 } 521 } 522 523 private final Completer mainCompleter = new Completer() { 524 @Override 525 public void complete(Symbol sym) throws CompletionFailure { 526 ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym); 527 528 if (msym.kind == ERR) { 529 //make sure the module is initialized: 530 msym.directives = List.nil(); 531 msym.exports = List.nil(); 532 msym.provides = List.nil(); 533 msym.requires = List.nil(); 534 msym.uses = List.nil(); 535 } else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { 536 setupAutomaticModule(msym); 537 } else { 538 msym.module_info.complete(); 539 } 540 541 // If module-info comes from a .java file, the underlying 542 // call of classFinder.fillIn will have called through the 543 // source completer, to Enter, and then to Modules.enter, 544 // which will call completeModule. 545 // But, if module-info comes from a .class file, the underlying 546 // call of classFinder.fillIn will just call ClassReader to read 547 // the .class file, and so we call completeModule here. 548 if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) { 549 completeModule(msym); 550 } 551 } 552 553 @Override 554 public String toString() { 555 return "mainCompleter"; 556 } 557 }; 558 559 private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure { 560 try { 561 ListBuffer<Directive> directives = new ListBuffer<>(); 562 ListBuffer<ExportsDirective> exports = new ListBuffer<>(); 563 Set<String> seenPackages = new HashSet<>(); 564 565 for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) { 566 String binName = fileManager.inferBinaryName(msym.classLocation, clazz); 567 String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package???? 568 if (seenPackages.add(pack)) { 569 ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null); 570 //TODO: opens? 571 directives.add(d); 572 exports.add(d); 573 } 574 } 575 576 msym.exports = exports.toList(); 577 msym.provides = List.nil(); 578 msym.requires = List.nil(); 579 msym.uses = List.nil(); 580 msym.directives = directives.toList(); 581 msym.flags_field |= Flags.ACYCLIC; 582 } catch (IOException ex) { 583 throw new IllegalStateException(ex); 584 } 585 } 586 587 private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure { 588 ListBuffer<Directive> directives = new ListBuffer<>(); 589 590 directives.addAll(msym.directives); 591 592 ListBuffer<RequiresDirective> requires = new ListBuffer<>(); 593 594 for (ModuleSymbol ms : allModules()) { 595 if (ms == syms.unnamedModule || ms == msym) 596 continue; 597 Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ? 598 EnumSet.of(RequiresFlag.TRANSITIVE) : EnumSet.noneOf(RequiresFlag.class); 599 RequiresDirective d = new RequiresDirective(ms, flags); 600 directives.add(d); 601 requires.add(d); 602 } 603 604 RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule); 605 directives.add(requiresUnnamed); 606 requires.add(requiresUnnamed); 607 608 msym.requires = requires.toList(); 609 msym.directives = directives.toList(); 610 } 611 612 private Completer getSourceCompleter(JCCompilationUnit tree) { 613 return new Completer() { 614 @Override 615 public void complete(Symbol sym) throws CompletionFailure { 616 ModuleSymbol msym = (ModuleSymbol) sym; 617 msym.flags_field |= UNATTRIBUTED; 618 ModuleVisitor v = new ModuleVisitor(); 619 JavaFileObject prev = log.useSource(tree.sourcefile); 620 JCModuleDecl moduleDecl = tree.getModuleDecl(); 621 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos()); 622 623 try { 624 moduleDecl.accept(v); 625 completeModule(msym); 626 checkCyclicDependencies(moduleDecl); 627 } finally { 628 log.useSource(prev); 629 deferredLintHandler.setPos(prevLintPos); 630 msym.flags_field &= ~UNATTRIBUTED; 631 } 632 } 633 634 @Override 635 public String toString() { 636 return "SourceCompleter: " + tree.sourcefile.getName(); 637 } 638 639 }; 640 } 641 642 public boolean isRootModule(ModuleSymbol module) { 643 Assert.checkNonNull(rootModules); 644 return rootModules.contains(module); 645 } 646 647 class ModuleVisitor extends JCTree.Visitor { 648 private ModuleSymbol sym; 649 private final Set<ModuleSymbol> allRequires = new HashSet<>(); 650 private final Map<PackageSymbol,List<ExportsDirective>> allExports = new HashMap<>(); 651 private final Map<PackageSymbol,List<OpensDirective>> allOpens = new HashMap<>(); 652 653 @Override 654 public void visitModuleDef(JCModuleDecl tree) { 655 sym = Assert.checkNonNull(tree.sym); 656 657 if (tree.getModuleType() == ModuleKind.OPEN) { 658 sym.flags.add(ModuleFlags.OPEN); 659 } 660 sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED); 661 662 sym.requires = List.nil(); 663 sym.exports = List.nil(); 664 sym.opens = List.nil(); 665 tree.directives.forEach(t -> t.accept(this)); 666 sym.requires = sym.requires.reverse(); 667 sym.exports = sym.exports.reverse(); 668 sym.opens = sym.opens.reverse(); 669 ensureJavaBase(); 670 } 671 672 @Override 673 public void visitRequires(JCRequires tree) { 674 ModuleSymbol msym = lookupModule(tree.moduleName); 675 if (msym.kind != MDL) { 676 log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym)); 677 warnedMissing.add(msym); 678 } else if (allRequires.contains(msym)) { 679 log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym)); 680 } else { 681 allRequires.add(msym); 682 Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class); 683 if (tree.isTransitive) 684 flags.add(RequiresFlag.TRANSITIVE); 685 if (tree.isStaticPhase) 686 flags.add(RequiresFlag.STATIC_PHASE); 687 RequiresDirective d = new RequiresDirective(msym, flags); 688 tree.directive = d; 689 sym.requires = sym.requires.prepend(d); 690 } 691 } 692 693 @Override 694 public void visitExports(JCExports tree) { 695 Name name = TreeInfo.fullName(tree.qualid); 696 PackageSymbol packge = syms.enterPackage(sym, name); 697 attr.setPackageSymbols(tree.qualid, packge); 698 699 if (tree.hasTag(Tag.OPENS) && sym.flags.contains(ModuleFlags.OPEN)) { 700 log.error(tree.pos(), Errors.NoOpensUnlessStrong); 701 } 702 List<ExportsDirective> exportsForPackage = allExports.computeIfAbsent(packge, p -> List.nil()); 703 for (ExportsDirective d : exportsForPackage) { 704 reportExportsConflict(tree, packge); 705 } 706 707 List<ModuleSymbol> toModules = null; 708 if (tree.moduleNames != null) { 709 Set<ModuleSymbol> to = new LinkedHashSet<>(); 710 for (JCExpression n: tree.moduleNames) { 711 ModuleSymbol msym = lookupModule(n); 712 chk.checkModuleExists(n.pos(), msym); 713 for (ExportsDirective d : exportsForPackage) { 714 checkDuplicateExportsToModule(n, msym, d); 715 } 716 if (!to.add(msym)) { 717 reportExportsConflictToModule(n, msym); 718 } 719 } 720 toModules = List.from(to); 721 } 722 723 if (toModules == null || !toModules.isEmpty()) { 724 Set<ExportsFlag> flags = EnumSet.noneOf(ExportsFlag.class); 725 ExportsDirective d = new ExportsDirective(packge, toModules, flags); 726 sym.exports = sym.exports.prepend(d); 727 tree.directive = d; 728 729 allExports.put(packge, exportsForPackage.prepend(d)); 730 } 731 } 732 733 private void reportExportsConflict(JCExports tree, PackageSymbol packge) { 734 log.error(tree.qualid.pos(), Errors.ConflictingExports(packge)); 735 } 736 737 private void checkDuplicateExportsToModule(JCExpression name, ModuleSymbol msym, 738 ExportsDirective d) { 739 if (d.modules != null) { 740 for (ModuleSymbol other : d.modules) { 741 if (msym == other) { 742 reportExportsConflictToModule(name, msym); 743 } 744 } 745 } 746 } 747 748 private void reportExportsConflictToModule(JCExpression name, ModuleSymbol msym) { 749 log.error(name.pos(), Errors.ConflictingExportsToModule(msym)); 750 } 751 752 @Override 753 public void visitOpens(JCOpens tree) { 754 Name name = TreeInfo.fullName(tree.qualid); 755 PackageSymbol packge = syms.enterPackage(sym, name); 756 attr.setPackageSymbols(tree.qualid, packge); 757 758 if (sym.flags.contains(ModuleFlags.OPEN)) { 759 log.error(tree.pos(), Errors.NoOpensUnlessStrong); 760 } 761 List<OpensDirective> opensForPackage = allOpens.computeIfAbsent(packge, p -> List.nil()); 762 for (OpensDirective d : opensForPackage) { 763 reportOpensConflict(tree, packge); 764 } 765 766 List<ModuleSymbol> toModules = null; 767 if (tree.moduleNames != null) { 768 Set<ModuleSymbol> to = new LinkedHashSet<>(); 769 for (JCExpression n: tree.moduleNames) { 770 ModuleSymbol msym = lookupModule(n); 771 chk.checkModuleExists(n.pos(), msym); 772 for (OpensDirective d : opensForPackage) { 773 checkDuplicateOpensToModule(n, msym, d); 774 } 775 if (!to.add(msym)) { 776 reportOpensConflictToModule(n, msym); 777 } 778 } 779 toModules = List.from(to); 780 } 781 782 if (toModules == null || !toModules.isEmpty()) { 783 Set<OpensFlag> flags = EnumSet.noneOf(OpensFlag.class); 784 OpensDirective d = new OpensDirective(packge, toModules, flags); 785 sym.opens = sym.opens.prepend(d); 786 tree.directive = d; 787 788 allOpens.put(packge, opensForPackage.prepend(d)); 789 } 790 } 791 792 private void reportOpensConflict(JCOpens tree, PackageSymbol packge) { 793 log.error(tree.qualid.pos(), Errors.ConflictingOpens(packge)); 794 } 795 796 private void checkDuplicateOpensToModule(JCExpression name, ModuleSymbol msym, 797 OpensDirective d) { 798 if (d.modules != null) { 799 for (ModuleSymbol other : d.modules) { 800 if (msym == other) { 801 reportOpensConflictToModule(name, msym); 802 } 803 } 804 } 805 } 806 807 private void reportOpensConflictToModule(JCExpression name, ModuleSymbol msym) { 808 log.error(name.pos(), Errors.ConflictingOpensToModule(msym)); 809 } 810 811 @Override 812 public void visitProvides(JCProvides tree) { } 813 814 @Override 815 public void visitUses(JCUses tree) { } 816 817 private void ensureJavaBase() { 818 if (sym.name == names.java_base) 819 return; 820 821 for (RequiresDirective d: sym.requires) { 822 if (d.module.name == names.java_base) 823 return; 824 } 825 826 ModuleSymbol java_base = syms.enterModule(names.java_base); 827 Directive.RequiresDirective d = 828 new Directive.RequiresDirective(java_base, 829 EnumSet.of(Directive.RequiresFlag.MANDATED)); 830 sym.requires = sym.requires.prepend(d); 831 } 832 833 private ModuleSymbol lookupModule(JCExpression moduleName) { 834 Name name = TreeInfo.fullName(moduleName); 835 ModuleSymbol msym = moduleFinder.findModule(name); 836 TreeInfo.setSymbol(moduleName, msym); 837 return msym; 838 } 839 } 840 841 public Completer getUsesProvidesCompleter() { 842 return sym -> { 843 ModuleSymbol msym = (ModuleSymbol) sym; 844 845 msym.complete(); 846 847 Env<AttrContext> env = typeEnvs.get(msym); 848 UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env); 849 JavaFileObject prev = log.useSource(env.toplevel.sourcefile); 850 JCModuleDecl decl = env.toplevel.getModuleDecl(); 851 DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos()); 852 853 try { 854 decl.accept(v); 855 } finally { 856 log.useSource(prev); 857 deferredLintHandler.setPos(prevLintPos); 858 } 859 }; 860 } 861 862 class UsesProvidesVisitor extends JCTree.Visitor { 863 private final ModuleSymbol msym; 864 private final Env<AttrContext> env; 865 866 private final Set<ClassSymbol> allUses = new HashSet<>(); 867 private final Map<ClassSymbol, Set<ClassSymbol>> allProvides = new HashMap<>(); 868 869 public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) { 870 this.msym = msym; 871 this.env = env; 872 } 873 874 @Override @SuppressWarnings("unchecked") 875 public void visitModuleDef(JCModuleDecl tree) { 876 msym.directives = List.nil(); 877 msym.provides = List.nil(); 878 msym.uses = List.nil(); 879 tree.directives.forEach(t -> t.accept(this)); 880 msym.directives = msym.directives.reverse(); 881 msym.provides = msym.provides.reverse(); 882 msym.uses = msym.uses.reverse(); 883 884 if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED)) 885 msym.directives = msym.directives.prepend(msym.requires.head); 886 887 msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); 888 889 checkForCorrectness(); 890 } 891 892 @Override 893 public void visitExports(JCExports tree) { 894 if (tree.directive.packge.members().isEmpty()) { 895 log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge)); 896 } 897 msym.directives = msym.directives.prepend(tree.directive); 898 } 899 900 @Override 901 public void visitOpens(JCOpens tree) { 902 if (tree.directive.packge.members().isEmpty() && 903 ((tree.directive.packge.flags() & Flags.HAS_RESOURCE) == 0)) { 904 log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge)); 905 } 906 msym.directives = msym.directives.prepend(tree.directive); 907 } 908 909 MethodSymbol noArgsConstructor(ClassSymbol tsym) { 910 for (Symbol sym : tsym.members().getSymbolsByName(names.init)) { 911 MethodSymbol mSym = (MethodSymbol)sym; 912 if (mSym.params().isEmpty()) { 913 return mSym; 914 } 915 } 916 return null; 917 } 918 919 MethodSymbol factoryMethod(ClassSymbol tsym) { 920 for (Symbol sym : tsym.members().getSymbolsByName(names.provider, sym -> sym.kind == MTH)) { 921 MethodSymbol mSym = (MethodSymbol)sym; 922 if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) { 923 return mSym; 924 } 925 } 926 return null; 927 } 928 929 Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>(); 930 931 @Override 932 public void visitProvides(JCProvides tree) { 933 Type st = attr.attribType(tree.serviceName, env, syms.objectType); 934 ClassSymbol service = (ClassSymbol) st.tsym; 935 if (allProvides.containsKey(service)) { 936 log.error(tree.serviceName.pos(), Errors.RepeatedProvidesForService(service)); 937 } 938 ListBuffer<ClassSymbol> impls = new ListBuffer<>(); 939 for (JCExpression implName : tree.implNames) { 940 Type it = attr.attribType(implName, env, syms.objectType); 941 ClassSymbol impl = (ClassSymbol) it.tsym; 942 //find provider factory: 943 MethodSymbol factory = factoryMethod(impl); 944 if (factory != null) { 945 Type returnType = factory.type.getReturnType(); 946 if (!types.isSubtype(returnType, st)) { 947 log.error(implName.pos(), Errors.ServiceImplementationProviderReturnMustBeSubtypeOfServiceInterface); 948 } 949 } else { 950 if (!types.isSubtype(it, st)) { 951 log.error(implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface); 952 } else if ((impl.flags() & ABSTRACT) != 0) { 953 log.error(implName.pos(), Errors.ServiceImplementationIsAbstract(impl)); 954 } else if (impl.isInner()) { 955 log.error(implName.pos(), Errors.ServiceImplementationIsInner(impl)); 956 } else { 957 MethodSymbol constr = noArgsConstructor(impl); 958 if (constr == null) { 959 log.error(implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl)); 960 } else if ((constr.flags() & PUBLIC) == 0) { 961 log.error(implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl)); 962 } 963 } 964 } 965 if (it.hasTag(CLASS)) { 966 if (allProvides.computeIfAbsent(service, s -> new HashSet<>()).add(impl)) { 967 impls.append(impl); 968 } else { 969 log.error(implName.pos(), Errors.DuplicateProvides(service, impl)); 970 } 971 } 972 } 973 if (st.hasTag(CLASS) && !impls.isEmpty()) { 974 Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impls.toList()); 975 msym.provides = msym.provides.prepend(d); 976 msym.directives = msym.directives.prepend(d); 977 directiveToTreeMap.put(d, tree); 978 } 979 } 980 981 @Override 982 public void visitRequires(JCRequires tree) { 983 if (tree.directive != null && allModules().contains(tree.directive.module)) { 984 chk.checkDeprecated(tree.moduleName.pos(), msym, tree.directive.module); 985 msym.directives = msym.directives.prepend(tree.directive); 986 } 987 } 988 989 @Override 990 public void visitUses(JCUses tree) { 991 Type st = attr.attribType(tree.qualid, env, syms.objectType); 992 Symbol sym = TreeInfo.symbol(tree.qualid); 993 if ((sym.flags() & ENUM) != 0) { 994 log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym)); 995 } else if (st.hasTag(CLASS)) { 996 ClassSymbol service = (ClassSymbol) st.tsym; 997 if (allUses.add(service)) { 998 Directive.UsesDirective d = new Directive.UsesDirective(service); 999 msym.uses = msym.uses.prepend(d); 1000 msym.directives = msym.directives.prepend(d); 1001 } else { 1002 log.error(tree.pos(), Errors.DuplicateUses(service)); 1003 } 1004 } 1005 } 1006 1007 private void checkForCorrectness() { 1008 for (Directive.ProvidesDirective provides : msym.provides) { 1009 JCProvides tree = directiveToTreeMap.get(provides); 1010 for (ClassSymbol impl : provides.impls) { 1011 /* The implementation must be defined in the same module as the provides directive 1012 * (else, error) 1013 */ 1014 PackageSymbol implementationDefiningPackage = impl.packge(); 1015 if (implementationDefiningPackage.modle != msym) { 1016 // TODO: should use tree for the implentation name, not the entire provides tree 1017 // TODO: should improve error message to identify the implementation type 1018 log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle)); 1019 } 1020 1021 /* There is no inherent requirement that module that provides a service should actually 1022 * use it itself. However, it is a pointless declaration if the service package is not 1023 * exported and there is no uses for the service. 1024 */ 1025 PackageSymbol interfaceDeclaringPackage = provides.service.packge(); 1026 boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym; 1027 boolean isInterfaceExportedFromAReadableModule = 1028 msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage; 1029 if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) { 1030 // ok the interface is declared in this module. Let's check if it's exported 1031 boolean warn = true; 1032 for (ExportsDirective export : msym.exports) { 1033 if (interfaceDeclaringPackage == export.packge) { 1034 warn = false; 1035 break; 1036 } 1037 } 1038 if (warn) { 1039 for (UsesDirective uses : msym.uses) { 1040 if (provides.service == uses.service) { 1041 warn = false; 1042 break; 1043 } 1044 } 1045 } 1046 if (warn) { 1047 log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service)); 1048 } 1049 } 1050 } 1051 } 1052 } 1053 } 1054 1055 private Set<ModuleSymbol> allModules; 1056 1057 public Set<ModuleSymbol> allModules() { 1058 Assert.checkNonNull(allModules); 1059 return allModules; 1060 } 1061 1062 private void setupAllModules() { 1063 Assert.checkNonNull(rootModules); 1064 Assert.checkNull(allModules); 1065 1066 Set<ModuleSymbol> observable; 1067 1068 if (limitModsOpt == null && extraLimitMods.isEmpty()) { 1069 observable = null; 1070 } else { 1071 Set<ModuleSymbol> limitMods = new HashSet<>(); 1072 if (limitModsOpt != null) { 1073 for (String limit : limitModsOpt.split(",")) { 1074 if (!isValidName(limit)) 1075 continue; 1076 limitMods.add(syms.enterModule(names.fromString(limit))); 1077 } 1078 } 1079 for (String limit : extraLimitMods) { 1080 limitMods.add(syms.enterModule(names.fromString(limit))); 1081 } 1082 observable = computeTransitiveClosure(limitMods, null); 1083 observable.addAll(rootModules); 1084 if (lintOptions) { 1085 for (ModuleSymbol msym : limitMods) { 1086 if (!observable.contains(msym)) { 1087 log.warning(LintCategory.OPTIONS, 1088 Warnings.ModuleForOptionNotFound(Option.LIMIT_MODULES, msym)); 1089 } 1090 } 1091 } 1092 } 1093 1094 Predicate<ModuleSymbol> observablePred = sym -> 1095 (observable == null) ? (moduleFinder.findModule(sym).kind != ERR) : observable.contains(sym); 1096 Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0; 1097 Predicate<ModuleSymbol> noIncubatorPred = sym -> { 1098 sym.complete(); 1099 return !sym.resolutionFlags.contains(ModuleResolutionFlags.DO_NOT_RESOLVE_BY_DEFAULT); 1100 }; 1101 Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>(); 1102 1103 if (rootModules.contains(syms.unnamedModule)) { 1104 ModuleSymbol javaSE = syms.getModule(java_se); 1105 Predicate<ModuleSymbol> jdkModulePred; 1106 1107 if (javaSE != null && (observable == null || observable.contains(javaSE))) { 1108 jdkModulePred = sym -> { 1109 sym.complete(); 1110 return !sym.name.startsWith(java_) 1111 && sym.exports.stream().anyMatch(e -> e.modules == null); 1112 }; 1113 enabledRoot.add(javaSE); 1114 } else { 1115 jdkModulePred = sym -> true; 1116 } 1117 1118 for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) { 1119 if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym) && noIncubatorPred.test(sym)) { 1120 enabledRoot.add(sym); 1121 } 1122 } 1123 } 1124 1125 enabledRoot.addAll(rootModules); 1126 1127 if (addModsOpt != null || !extraAddMods.isEmpty()) { 1128 Set<String> fullAddMods = new HashSet<>(); 1129 fullAddMods.addAll(extraAddMods); 1130 1131 if (addModsOpt != null) { 1132 fullAddMods.addAll(Arrays.asList(addModsOpt.split(","))); 1133 } 1134 1135 for (String added : fullAddMods) { 1136 Stream<ModuleSymbol> modules; 1137 switch (added) { 1138 case ALL_SYSTEM: 1139 modules = new HashSet<>(syms.getAllModules()) 1140 .stream() 1141 .filter(systemModulePred.and(observablePred).and(noIncubatorPred)); 1142 break; 1143 case ALL_MODULE_PATH: 1144 modules = new HashSet<>(syms.getAllModules()) 1145 .stream() 1146 .filter(systemModulePred.negate().and(observablePred)); 1147 break; 1148 default: 1149 if (!isValidName(added)) 1150 continue; 1151 modules = Stream.of(syms.enterModule(names.fromString(added))); 1152 break; 1153 } 1154 modules.forEach(sym -> { 1155 enabledRoot.add(sym); 1156 if (observable != null) 1157 observable.add(sym); 1158 }); 1159 } 1160 } 1161 1162 Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable); 1163 1164 result.add(syms.unnamedModule); 1165 1166 String incubatingModules = result.stream() 1167 .filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING)) 1168 .map(msym -> msym.name.toString()) 1169 .collect(Collectors.joining(",")); 1170 1171 if (!incubatingModules.isEmpty()) { 1172 log.warning(Warnings.IncubatingModules(incubatingModules)); 1173 } 1174 1175 allModules = result; 1176 1177 //add module versions from options, if any: 1178 if (moduleVersionOpt != null) { 1179 Name version = names.fromString(moduleVersionOpt); 1180 rootModules.forEach(m -> m.version = version); 1181 } 1182 } 1183 1184 public boolean isInModuleGraph(ModuleSymbol msym) { 1185 return allModules == null || allModules.contains(msym); 1186 } 1187 1188 private Set<ModuleSymbol> computeTransitiveClosure(Set<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) { 1189 List<ModuleSymbol> primaryTodo = List.nil(); 1190 List<ModuleSymbol> secondaryTodo = List.nil(); 1191 1192 for (ModuleSymbol ms : base) { 1193 primaryTodo = primaryTodo.prepend(ms); 1194 } 1195 1196 Set<ModuleSymbol> result = new LinkedHashSet<>(); 1197 result.add(syms.java_base); 1198 1199 while (primaryTodo.nonEmpty() || secondaryTodo.nonEmpty()) { 1200 ModuleSymbol current; 1201 boolean isPrimaryTodo; 1202 if (primaryTodo.nonEmpty()) { 1203 current = primaryTodo.head; 1204 primaryTodo = primaryTodo.tail; 1205 isPrimaryTodo = true; 1206 } else { 1207 current = secondaryTodo.head; 1208 secondaryTodo = secondaryTodo.tail; 1209 isPrimaryTodo = false; 1210 } 1211 if (observable != null && !observable.contains(current)) 1212 continue; 1213 if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0)) 1214 continue; 1215 current.complete(); 1216 if (current.kind == ERR && isPrimaryTodo && warnedMissing.add(current)) { 1217 log.error(Errors.ModuleNotFound(current)); 1218 } 1219 for (RequiresDirective rd : current.requires) { 1220 if (rd.module == syms.java_base) continue; 1221 if ((rd.isTransitive() && isPrimaryTodo) || base.contains(current)) { 1222 primaryTodo = primaryTodo.prepend(rd.module); 1223 } else { 1224 secondaryTodo = secondaryTodo.prepend(rd.module); 1225 } 1226 } 1227 } 1228 1229 return result; 1230 } 1231 1232 public ModuleSymbol getObservableModule(Name name) { 1233 ModuleSymbol mod = syms.getModule(name); 1234 1235 if (allModules().contains(mod)) { 1236 return mod; 1237 } 1238 1239 return null; 1240 } 1241 1242 private Completer getUnnamedModuleCompleter() { 1243 moduleFinder.findAllModules(); 1244 return new Symbol.Completer() { 1245 @Override 1246 public void complete(Symbol sym) throws CompletionFailure { 1247 if (inInitModules) { 1248 sym.completer = this; 1249 return ; 1250 } 1251 ModuleSymbol msym = (ModuleSymbol) sym; 1252 Set<ModuleSymbol> allModules = new HashSet<>(allModules()); 1253 allModules.remove(syms.unnamedModule); 1254 for (ModuleSymbol m : allModules) { 1255 m.complete(); 1256 } 1257 initVisiblePackages(msym, allModules); 1258 } 1259 1260 @Override 1261 public String toString() { 1262 return "unnamedModule Completer"; 1263 } 1264 }; 1265 } 1266 1267 private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresTransitiveCache = new HashMap<>(); 1268 1269 private void completeModule(ModuleSymbol msym) { 1270 if (inInitModules) { 1271 msym.completer = sym -> completeModule(msym); 1272 return ; 1273 } 1274 1275 if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) { 1276 completeAutomaticModule(msym); 1277 } 1278 1279 Assert.checkNonNull(msym.requires); 1280 1281 initAddReads(); 1282 1283 msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet()))); 1284 1285 List<RequiresDirective> requires = msym.requires; 1286 1287 while (requires.nonEmpty()) { 1288 if (!allModules().contains(requires.head.module)) { 1289 Env<AttrContext> env = typeEnvs.get(msym); 1290 if (env != null) { 1291 JavaFileObject origSource = log.useSource(env.toplevel.sourcefile); 1292 try { 1293 log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module)); 1294 } finally { 1295 log.useSource(origSource); 1296 } 1297 } else { 1298 Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0); 1299 } 1300 msym.requires = List.filter(msym.requires, requires.head); 1301 } 1302 requires = requires.tail; 1303 } 1304 1305 Set<ModuleSymbol> readable = new LinkedHashSet<>(); 1306 Set<ModuleSymbol> requiresTransitive = new HashSet<>(); 1307 1308 for (RequiresDirective d : msym.requires) { 1309 d.module.complete(); 1310 readable.add(d.module); 1311 Set<ModuleSymbol> s = retrieveRequiresTransitive(d.module); 1312 Assert.checkNonNull(s, () -> "no entry in cache for " + d.module); 1313 readable.addAll(s); 1314 if (d.flags.contains(RequiresFlag.TRANSITIVE)) { 1315 requiresTransitive.add(d.module); 1316 requiresTransitive.addAll(s); 1317 } 1318 } 1319 1320 requiresTransitiveCache.put(msym, requiresTransitive); 1321 initVisiblePackages(msym, readable); 1322 for (ExportsDirective d: msym.exports) { 1323 if (d.packge != null) { 1324 d.packge.modle = msym; 1325 } 1326 } 1327 1328 } 1329 1330 private Set<ModuleSymbol> retrieveRequiresTransitive(ModuleSymbol msym) { 1331 Set<ModuleSymbol> requiresTransitive = requiresTransitiveCache.get(msym); 1332 1333 if (requiresTransitive == null) { 1334 //the module graph may contain cycles involving automatic modules or --add-reads edges 1335 requiresTransitive = new HashSet<>(); 1336 1337 Set<ModuleSymbol> seen = new HashSet<>(); 1338 List<ModuleSymbol> todo = List.of(msym); 1339 1340 while (todo.nonEmpty()) { 1341 ModuleSymbol current = todo.head; 1342 todo = todo.tail; 1343 if (!seen.add(current)) 1344 continue; 1345 requiresTransitive.add(current); 1346 current.complete(); 1347 Iterable<? extends RequiresDirective> requires; 1348 if (current != syms.unnamedModule) { 1349 Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym); 1350 requires = current.requires; 1351 for (RequiresDirective rd : requires) { 1352 if (rd.isTransitive()) 1353 todo = todo.prepend(rd.module); 1354 } 1355 } else { 1356 for (ModuleSymbol mod : allModules()) { 1357 todo = todo.prepend(mod); 1358 } 1359 } 1360 } 1361 1362 requiresTransitive.remove(msym); 1363 } 1364 1365 return requiresTransitive; 1366 } 1367 1368 private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) { 1369 initAddExports(); 1370 1371 msym.visiblePackages = new LinkedHashMap<>(); 1372 msym.readModules = new HashSet<>(readable); 1373 1374 Map<Name, ModuleSymbol> seen = new HashMap<>(); 1375 1376 for (ModuleSymbol rm : readable) { 1377 if (rm == syms.unnamedModule) 1378 continue; 1379 addVisiblePackages(msym, seen, rm, rm.exports); 1380 } 1381 1382 addExports.forEach((exportsFrom, exports) -> { 1383 addVisiblePackages(msym, seen, exportsFrom, exports); 1384 }); 1385 } 1386 1387 private void addVisiblePackages(ModuleSymbol msym, 1388 Map<Name, ModuleSymbol> seenPackages, 1389 ModuleSymbol exportsFrom, 1390 Collection<ExportsDirective> exports) { 1391 for (ExportsDirective d : exports) { 1392 if (d.modules == null || d.modules.contains(msym)) { 1393 Name packageName = d.packge.fullname; 1394 ModuleSymbol previousModule = seenPackages.get(packageName); 1395 1396 if (previousModule != null && previousModule != exportsFrom) { 1397 Env<AttrContext> env = typeEnvs.get(msym); 1398 JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile) 1399 : null; 1400 DiagnosticPosition pos = env != null ? env.tree.pos() : null; 1401 try { 1402 log.error(pos, Errors.PackageClashFromRequires(msym, packageName, 1403 previousModule, exportsFrom)); 1404 } finally { 1405 if (env != null) 1406 log.useSource(origSource); 1407 } 1408 continue; 1409 } 1410 1411 seenPackages.put(packageName, exportsFrom); 1412 msym.visiblePackages.put(d.packge.fullname, d.packge); 1413 } 1414 } 1415 } 1416 1417 private void initAddExports() { 1418 if (addExports != null) 1419 return; 1420 1421 addExports = new LinkedHashMap<>(); 1422 Set<ModuleSymbol> unknownModules = new HashSet<>(); 1423 1424 if (addExportsOpt == null) 1425 return; 1426 1427 Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)"); 1428 for (String s: addExportsOpt.split("\0+")) { 1429 if (s.isEmpty()) 1430 continue; 1431 Matcher em = ep.matcher(s); 1432 if (!em.matches()) { 1433 continue; 1434 } 1435 1436 // Terminology comes from 1437 // --add-exports module/package=target,... 1438 // Compare to 1439 // module module { exports package to target, ... } 1440 String moduleName = em.group(1); 1441 String packageName = em.group(2); 1442 String targetNames = em.group(3); 1443 1444 if (!isValidName(moduleName)) 1445 continue; 1446 1447 ModuleSymbol msym = syms.enterModule(names.fromString(moduleName)); 1448 if (!isKnownModule(msym, unknownModules)) 1449 continue; 1450 1451 if (!isValidName(packageName)) 1452 continue; 1453 PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName)); 1454 p.modle = msym; // TODO: do we need this? 1455 1456 List<ModuleSymbol> targetModules = List.nil(); 1457 for (String toModule : targetNames.split("[ ,]+")) { 1458 ModuleSymbol m; 1459 if (toModule.equals("ALL-UNNAMED")) { 1460 m = syms.unnamedModule; 1461 } else { 1462 if (!isValidName(toModule)) 1463 continue; 1464 m = syms.enterModule(names.fromString(toModule)); 1465 if (!isKnownModule(m, unknownModules)) 1466 continue; 1467 } 1468 targetModules = targetModules.prepend(m); 1469 } 1470 1471 Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>()); 1472 ExportsDirective d = new ExportsDirective(p, targetModules); 1473 extra.add(d); 1474 } 1475 } 1476 1477 private boolean isKnownModule(ModuleSymbol msym, Set<ModuleSymbol> unknownModules) { 1478 if (allModules.contains(msym)) { 1479 return true; 1480 } 1481 1482 if (!unknownModules.contains(msym)) { 1483 if (lintOptions) { 1484 log.warning(LintCategory.OPTIONS, 1485 Warnings.ModuleForOptionNotFound(Option.ADD_EXPORTS, msym)); 1486 } 1487 unknownModules.add(msym); 1488 } 1489 return false; 1490 } 1491 1492 private void initAddReads() { 1493 if (addReads != null) 1494 return; 1495 1496 addReads = new LinkedHashMap<>(); 1497 1498 if (addReadsOpt == null) 1499 return; 1500 1501 Pattern rp = Pattern.compile("([^=]+)=(.*)"); 1502 for (String s : addReadsOpt.split("\0+")) { 1503 if (s.isEmpty()) 1504 continue; 1505 Matcher rm = rp.matcher(s); 1506 if (!rm.matches()) { 1507 continue; 1508 } 1509 1510 // Terminology comes from 1511 // --add-reads source-module=target-module,... 1512 // Compare to 1513 // module source-module { requires target-module; ... } 1514 String sourceName = rm.group(1); 1515 String targetNames = rm.group(2); 1516 1517 if (!isValidName(sourceName)) 1518 continue; 1519 1520 ModuleSymbol msym = syms.enterModule(names.fromString(sourceName)); 1521 if (!allModules.contains(msym)) { 1522 if (lintOptions) { 1523 log.warning(Warnings.ModuleForOptionNotFound(Option.ADD_READS, msym)); 1524 } 1525 continue; 1526 } 1527 1528 for (String targetName : targetNames.split("[ ,]+", -1)) { 1529 ModuleSymbol targetModule; 1530 if (targetName.equals("ALL-UNNAMED")) { 1531 targetModule = syms.unnamedModule; 1532 } else { 1533 if (!isValidName(targetName)) 1534 continue; 1535 targetModule = syms.enterModule(names.fromString(targetName)); 1536 if (!allModules.contains(targetModule)) { 1537 if (lintOptions) { 1538 log.warning(LintCategory.OPTIONS, Warnings.ModuleForOptionNotFound(Option.ADD_READS, targetModule)); 1539 } 1540 continue; 1541 } 1542 } 1543 addReads.computeIfAbsent(msym, m -> new HashSet<>()) 1544 .add(new RequiresDirective(targetModule, EnumSet.of(RequiresFlag.EXTRA))); 1545 } 1546 } 1547 } 1548 1549 private void checkCyclicDependencies(JCModuleDecl mod) { 1550 for (JCDirective d : mod.directives) { 1551 JCRequires rd; 1552 if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null) 1553 continue; 1554 Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>(); 1555 List<ModuleSymbol> queue = List.of(rd.directive.module); 1556 while (queue.nonEmpty()) { 1557 ModuleSymbol current = queue.head; 1558 queue = queue.tail; 1559 if (!nonSyntheticDeps.add(current)) 1560 continue; 1561 current.complete(); 1562 if ((current.flags() & Flags.ACYCLIC) != 0) 1563 continue; 1564 Assert.checkNonNull(current.requires, current::toString); 1565 for (RequiresDirective dep : current.requires) { 1566 if (!dep.flags.contains(RequiresFlag.EXTRA)) 1567 queue = queue.prepend(dep.module); 1568 } 1569 } 1570 if (nonSyntheticDeps.contains(mod.sym)) { 1571 log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module)); 1572 } 1573 mod.sym.flags_field |= Flags.ACYCLIC; 1574 } 1575 } 1576 1577 private boolean isValidName(CharSequence name) { 1578 return SourceVersion.isName(name, Source.toSourceVersion(source)); 1579 } 1580 1581 // DEBUG 1582 private String toString(ModuleSymbol msym) { 1583 return msym.name + "[" 1584 + "kind:" + msym.kind + ";" 1585 + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";" 1586 + "info:" + toString(msym.module_info.sourcefile) + "," 1587 + toString(msym.module_info.classfile) + "," 1588 + msym.module_info.completer 1589 + "]"; 1590 } 1591 1592 // DEBUG 1593 String toString(Location locn) { 1594 return (locn == null) ? "--" : locn.getName(); 1595 } 1596 1597 // DEBUG 1598 String toString(JavaFileObject fo) { 1599 return (fo == null) ? "--" : fo.getName(); 1600 } 1601 1602 public void newRound() { 1603 rootModules = null; 1604 allModules = null; 1605 warnedMissing.clear(); 1606 } 1607 }