3333import java .io .*;
3434import java .nio .charset .StandardCharsets ;
3535import java .util .LinkedHashMap ;
36- import java .util .LinkedHashSet ;
3736import java .util .Map ;
38- import java .util .Set ;
3937import java .util .jar .*;
4038import java .util .regex .Pattern ;
41- import java .util .stream .Collectors ;
4239import java .util .zip .ZipEntry ;
4340
4441/**
@@ -144,7 +141,7 @@ private static void addAutomaticModuleName(File originalJar, File moduleJar, Str
144141 private static void addModuleDescriptor (File originalJar , File moduleJar , ModuleInfo moduleInfo ) {
145142 try (JarInputStream inputStream = new JarInputStream (new FileInputStream (originalJar ))) {
146143 try (JarOutputStream outputStream = newJarOutputStream (new FileOutputStream (moduleJar ), inputStream .getManifest ())) {
147- Map <String , Set < String > > providers = copyAndExtractProviders (inputStream , outputStream );
144+ Map <String , String [] > providers = copyAndExtractProviders (inputStream , outputStream );
148145 outputStream .putNextEntry (new JarEntry ("module-info.class" ));
149146 outputStream .write (addModuleInfo (moduleInfo , providers ));
150147 outputStream .closeEntry ();
@@ -158,9 +155,9 @@ private static JarOutputStream newJarOutputStream(OutputStream out, Manifest man
158155 return manifest == null ? new JarOutputStream (out ) : new JarOutputStream (out , manifest );
159156 }
160157
161- private static Map <String , Set < String > > copyAndExtractProviders (JarInputStream inputStream , JarOutputStream outputStream ) throws IOException {
158+ private static Map <String , String [] > copyAndExtractProviders (JarInputStream inputStream , JarOutputStream outputStream ) throws IOException {
162159 JarEntry jarEntry = inputStream .getNextJarEntry ();
163- Map <String , Set < String > > providers = new LinkedHashMap <>();
160+ Map <String , String [] > providers = new LinkedHashMap <>();
164161 while (jarEntry != null ) {
165162 byte [] content = inputStream .readAllBytes ();
166163 String entryName = jarEntry .getName ();
@@ -175,17 +172,18 @@ private static Map<String, Set<String>> copyAndExtractProviders(JarInputStream i
175172 return providers ;
176173 }
177174
178- private static Set < String > extractImplementations (byte [] content ) {
175+ private static String [] extractImplementations (byte [] content ) {
179176 return new BufferedReader (new InputStreamReader (new ByteArrayInputStream (content ), StandardCharsets .UTF_8 ))
180177 .lines ()
181178 .map (String ::trim )
182179 .filter (line -> !line .isEmpty ())
183180 .filter (line -> !line .startsWith ("#" ))
184181 .map (line -> line .replace ('.' ,'/' ))
185- .collect (Collectors .toCollection (LinkedHashSet ::new ));
182+ .distinct ()
183+ .toArray (String []::new );
186184 }
187185
188- private static byte [] addModuleInfo (ModuleInfo moduleInfo , Map <String , Set < String > > providers ) {
186+ private static byte [] addModuleInfo (ModuleInfo moduleInfo , Map <String , String [] > providers ) {
189187 ClassWriter classWriter = new ClassWriter (0 );
190188 classWriter .visit (Opcodes .V9 , Opcodes .ACC_MODULE , "module-info" , null , null , null );
191189 ModuleVisitor moduleVisitor = classWriter .visitModule (moduleInfo .getModuleName (), Opcodes .ACC_OPEN , moduleInfo .getModuleVersion ());
@@ -202,11 +200,8 @@ private static byte[] addModuleInfo(ModuleInfo moduleInfo, Map<String, Set<Strin
202200 for (String requireName : moduleInfo .getRequiresStatic ()) {
203201 moduleVisitor .visitRequire (requireName , Opcodes .ACC_STATIC_PHASE , null );
204202 }
205- for (Map .Entry <String , Set <String >> entry : providers .entrySet ()) {
206- String provider = entry .getKey ();
207- String [] implementations = entry .getValue ()
208- .toArray (new String [0 ]);
209- moduleVisitor .visitProvide (provider , implementations );
203+ for (Map .Entry <String , String []> entry : providers .entrySet ()) {
204+ moduleVisitor .visitProvide (entry .getKey (), entry .getValue ());
210205 }
211206 moduleVisitor .visitEnd ();
212207 classWriter .visitEnd ();
0 commit comments