@@ -71,6 +71,13 @@ private static void defineModule0(Module module, boolean isOpen, String version,
7171 if (module .getClassLoader () != null ) {
7272 throw new IllegalArgumentException ("Class loader must be the boot class loader" );
7373 }
74+
75+ // TODO (ivan-ristovic) is synchronization nececary?
76+ boolean duplicateJavaBase = ModuleUtil .bootLayerContainsModule ("java.base" );
77+ if (duplicateJavaBase ) {
78+ throw new InternalError ("Module java.base is already defined" );
79+ }
80+
7481 return ;
7582 }
7683
@@ -90,7 +97,7 @@ private static void defineModule0(Module module, boolean isOpen, String version,
9097 }
9198 }
9299
93- // TODO is synchronization nececary ?
100+ // TODO (ivan-ristovic) is synchronization necessary ?
94101 Package existingPackage = null ;
95102 boolean moduleAlreadyExists = false ;
96103 for (String pn : pns ) {
@@ -140,7 +147,6 @@ private static void addExportsToAll0(Module from, String pn) {
140147 @ Substitute
141148 private static void addExportsToAllUnnamed0 (Module from , String pn ) {
142149 ModuleUtil .checkFromModuleAndPackageNullability (from , pn );
143-
144150 if (from .isNamed ()) {
145151 ModuleUtil .checkIsPackageContainedInModule (pn , from );
146152 }
@@ -165,30 +171,42 @@ static void checkFromModuleAndPackageNullability(Module from, String pn) {
165171 }
166172
167173 static void checkIsPackageContainedInModule (String pn , Module module ) {
174+ ClassLoader loader = module .getClassLoader () == null ? ClassLoader .getPlatformClassLoader () : module .getClassLoader ();
175+ Package definedPackage = loader .getDefinedPackage (pn );
176+ if (definedPackage != null ) {
177+ Target_java_lang_NamedPackage namedPackage = SubstrateUtil .cast (definedPackage , Target_java_lang_NamedPackage .class );
178+ Module actualModule = namedPackage .module ;
179+ if (!actualModule .equals (module )) {
180+ throw new IllegalArgumentException ("Package " + pn + " found in module " + actualModule .getName () +
181+ ", not in module: " + module .getName ());
182+ }
183+ }
184+
168185 if (module .getPackages ().stream ().noneMatch (p -> p .equals (pn ))) {
169186 throw new IllegalArgumentException ("Package " + pn + " not found in from_module " + module .getName ());
170187 }
171-
172- // module.getClassLoader().getDefinedPackage(pn);
173- // TODO print message if package is not in from_module but is in another module
174188 }
175189
176190 static boolean isPackageNameForbidden (String pn ) {
177191 if (!pn .startsWith ("java" )) {
178192 return false ;
179193 }
180- char trailingChar = pn .length () < 5 ? '/ ' : pn .charAt ("java" .length ());
181- return trailingChar == '/ ' ;
194+ char trailingChar = pn .length () < 5 ? '. ' : pn .charAt ("java" .length ());
195+ return trailingChar == '. ' ;
182196 }
183197
184198 static boolean isValidPackageName (String pn ) {
185199 // It is OK to use SourceVersion.isName here even though it calls String.split()
186200 // because pattern "\\." will take the fast path in the String.split() method
187- return Objects .nonNull (pn ) && ! pn . isEmpty () && SourceVersion .isName (pn );
201+ return Objects .nonNull (pn ) && SourceVersion .isName (pn );
188202 }
189203
190204 static boolean moduleAlreadyExists (Module module ) {
191- // TODO
205+ // TODO (ivan-ristovic)
192206 return false ;
193207 }
208+
209+ public static boolean bootLayerContainsModule (String name ) {
210+ return ModuleLayer .boot ().modules ().stream ().anyMatch (m -> m .getName ().equals (name ));
211+ }
194212}
0 commit comments