Repository: netty/netty
File: common/src/main/java/io/netty/util/internal/CleanerJava25.java (Java)

Code before:
 */
package io.netty.util.internal;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;

import static java.lang.invoke.MethodType.methodType;

/**
 * Provide a way to clean direct {@link ByteBuffer} instances on Java 24+,
 * where we don't have {@code Unsafe} available, but we have memory segments.
 */
final class CleanerJava25 implements Cleaner {
    private static final InternalLogger logger;

    private static final MethodHandle INVOKE_ALLOCATOR;

    static {
        boolean suitableJavaVersion;
        if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) {
            // native image supports this since 25, but we don't use PlatformDependent0 here, since
            // we need to initialize CleanerJava24 at build time.
            String v = System.getProperty("java.specification.version");
            try {
                suitableJavaVersion = Integer.parseInt(v) >= 25;
            } catch (NumberFormatException e) {
                suitableJavaVersion = false;
            }
            // also need to prevent initializing the logger at build time
            logger = null;
        } else {
            // Only attempt to use MemorySegments on Java 25 or greater, because of the following JDK bugs:
            // - https://bugs.openjdk.org/browse/JDK-8357145
            // - https://bugs.openjdk.org/browse/JDK-8357268
            suitableJavaVersion = PlatformDependent0.javaVersion() >= 25;
            logger = InternalLoggerFactory.getInstance(CleanerJava25.class);
        }

        MethodHandle method;
        Throwable error;
        if (suitableJavaVersion) {
            try {
                // Here we compose and construct a MethodHandle that takes an 'int' capacity argument,
                // and produces a 'CleanableDirectBufferImpl' instance.
                // The method handle will create a new shared Arena instance, allocate a MemorySegment from it,
                // convert the MemorySegment to a ByteBuffer and a memory address, and then pass both the Arena,
                // the ByteBuffer, and the memory address to the CleanableDirectBufferImpl constructor,

Review comment (suggestion):
```suggestion
            // we need to initialize CleanerJava25 at build time.
```

Code after:
 */
package io.netty.util.internal;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;

import static java.lang.invoke.MethodType.methodType;

/**
 * Provide a way to clean direct {@link ByteBuffer} instances on Java 24+,
 * where we don't have {@code Unsafe} available, but we have memory segments.
 */
final class CleanerJava25 implements Cleaner {
    private static final InternalLogger logger;

    private static final MethodHandle INVOKE_ALLOCATOR;

    static {
        boolean suitableJavaVersion;
        if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) {
            // native image supports this since 25, but we don't use PlatformDependent0 here, since
            // we need to initialize CleanerJava25 at build time.
            String v = System.getProperty("java.specification.version");
            try {
                suitableJavaVersion = Integer.parseInt(v) >= 25;
            } catch (NumberFormatException e) {
                suitableJavaVersion = false;
            }
            // also need to prevent initializing the logger at build time
            logger = null;
        } else {
            // Only attempt to use MemorySegments on Java 25 or greater, because of the following JDK bugs:
            // - https://bugs.openjdk.org/browse/JDK-8357145
            // - https://bugs.openjdk.org/browse/JDK-8357268
            suitableJavaVersion = PlatformDependent0.javaVersion() >= 25;
            logger = InternalLoggerFactory.getInstance(CleanerJava25.class);
        }

        MethodHandle method;
        Throwable error;
        if (suitableJavaVersion) {
            try {
                // Here we compose and construct a MethodHandle that takes an 'int' capacity argument,
                // and produces a 'CleanableDirectBufferImpl' instance.
                // The method handle will create a new shared Arena instance, allocate a MemorySegment from it,
                // convert the MemorySegment to a ByteBuffer and a memory address, and then pass both the Arena,
                // the ByteBuffer, and the memory address to the CleanableDirectBufferImpl constructor,