Skip to content

Commit

Permalink
Optimization of computing hash for classes in BanDuplicateClasses
Browse files Browse the repository at this point in the history
hash is calculated during jar scanning,
before was calculated during class compare
which cause to open and read jars again - now jar is opened once

close #122
  • Loading branch information
slawekjaranowski committed Jul 1, 2022
1 parent 7cce34b commit af6cc58
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -151,7 +152,8 @@ else if ( file.isDirectory() )
for ( String name : FileUtils.getFileNames( file, null, null, false ) )
{
getLog().debug( " " + name );
checkAndAddName( o, name, classesSeen, duplicateClassNames, ignorableDependencies );
checkAndAddName( o, name, () -> Files.newInputStream( file.toPath().resolve( name ) ),
classesSeen, duplicateClassNames, ignorableDependencies );
}
}
catch ( IOException e )
Expand All @@ -170,7 +172,9 @@ else if ( isJarFile( o ) )
for ( JarEntry entry : Collections.list( jar.entries() ) )
{
String fileName = entry.getName();
checkAndAddName( o, fileName, classesSeen, duplicateClassNames, ignorableDependencies );

checkAndAddName( o, fileName, () -> jar.getInputStream( entry ),
classesSeen, duplicateClassNames, ignorableDependencies );
}
}
}
Expand Down Expand Up @@ -220,10 +224,10 @@ else if ( isJarFile( o ) )

}

private void checkAndAddName( Artifact artifact, String pathToClassFile, Map<String,
private void checkAndAddName( Artifact artifact, String pathToClassFile, InputStreamSupplier inputStreamSupplier, Map<String,
ClassesWithSameName> classesSeen, Set<String> duplicateClasses,
Collection<IgnorableDependency> ignores )
throws EnforcerRuleException
throws EnforcerRuleException, IOException
{
if ( !pathToClassFile.endsWith( ".class" ) )
{
Expand All @@ -244,7 +248,8 @@ private void checkAndAddName( Artifact artifact, String pathToClassFile, Map<Str

ClassesWithSameName classesWithSameName = classesSeen.get( pathToClassFile );
boolean isFirstTimeSeeingThisClass = ( classesWithSameName == null );
ClassFile classFile = new ClassFile( pathToClassFile, artifact );

ClassFile classFile = new ClassFile( pathToClassFile, artifact, inputStreamSupplier );

if ( isFirstTimeSeeingThisClass )
{
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/apache/maven/plugins/enforcer/ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* under the License.
*/

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.maven.artifact.Artifact;

/**
Expand All @@ -44,19 +48,28 @@ public class ClassFile
/** the path to the .class file. Example: org/apache/maven/Stuff.class */
private final String classFilePath;
private final Artifact artifactThisClassWasFoundIn;
private final Hasher hasher;
private String lazilyComputedHash;
private String hash;

/**
* Constructor.
* @param classFilePath path to the class file. Example: org/apache/maven/Stuff.class
* @param artifactThisClassWasFoundIn the maven artifact the class appeared in (example: a jar file)
* @param inputStreamSupplier a supplier for class content input stream
*/
public ClassFile( String classFilePath, Artifact artifactThisClassWasFoundIn )
public ClassFile( String classFilePath, Artifact artifactThisClassWasFoundIn, InputStreamSupplier inputStreamSupplier )
throws IOException
{
this.classFilePath = classFilePath;
this.artifactThisClassWasFoundIn = artifactThisClassWasFoundIn;
this.hasher = new Hasher( classFilePath );
this.hash = computeHash(inputStreamSupplier);
}

private String computeHash( InputStreamSupplier inputStreamSupplier ) throws IOException
{
try (InputStream inputStream = inputStreamSupplier.get())
{
return DigestUtils.md5Hex( inputStream );
}
}

/**
Expand All @@ -81,12 +94,7 @@ public Artifact getArtifactThisClassWasFoundIn()
*/
public String getHash()
{
if ( lazilyComputedHash == null )
{
lazilyComputedHash = hasher.generateHash( artifactThisClassWasFoundIn );
}

return lazilyComputedHash;
return hash;
}

}
139 changes: 0 additions & 139 deletions src/main/java/org/apache/maven/plugins/enforcer/Hasher.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.apache.maven.plugins.enforcer;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.io.InputStream;

/**
* Supplier functional interface for InputStream with exception.
*
* @author Slawomir Jaranowski
*/
@FunctionalInterface
interface InputStreamSupplier
{
InputStream get() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
*/

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.nio.file.Files;

import org.apache.maven.artifact.Artifact;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -58,31 +55,8 @@ public ClassFile createWithContent( String pathToClassFile, String fileContents
.withType( "some type that isn't 'jar' so our code assumes it's a directory" )
.build();

return new ClassFile( pathToClassFile, artifact );
}

public ClassFile createJarWithContent( String jarFileName, String pathToClassFile, String fileContents )
throws IOException
{
uniqueId++;
String uniqueIdStr = Integer.toString( uniqueId );

File tempDirectory = createTempDirectory( uniqueIdStr );
File tempJarFile = new File( tempDirectory, jarFileName );

try ( JarOutputStream outStream = new JarOutputStream( new FileOutputStream( tempJarFile ) ) )
{
outStream.putNextEntry( new JarEntry( pathToClassFile ) );
outStream.write( fileContents.getBytes( StandardCharsets.UTF_8 ) );
}

Artifact artifact = ArtifactBuilder.newBuilder()
.withFileOrDirectory( tempJarFile )
.withVersion( uniqueIdStr )
.withType( "jar" )
.build();

return new ClassFile( pathToClassFile, artifact );
return new ClassFile( pathToClassFile, artifact,
() -> Files.newInputStream( tempDirectory.toPath().resolve( pathToClassFile ) ) );
}

private File createTempDirectory( String uniqueIdStr )
Expand Down
53 changes: 0 additions & 53 deletions src/test/java/org/apache/maven/plugins/enforcer/HasherTest.java

This file was deleted.

0 comments on commit af6cc58

Please sign in to comment.