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

Add logging to CompiledJavaVersionBuildStep #31510

Merged
merged 1 commit into from
Mar 1, 2023
Merged
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 @@ -11,37 +11,46 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicReference;

import org.jboss.logging.Logger;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem;
import io.quarkus.deployment.pkg.builditem.CompiledJavaVersionBuildItem;

public class CompiledJavaVersionBuildStep {

private static final Logger log = Logger.getLogger(CompiledJavaVersionBuildStep.class);

/**
* Determines the Java version by looking up the major version of the first successfully parsed
* application .class file that is found
*/
@BuildStep
public CompiledJavaVersionBuildItem compiledJavaVersion(BuildSystemTargetBuildItem buildSystemTarget) {
if ((buildSystemTarget.getOutputDirectory() == null) || (!Files.exists(buildSystemTarget.getOutputDirectory()))) {
log.debug("Skipping because output directory does not exist");
// needed for Arquillian TCK tests
return CompiledJavaVersionBuildItem.unknown();
}
AtomicReference<Integer> majorVersion = new AtomicReference<>(null);
try {
log.debugf("Walking directory '%s'", buildSystemTarget.getOutputDirectory().toAbsolutePath().toString());
Files.walkFileTree(buildSystemTarget.getOutputDirectory(), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.getFileName().toString().endsWith(".class")) {
log.debugf("Checking file '%s'", file.toAbsolutePath().toString());
try (InputStream in = new FileInputStream(file.toFile())) {
DataInputStream data = new DataInputStream(in);
if (0xCAFEBABE == data.readInt()) {
data.readUnsignedShort(); // minor version -> we don't care about it
majorVersion.set(data.readUnsignedShort());
int v = data.readUnsignedShort();
majorVersion.set(v);
log.debugf("Determined compile java version to be %d", v);
return FileVisitResult.TERMINATE;
}
} catch (IOException ignored) {

} catch (IOException e) {
log.debugf(e, "Encountered exception while processing file '%s'", file.toAbsolutePath().toString());
}
}
// if this was not .class file or there was an error parsing its contents, we continue on to the next file
Expand All @@ -52,6 +61,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

}
if (majorVersion.get() == null) {
log.debug("No .class files located");
return CompiledJavaVersionBuildItem.unknown();
}
return CompiledJavaVersionBuildItem.fromMajorJavaVersion(majorVersion.get());
Expand Down