Skip to content

Commit

Permalink
Cache signatures so that multiple runs of the mojo can reuse loaded s…
Browse files Browse the repository at this point in the history
…ignatures (#165)

Loading JDK signatures can take more than half a second, so when the build contains lots of modules, it can save a lot of time
  • Loading branch information
gnodet committed Jun 11, 2021
1 parent dd3c7c2 commit f8df3fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Expand Up @@ -44,17 +44,21 @@
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.codehaus.mojo.animal_sniffer.ClassFileVisitor;
import org.codehaus.mojo.animal_sniffer.ClassListBuilder;
import org.codehaus.mojo.animal_sniffer.Clazz;
import org.codehaus.mojo.animal_sniffer.SignatureChecker;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Checks the classes compiled by this module.
Expand Down Expand Up @@ -203,6 +207,8 @@ public void setSignature( String signatureId ) {
@Component
protected ArtifactFactory artifactFactory;

static Map<File, Map<String, Clazz>> classes = new ConcurrentHashMap<>();

@Override
public void execute()
throws MojoExecutionException, MojoFailureException
Expand Down Expand Up @@ -262,7 +268,7 @@ public void execute()
}

final SignatureChecker signatureChecker =
new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages,
new SignatureChecker( loadClasses( a.getFile() ), ignoredPackages,
new MavenLogger( getLog() ) );
signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to
// the output directory as resources.
Expand Down Expand Up @@ -309,6 +315,17 @@ public void execute()
}
}

private static Map<String, Clazz> loadClasses( File f ) throws IOException
{
Map<String, Clazz> classes = CheckSignatureMojo.classes.get( f );
if ( classes == null )
{
classes = SignatureChecker.loadClasses( new FileInputStream( f ) );
CheckSignatureMojo.classes.putIfAbsent( f, classes );
}
return classes;
}

private static Dependency findMatchingDependency( Signature signature, List<Dependency> dependencies )
{
Dependency match = null;
Expand Down
Expand Up @@ -74,7 +74,7 @@ public class SignatureChecker
*/
public static final String PREVIOUS_ANNOTATION_FQN = "org.jvnet.animal_sniffer.IgnoreJRERequirement";

private final Map<String, Clazz> classes = new HashMap<>();
private final Map<String, Clazz> classes;

private final Logger logger;

Expand Down Expand Up @@ -108,6 +108,13 @@ public static void main( String[] args )
public SignatureChecker( InputStream in, Set<String> ignoredPackages, Logger logger )
throws IOException
{
this( loadClasses( in ), ignoredPackages, logger );
}

public SignatureChecker( Map<String, Clazz> classes, Set<String> ignoredPackages, Logger logger )
throws IOException
{
this.classes = classes;
this.ignoredPackages = new HashSet<>();
this.ignoredPackageRules = new LinkedList<>();
for(String wildcard : ignoredPackages )
Expand All @@ -126,14 +133,19 @@ public SignatureChecker( InputStream in, Set<String> ignoredPackages, Logger log
this.annotationDescriptors.add( toAnnotationDescriptor( PREVIOUS_ANNOTATION_FQN ) );

this.logger = logger;
}

public static Map<String, Clazz> loadClasses( InputStream in ) throws IOException
{
Map<String, Clazz> classes = new HashMap<>();
try (ObjectInputStream ois = new ObjectInputStream( new GZIPInputStream( in ) ))
{
while ( true )
{
Clazz c = (Clazz) ois.readObject();
if ( c == null )
{
return; // finished
return classes; // finished
}
classes.put( c.getName(), c );
}
Expand Down

0 comments on commit f8df3fe

Please sign in to comment.