Skip to content

Fix major Java version detection. #93

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,18 @@ public abstract class AbstractZipArchiver

private static int getJavaVersion()
{
String javaSpecVersion = System.getProperty( "java.specification.version" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this code proves to cause more trouble than it is worth. Once Java becomes the minimum required version isJava7OrLower will be deleted. I'm not sure it is worth to try to keep up with the Java versioning changes just for that. We can just change:

private static final boolean isJava7OrLower = getJavaVersion() <= 7;

to

private static final boolean isJava7 = System.getProperty( "java.specification.version" ).startsWith( "1.7" );

As the Java 7 is the minimum required version we know that if it is not Java 7 then it is newer version. Also whenever the Java version string schema will be for Java version higher than 7 it will not begin with "1.7". So this code will work as long "java.specification.version" is not an empty string. We can add check for that and throw an exception - you know just in case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given Java 7 is the exception, I would be reluctant to throw an exception if it's empty, but instead to try and detect 1.7 and if that fails fallback to the normal code path.

if ( javaSpecVersion.contains( "." ) )
{//before jdk 9
return Integer.parseInt( javaSpecVersion.split( "\\." )[1] );
}
else
return majorJavaVersion( System.getProperty( "java.specification.version" ) );
}

static int majorJavaVersion( final String javaSpecVersion )
{
final String[] components = javaSpecVersion.split( "\\." );
final int version = Integer.parseInt( components[0] );
if ( version == 1 )
{
return Integer.parseInt( javaSpecVersion );
return Integer.parseInt( components[1] );
}
return version;
}

public String getComment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public class ZipArchiverTest
extends BasePlexusArchiverTest
{

public void testMajorJavaVersion()
{
assertEquals( 6, ZipArchiver.majorJavaVersion( "1.6" ) );
assertEquals( 7, ZipArchiver.majorJavaVersion( "1.7" ) );
assertEquals( 8, ZipArchiver.majorJavaVersion( "1.8" ) );
assertEquals( 9, ZipArchiver.majorJavaVersion( "9" ) );
assertEquals( 10, ZipArchiver.majorJavaVersion( "10" ) );
assertEquals( 10, ZipArchiver.majorJavaVersion( "10.0.2" ) );
}

public void testImplicitPermissions()
throws IOException
{
Expand Down