Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache signatures so that multiple runs of the mojo can reuse loaded signatures #165

Merged
merged 1 commit into from Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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