Skip to content

Commit

Permalink
Small cleanup of JDK versions enforcements
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz authored and slawekjaranowski committed Mar 10, 2024
1 parent 461f15d commit fdeb501
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.apache.maven.shared.artifact.filter.AbstractStrictPatternArtifactFilter;
import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
import org.codehaus.plexus.util.IOUtil;
import org.eclipse.aether.RepositorySystem;

/**
Expand Down Expand Up @@ -154,7 +153,7 @@ static String renderVersion(int major, int minor) {
private String message;

/**
* JDK version as used for example in the maven-compiler-plugin: 1.5, 1.6 and so on. If in need of more precise
* JDK version as used for example in the maven-compiler-plugin: 8, 11 and so on. If in need of more precise
* configuration please see {@link #maxJavaMajorVersionNumber} and {@link #maxJavaMinorVersionNumber} Mandatory if
* {@link #maxJavaMajorVersionNumber} not specified.
*/
Expand Down Expand Up @@ -224,14 +223,13 @@ private void computeParameters() throws EnforcerRuleException {
}
if (maxJdkVersion == null && maxJavaMajorVersionNumber == -1) {
throw new IllegalArgumentException(
"Exactly one of maxJdkVersion or " + "maxJavaMajorVersionNumber options should be set.");
"Exactly one of maxJdkVersion or maxJavaMajorVersionNumber options should be set.");
}
if (maxJdkVersion != null) {
Integer needle = JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.get(maxJdkVersion);
if (needle == null) {
throw new IllegalArgumentException(
"Unknown JDK version given. Should be something like "
+ "\"1.7\", \"8\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\"");
"Unknown JDK version given. Should be something like \"8\", \"11\", \"17\", \"21\" ..");
}
maxJavaMajorVersionNumber = needle;
if (!strict && needle < 53) {
Expand Down Expand Up @@ -291,25 +289,16 @@ private String isBadArtifact(Artifact a) throws EnforcerRuleException {
}
}

InputStream is = null;
try {
is = jarFile.getInputStream(entry);
try (InputStream is = jarFile.getInputStream(entry)) {
int total = magicAndClassFileVersion.length;
while (total > 0) {
int read =
is.read(magicAndClassFileVersion, magicAndClassFileVersion.length - total, total);

if (read == -1) {
throw new EOFException(f.toString());
}

total -= read;
}

is.close();
is = null;
} finally {
IOUtil.close(is);
}

int minor = (magicAndClassFileVersion[4] << 8) + magicAndClassFileVersion[5];
Expand Down
2 changes: 1 addition & 1 deletion src/site/apt/enforceBytecodeVersion.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Enforce Bytecode Version

The following parameters are supported by this rule:

* <<maxJdkVersion>> - the maximum target jdk version in the 1.x form (e.g. 1.6, 1.7, 1.8, 1.9 or 6, 7, 8, 9, 10, 11...)
* <<maxJdkVersion>> - the maximum target jdk version (e.g. 8, 11, 17, 21...)

* <<maxJavaMajorVersionNumber>> - an integer indicating the maximum bytecode major version number (cannot be specified if maxJdkVersion is present)

Expand Down

0 comments on commit fdeb501

Please sign in to comment.