RFR: 8365588: defineClass that accepts a ByteBuffer does not work as expected [v2]
Xueming Shen
sherman at openjdk.org
Thu Oct 9 21:37:05 UTC 2025
On Wed, 8 Oct 2025 05:11:12 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> --add-opens java.base/java.lang=ALL-UNNAMED is for the reflective access to "defineClass" of the platform classloader. any better alternative to test the platform classloader's defineClass behavior other than the reflect api?
>>
>>
>> Method m = ClassLoader.class.getDeclaredMethod(
>> "defineClass", String.class, ByteBuffer.class, ProtectionDomain.class
>> );
>> m.setAccessible(true);
>> Class<?> clazz = (Class<?>) m.invoke(builtin, null, bb, null);
>>
>>
>> The dup @run lines are the workaround to test the platform classloader's defineClass on the same target test class class twice, one for the direct bb, one for the heap bb (with 2 separate vm runs). Otherwise the test fails with "java.lang.LinkageError: loader 'platform' attempted duplicate class definition for Greeting."
>>
>> Does ParameterizedTest support vm/platform classloader isolation for each parameterized test run against the same test with different parameters?
>
>> Does ParameterizedTest support vm/platform classloader isolation for each parameterized test run against the same test with different parameters?
>
> I should have been clearer in my comment. The suggestion to use a ParameterizedTest is to exercise a custom class loader using super.defineClass with different buffers (direct, heap, position != 0, ...) so that it exercises the "non-trusted" cases.
>
> You are right that testing the platform class loader (as a trusted class loader) will require opening java.lang.
The test case has been updated to use @ParameterizedTest for the user-defined classloader, for the following variants.
static Stream<Arguments> bufferTypes() {
return Stream.of(
arguments(ARRAY_BUFFER, 0, false),
arguments(ARRAY_BUFFER_READONLY, 0, true),
arguments(DIRECT_BUFFER, 0, false),
arguments(DIRECT_BUFFER_READONLY, 0, false),
arguments(ARRAY_BUFFER, 16, false),
arguments(ARRAY_BUFFER_READONLY, 16, true),
arguments(DIRECT_BUFFER, 16, false),
arguments(DIRECT_BUFFER_READONLY, 16, false)
);
}
I keep the built-in classloader testing asis in the same test file for now. Let me know if you have a strong opinion to split it out.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27569#discussion_r2418010283
More information about the core-libs-dev
mailing list