@@ -97,25 +97,28 @@ private static void defineModule0(Module module, boolean isOpen, String version,
9797 }
9898 }
9999
100- // TODO (ivan-ristovic) is synchronization necessary?
101- Package existingPackage = null ;
102- boolean moduleAlreadyExists = false ;
103- for (String pn : pns ) {
104- existingPackage = loader .getDefinedPackage (pn );
105- if (Objects .nonNull (existingPackage )) {
106- moduleAlreadyExists = ModuleUtil .moduleAlreadyExists (module );
107- break ;
100+ Package definedPackage = null ;
101+ Module definedModule = null ;
102+ //noinspection CheckStyle
103+ synchronized (ModuleUtil .moduleLock ) {
104+ for (String pn : pns ) {
105+ definedPackage = loader .getDefinedPackage (pn );
106+ if (Objects .nonNull (definedPackage )) {
107+ definedModule = ModuleUtil .getDefinedModule (module .getName ());
108+ break ;
109+ }
108110 }
109111 }
110- if (moduleAlreadyExists ) {
112+
113+ if (Objects .nonNull (definedModule )) {
111114 throw new IllegalStateException ("Module " + module .getName () + " is already defined" );
112- } else if (Objects .nonNull (existingPackage )) {
113- Target_java_lang_NamedPackage namedPackage = SubstrateUtil .cast (existingPackage , Target_java_lang_NamedPackage .class );
115+ } else if (Objects .nonNull (definedPackage )) {
116+ Target_java_lang_NamedPackage namedPackage = SubstrateUtil .cast (definedPackage , Target_java_lang_NamedPackage .class );
114117 if (namedPackage .module .isNamed ()) {
115- throw new IllegalStateException ("Package " + existingPackage .getName () + " is already in another module, "
118+ throw new IllegalStateException ("Package " + definedPackage .getName () + " is already in another module, "
116119 + namedPackage .module .getName () + ", defined to the class loader" );
117120 } else {
118- throw new IllegalStateException ("Package " + existingPackage .getName ()
121+ throw new IllegalStateException ("Package " + definedPackage .getName ()
119122 + " is already in the unnamed module defined to the class loader" );
120123 }
121124 }
@@ -160,6 +163,9 @@ private static final class Target_java_lang_Module_ReflectionData {
160163}
161164
162165class ModuleUtil {
166+
167+ static final Object moduleLock = new Object ();
168+
163169 static void checkFromModuleAndPackageNullability (Module from , String pn ) {
164170 if (Objects .isNull (from )) {
165171 throw new NullPointerException ("from_module is null" );
@@ -182,7 +188,7 @@ static void checkIsPackageContainedInModule(String pn, Module module) {
182188 }
183189 }
184190
185- if (module .getPackages ().stream (). noneMatch ( p -> p . equals ( pn ) )) {
191+ if (! module .getPackages ().contains ( pn )) {
186192 throw new IllegalArgumentException ("Package " + pn + " not found in from_module " + module .getName ());
187193 }
188194 }
@@ -201,9 +207,9 @@ static boolean isValidPackageName(String pn) {
201207 return Objects .nonNull (pn ) && SourceVersion .isName (pn );
202208 }
203209
204- static boolean moduleAlreadyExists ( Module module ) {
210+ static Module getDefinedModule ( String moduleName ) {
205211 // TODO (ivan-ristovic)
206- return false ;
212+ return null ;
207213 }
208214
209215 public static boolean bootLayerContainsModule (String name ) {
0 commit comments