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

Small cleanup of JDK versions enforcements #289

Merged
merged 1 commit into from
Mar 10, 2024
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.
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 @@ -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