3737import java .util .jar .JarFile ;
3838import java .util .stream .Stream ;
3939
40- import org .objectweb .asm .ClassReader ;
41- import org .objectweb .asm .ClassVisitor ;
42- import org .objectweb .asm .MethodVisitor ;
4340import org .apache .maven .artifact .Artifact ;
4441import org .apache .maven .artifact .resolver .filter .ArtifactFilter ;
4542import org .apache .maven .plugin .AbstractMojo ;
5451import org .codehaus .plexus .PlexusContainer ;
5552import org .codehaus .plexus .component .repository .exception .ComponentLookupException ;
5653import org .codehaus .plexus .util .xml .PrettyPrintXMLWriter ;
54+ import org .objectweb .asm .ClassReader ;
55+ import org .objectweb .asm .ClassVisitor ;
56+ import org .objectweb .asm .MethodVisitor ;
5757import org .objectweb .asm .ModuleVisitor ;
5858import org .objectweb .asm .Type ;
5959
@@ -472,16 +472,16 @@ private boolean checkDependencies() throws MojoExecutionException {
472472
473473 // todo: enhance analyzer (dependency) to do it since it already visits classes
474474 // will save some time
475- private Set <String > scanForSpiUsage (final Set < Artifact > usedDeclared ,
476- final Map <Artifact , Set <String >> usedUndeclaredWithClasses ) {
475+ private Set <String > scanForSpiUsage (
476+ final Set < Artifact > usedDeclared , final Map <Artifact , Set <String >> usedUndeclaredWithClasses ) {
477477 return Stream .concat (
478- usedDeclared .stream ().flatMap (this ::findUsedSpi ),
479- usedUndeclaredWithClasses .keySet ().stream ().flatMap (this ::findUsedSpi ))
478+ usedDeclared .stream ().flatMap (this ::findUsedSpi ),
479+ usedUndeclaredWithClasses .keySet ().stream ().flatMap (this ::findUsedSpi ))
480480 .collect (toSet ());
481481 }
482482
483483 private Stream <String > findUsedSpi (final Artifact artifact ) {
484- try (final JarFile jar = new JarFile (artifact .getFile ())) {
484+ try (JarFile jar = new JarFile (artifact .getFile ())) {
485485 return list (jar .entries ()).stream ()
486486 .filter (entry -> entry .getName ().endsWith (".class" ))
487487 .flatMap (entry -> {
@@ -492,39 +492,43 @@ private Stream<String> findUsedSpi(final Artifact artifact) {
492492 return Stream .empty ();
493493 }
494494 final Set <String > spi = new HashSet <>();
495- classReader .accept (new ClassVisitor (ASM9 ) {
496- @ Override
497- public MethodVisitor visitMethod (final int access ,
498- final String name ,
499- final String descriptor ,
500- final String signature ,
501- final String [] exceptions ) {
502- return new MethodVisitor (ASM9 ) {
503- private Type lastType = null ;
504-
495+ classReader .accept (
496+ new ClassVisitor (ASM9 ) {
505497 @ Override
506- public void visitLdcInsn (final Object value ) {
507- if (value instanceof Type ) {
508- lastType = (Type ) value ;
509- }
498+ public MethodVisitor visitMethod (
499+ final int access ,
500+ final String name ,
501+ final String descriptor ,
502+ final String signature ,
503+ final String [] exceptions ) {
504+ return new MethodVisitor (ASM9 ) {
505+ private Type lastType = null ;
506+
507+ @ Override
508+ public void visitLdcInsn (final Object value ) {
509+ if (value instanceof Type ) {
510+ lastType = (Type ) value ;
511+ }
512+ }
513+
514+ @ Override
515+ public void visitMethodInsn (
516+ final int opcode ,
517+ final String owner ,
518+ final String name ,
519+ final String descriptor ,
520+ final boolean isInterface ) {
521+ if (opcode == INVOKESTATIC
522+ && Objects .equals (owner , "java/util/ServiceLoader" )
523+ && Objects .equals (name , "load" )) {
524+ spi .add (lastType .getClassName ());
525+ }
526+ lastType = null ;
527+ }
528+ };
510529 }
511-
512- @ Override
513- public void visitMethodInsn (final int opcode ,
514- final String owner ,
515- final String name ,
516- final String descriptor ,
517- final boolean isInterface ) {
518- if (opcode == INVOKESTATIC &&
519- Objects .equals (owner , "java/util/ServiceLoader" ) &&
520- Objects .equals (name , "load" )) {
521- spi .add (lastType .getClassName ());
522- }
523- lastType = null ;
524- }
525- };
526- }
527- }, 0 );
530+ },
531+ 0 );
528532 return spi .stream ();
529533 })
530534 .collect (toList ()) // materialize before closing the jar
@@ -544,7 +548,7 @@ private void cleanupUnused(final Set<String> spi, final Set<Artifact> unusedDecl
544548 // TODO: enhance to ensure there is a single binding else just log a warning for all
545549 // and maybe even handle version?
546550 private boolean isSlf4jBinding (final Artifact artifact ) {
547- try (final JarFile file = new JarFile (artifact .getFile ())) {
551+ try (JarFile file = new JarFile (artifact .getFile ())) {
548552 return file .getEntry ("org/slf4j/impl/StaticLoggerBinder.class" ) != null ;
549553 } catch (final IOException e ) {
550554 return false ;
@@ -553,7 +557,7 @@ private boolean isSlf4jBinding(final Artifact artifact) {
553557
554558 private boolean hasUsedSPIImpl (final Set <String > usedSpi , final Artifact artifact ) {
555559 final Set <String > spi ;
556- try (final JarFile file = new JarFile (artifact .getFile ())) {
560+ try (JarFile file = new JarFile (artifact .getFile ())) {
557561 spi = list (file .entries ()).stream ()
558562 .filter (it -> it .getName ().startsWith ("META-INF/services/" ) && !it .isDirectory ())
559563 .map (it -> it .getName ().substring ("META-INF/services/" .length ()))
@@ -562,19 +566,21 @@ private boolean hasUsedSPIImpl(final Set<String> usedSpi, final Artifact artifac
562566 // java >= 9
563567 final JarEntry moduleEntry = file .getJarEntry ("module-info.class" );
564568 if (moduleEntry != null ) {
565- try (final InputStream in = file .getInputStream (moduleEntry )) {
569+ try (InputStream in = file .getInputStream (moduleEntry )) {
566570 final ClassReader cr = new ClassReader (in );
567- cr .accept (new ClassVisitor (ASM9 ) {
568- @ Override
569- public ModuleVisitor visitModule (String name , int access , String version ) {
570- return new ModuleVisitor (ASM9 ) {
571+ cr .accept (
572+ new ClassVisitor (ASM9 ) {
571573 @ Override
572- public void visitProvide (final String service , final String [] providers ) {
573- spi .add (service .replace ('/' , '.' ));
574+ public ModuleVisitor visitModule (String name , int access , String version ) {
575+ return new ModuleVisitor (ASM9 ) {
576+ @ Override
577+ public void visitProvide (final String service , final String [] providers ) {
578+ spi .add (service .replace ('/' , '.' ));
579+ }
580+ };
574581 }
575- };
576- }
577- }, 0 );
582+ },
583+ 0 );
578584 }
579585 }
580586 } catch (final IOException e ) {
@@ -693,8 +699,8 @@ private void writeScriptableOutput(Set<Artifact> artifacts) {
693699 }
694700
695701 private List <Artifact > filterDependencies (Set <Artifact > artifacts , String [] excludes ) {
696- ArtifactFilter filter = new StrictPatternExcludesArtifactFilter (excludes == null ?
697- Collections .emptyList () :Arrays .asList (excludes ));
702+ ArtifactFilter filter = new StrictPatternExcludesArtifactFilter (
703+ excludes == null ? Collections .emptyList () : Arrays .asList (excludes ));
698704 List <Artifact > result = new ArrayList <>();
699705
700706 for (Iterator <Artifact > it = artifacts .iterator (); it .hasNext (); ) {
0 commit comments