2323import java .util .Collection ;
2424import java .util .Collections ;
2525import java .util .GregorianCalendar ;
26+ import java .util .HashSet ;
2627import java .util .LinkedHashMap ;
2728import java .util .LinkedHashSet ;
2829import java .util .List ;
@@ -440,10 +441,11 @@ private byte[] addModuleInfo(
440441 ClassReader classReader =
441442 moduleInfo .preserveExisting && existingModuleInfo != null ? new ClassReader (existingModuleInfo ) : null ;
442443 ClassWriter classWriter = new ClassWriter (classReader , 0 );
443- int openModule = moduleInfo .openModule ? Opcodes .ACC_OPEN : 0 ;
444- String moduleVersion = moduleInfo .getModuleVersion () == null ? version : moduleInfo .getModuleVersion ();
445444
446445 if (classReader == null ) {
446+ int openModule = moduleInfo .openModule ? Opcodes .ACC_OPEN : 0 ;
447+ String moduleVersion = moduleInfo .getModuleVersion () == null ? version : moduleInfo .getModuleVersion ();
448+
447449 classWriter .visit (Opcodes .V9 , Opcodes .ACC_MODULE , "module-info" , null , null , null );
448450 ModuleVisitor moduleVisitor =
449451 classWriter .visitModule (moduleInfo .getModuleName (), openModule , moduleVersion );
@@ -452,23 +454,28 @@ private byte[] addModuleInfo(
452454 moduleVisitor .visitEnd ();
453455 classWriter .visitEnd ();
454456 } else {
457+ Set <String > explicitlyHandledPackage = new HashSet <>();
458+ explicitlyHandledPackage .addAll (moduleInfo .exports .keySet ());
459+ explicitlyHandledPackage .addAll (autoExportedPackages );
460+ explicitlyHandledPackage .addAll (removedPackages );
461+
455462 ClassVisitor classVisitor = new ClassVisitor (Opcodes .ASM9 , classWriter ) {
456463 @ Override
457464 public ModuleVisitor visitModule (String name , int access , String version ) {
458- ModuleVisitor moduleVisitor = super .visitModule (name , access , version );
465+ ModuleVisitor moduleVisitor = cv .visitModule (name , access , version );
459466 return new ModuleVisitor (Opcodes .ASM9 , moduleVisitor ) {
460467
461468 @ Override
462469 public void visitPackage (String packaze ) {
463470 if (!removedPackages .contains (pathToPackage (packaze ))) {
464- super .visitPackage (packaze );
471+ mv .visitPackage (packaze );
465472 }
466473 }
467474
468475 @ Override
469476 public void visitExport (String packaze , int access , String ... modules ) {
470- if (!removedPackages .contains (pathToPackage (packaze ))) {
471- super .visitExport (packaze , access , modules );
477+ if (!explicitlyHandledPackage .contains (pathToPackage (packaze ))) {
478+ mv .visitExport (packaze , access , modules );
472479 }
473480 }
474481
@@ -477,7 +484,7 @@ public void visitUse(String service) {
477484 String packaze = service .substring (0 , service .lastIndexOf ("/" ));
478485 // if package is removed, also remove 'use' directives based on the package
479486 if (!removedPackages .contains (pathToPackage (packaze ))) {
480- super .visitUse (service );
487+ mv .visitUse (service );
481488 }
482489 }
483490
@@ -489,14 +496,14 @@ public void visitProvide(String service, String... providers) {
489496 .contains (p ))
490497 .toArray (String []::new );
491498 if (filteredProviders .length > 0 ) {
492- super .visitProvide (service , filteredProviders );
499+ mv .visitProvide (service , filteredProviders );
493500 }
494501 }
495502
496503 @ Override
497504 public void visitEnd () {
498- addModuleInfoEntries (moduleInfo , Collections .emptyMap (), autoExportedPackages , this );
499- super .visitEnd ();
505+ addModuleInfoEntries (moduleInfo , Collections .emptyMap (), autoExportedPackages , mv );
506+ mv .visitEnd ();
500507 }
501508 };
502509 }
0 commit comments