Skip to content

Commit

Permalink
Let JacocoCoverageRunner identify the classpath on JDK 16+
Browse files Browse the repository at this point in the history
The Unsafe-backed extraction of the classpath in
`JacocoCoverageRunner#getClassLoaderUrls` has to be adapted for JDK 16+
as the private `ucp` field on `AppClassLoader` has been moved to its
superclass.

With this commit, the existing coverage integration tests pass for the
JDK 17 toolchain.

Closes #15081.

PiperOrigin-RevId: 439518636
  • Loading branch information
fmeum authored and copybara-github committed Apr 5, 2022
1 parent 78d37e2 commit fac463d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,17 @@ private static URL[] getClassLoaderUrls(ClassLoader classLoader) {
field.setAccessible(true);
Unsafe unsafe = (Unsafe) field.get(null);

// jdk.internal.loader.ClassLoaders.AppClassLoader.ucp
Field ucpField = classLoader.getClass().getDeclaredField("ucp");
Field ucpField;
try {
// Java 9-15:
// jdk.internal.loader.ClassLoaders.AppClassLoader.ucp
ucpField = classLoader.getClass().getDeclaredField("ucp");
} catch (NoSuchFieldException e) {
// Java 16+:
// jdk.internal.loader.BuiltinClassLoader.ucp
// https://github.com/openjdk/jdk/commit/03a4df0acd103702e52dcd01c3f03fda4d7b04f5#diff-32cc12c0e3172fe5f2da1f65a75fa1cb920c39040d06323c83ad2c4d84e095aaL147
ucpField = classLoader.getClass().getSuperclass().getDeclaredField("ucp");
}
long ucpFieldOffset = unsafe.objectFieldOffset(ucpField);
Object ucpObject = unsafe.getObject(classLoader, ucpFieldOffset);

Expand Down
3 changes: 2 additions & 1 deletion src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ sh_test(
],
tags = ["no_windows"],
)
for java_version in JAVA_VERSIONS_COVERAGE
# TODO: Update JAVA_VERSIONS_COVERAGE on the next java_tools release.
for java_version in JAVA_VERSIONS_COVERAGE + ("17",)
]

sh_test(
Expand Down

0 comments on commit fac463d

Please sign in to comment.