< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java

Print this page
rev 3886 : 8155026: javac grants implied readability to explicit modules
Summary: Ordinary named modules shouldn't be requires public for automatic modules
Reviewed-by: TBD


 487             Set<String> seenPackages = new HashSet<>();
 488 
 489             for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
 490                 String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
 491                 String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
 492                 if (seenPackages.add(pack)) {
 493                     ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null);
 494                     directives.add(d);
 495                     exports.add(d);
 496                 }
 497             }
 498 
 499             ListBuffer<RequiresDirective> requires = new ListBuffer<>();
 500 
 501             //ensure all modules are found:
 502             moduleFinder.findAllModules();
 503 
 504             for (ModuleSymbol ms : allModules()) {
 505                 if (ms == syms.unnamedModule || ms == msym)
 506                     continue;
 507                 RequiresDirective d = new RequiresDirective(ms, EnumSet.of(RequiresFlag.PUBLIC));


 508                 directives.add(d);
 509                 requires.add(d);
 510             }
 511 
 512             RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
 513             directives.add(requiresUnnamed);
 514             requires.add(requiresUnnamed);
 515 
 516             msym.exports = exports.toList();
 517             msym.provides = List.nil();
 518             msym.requires = requires.toList();
 519             msym.uses = List.nil();
 520             msym.directives = directives.toList();
 521             msym.flags_field |= Flags.ACYCLIC;
 522         } catch (IOException ex) {
 523             throw new IllegalStateException(ex);
 524         }
 525     }
 526 
 527     private Completer getSourceCompleter(JCCompilationUnit tree) {


 978                         log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
 979                     } finally {
 980                         log.useSource(origSource);
 981                     }
 982                 } else {
 983                     Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
 984                 }
 985                 if (previous != null) {
 986                     previous.tail = requires.tail;
 987                 } else {
 988                     msym.requires.tail = requires.tail;
 989                 }
 990             } else {
 991                 previous = requires;
 992             }
 993             requires = requires.tail;
 994         }
 995 
 996         Set<ModuleSymbol> readable = new LinkedHashSet<>();
 997         Set<ModuleSymbol> requiresPublic = new HashSet<>();
 998         if ((msym.flags() & Flags.AUTOMATIC_MODULE) == 0) {
 999             for (RequiresDirective d : msym.requires) {
1000                 d.module.complete();
1001                 readable.add(d.module);
1002                 Set<ModuleSymbol> s = retrieveRequiresPublic(d.module);
1003                 Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
1004                 readable.addAll(s);
1005                 if (d.flags.contains(RequiresFlag.PUBLIC)) {
1006                     requiresPublic.add(d.module);
1007                     requiresPublic.addAll(s);
1008                 }
1009             }
1010         } else {
1011             //the module graph may contain cycles involving automatic modules
1012             //handle automatic modules separatelly:
1013             Set<ModuleSymbol> s = retrieveRequiresPublic(msym);
1014 
1015             readable.addAll(s);
1016             requiresPublic.addAll(s);
1017 
1018             //ensure the unnamed module is added (it is not requires public):
1019             readable.add(syms.unnamedModule);
1020         }
1021         requiresPublicCache.put(msym, requiresPublic);
1022         initVisiblePackages(msym, readable);
1023         for (ExportsDirective d: msym.exports) {
1024             d.packge.modle = msym;
1025         }
1026 
1027     }
1028 
1029     private Set<ModuleSymbol> retrieveRequiresPublic(ModuleSymbol msym) {
1030         Set<ModuleSymbol> requiresPublic = requiresPublicCache.get(msym);
1031 
1032         if (requiresPublic == null) {
1033             //the module graph may contain cycles involving automatic modules or -XaddReads edges
1034             requiresPublic = new HashSet<>();
1035 
1036             Set<ModuleSymbol> seen = new HashSet<>();
1037             List<ModuleSymbol> todo = List.of(msym);
1038 
1039             while (todo.nonEmpty()) {
1040                 ModuleSymbol current = todo.head;




 487             Set<String> seenPackages = new HashSet<>();
 488 
 489             for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
 490                 String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
 491                 String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
 492                 if (seenPackages.add(pack)) {
 493                     ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null);
 494                     directives.add(d);
 495                     exports.add(d);
 496                 }
 497             }
 498 
 499             ListBuffer<RequiresDirective> requires = new ListBuffer<>();
 500 
 501             //ensure all modules are found:
 502             moduleFinder.findAllModules();
 503 
 504             for (ModuleSymbol ms : allModules()) {
 505                 if (ms == syms.unnamedModule || ms == msym)
 506                     continue;
 507                 Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ?
 508                         EnumSet.of(RequiresFlag.PUBLIC) : EnumSet.noneOf(RequiresFlag.class);
 509                 RequiresDirective d = new RequiresDirective(ms, flags);
 510                 directives.add(d);
 511                 requires.add(d);
 512             }
 513 
 514             RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
 515             directives.add(requiresUnnamed);
 516             requires.add(requiresUnnamed);
 517 
 518             msym.exports = exports.toList();
 519             msym.provides = List.nil();
 520             msym.requires = requires.toList();
 521             msym.uses = List.nil();
 522             msym.directives = directives.toList();
 523             msym.flags_field |= Flags.ACYCLIC;
 524         } catch (IOException ex) {
 525             throw new IllegalStateException(ex);
 526         }
 527     }
 528 
 529     private Completer getSourceCompleter(JCCompilationUnit tree) {


 980                         log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
 981                     } finally {
 982                         log.useSource(origSource);
 983                     }
 984                 } else {
 985                     Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
 986                 }
 987                 if (previous != null) {
 988                     previous.tail = requires.tail;
 989                 } else {
 990                     msym.requires.tail = requires.tail;
 991                 }
 992             } else {
 993                 previous = requires;
 994             }
 995             requires = requires.tail;
 996         }
 997 
 998         Set<ModuleSymbol> readable = new LinkedHashSet<>();
 999         Set<ModuleSymbol> requiresPublic = new HashSet<>();
1000 
1001         for (RequiresDirective d : msym.requires) {
1002             d.module.complete();
1003             readable.add(d.module);
1004             Set<ModuleSymbol> s = retrieveRequiresPublic(d.module);
1005             Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
1006             readable.addAll(s);
1007             if (d.flags.contains(RequiresFlag.PUBLIC)) {
1008                 requiresPublic.add(d.module);
1009                 requiresPublic.addAll(s);
1010             }
1011         }







1012 



1013         requiresPublicCache.put(msym, requiresPublic);
1014         initVisiblePackages(msym, readable);
1015         for (ExportsDirective d: msym.exports) {
1016             d.packge.modle = msym;
1017         }
1018 
1019     }
1020 
1021     private Set<ModuleSymbol> retrieveRequiresPublic(ModuleSymbol msym) {
1022         Set<ModuleSymbol> requiresPublic = requiresPublicCache.get(msym);
1023 
1024         if (requiresPublic == null) {
1025             //the module graph may contain cycles involving automatic modules or -XaddReads edges
1026             requiresPublic = new HashSet<>();
1027 
1028             Set<ModuleSymbol> seen = new HashSet<>();
1029             List<ModuleSymbol> todo = List.of(msym);
1030 
1031             while (todo.nonEmpty()) {
1032                 ModuleSymbol current = todo.head;


< prev index next >