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

Code before:
     * @return
     * {@code true}: if this {@code thread} is a virtual thread.
     */
    static boolean isVirtualThread(Thread thread) {
        // Quick exclusion.
        if (thread == null || JAVA_VERSION < 19 || thread instanceof FastThreadLocalThread) {
            return false;
        }
        // Uncommon case: `IS_VIRTUAL_THREAD_METHOD_HANDLE` is null.
        if (IS_VIRTUAL_THREAD_METHOD_HANDLE == null) {
            Class<?> clazz = thread.getClass();
            if (clazz == Thread.class || (clazz = clazz.getSuperclass()) == Thread.class) {
                return false;
            }
            return BASE_VIRTUAL_THREAD_CLASS_BINARY_NAME.equals(clazz.getName());
        }
        // Preferred fast check path:
        try {
            return (boolean) IS_VIRTUAL_THREAD_METHOD_HANDLE.invokeExact(thread);
        } catch (Throwable t) {
            // Should not happen.
            if (t instanceof Error) {
                throw (Error) t;
            }
            throw new Error(t);
        }
    }

    private static boolean unsafeStaticFieldOffsetSupported() {
        return !RUNNING_IN_NATIVE_IMAGE;
    }

    static boolean isExplicitNoUnsafe() {
        return EXPLICIT_NO_UNSAFE_CAUSE != null;
    }

    private static Throwable explicitNoUnsafeCause0() {
        boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
        logger.debug("-Dio.netty.noUnsafe: {}", noUnsafe);

        // See JDK 23 JEP 471 https://openjdk.org/jeps/471 and sun.misc.Unsafe.beforeMemoryAccess() on JDK 23+.
        String unsafeMemoryAccess = SystemPropertyUtil.get("sun.misc.unsafe.memory.access", "<unspecified>");
        if (!("allow".equals(unsafeMemoryAccess) || "<unspecified>".equals(unsafeMemoryAccess))) {
            logger.debug("--sun-misc-unsafe-memory-access={}", unsafeMemoryAccess);
            noUnsafe = true;
        }

        if (noUnsafe) {
            logger.debug("sun.misc.Unsafe: unavailable (io.netty.noUnsafe)");
            return new UnsupportedOperationException("sun.misc.Unsafe: unavailable (io.netty.noUnsafe)");
        }

Review comment (refactor):
We can simplify this because the method handle being `null` implies the version check, and after optimization there's no overhead to the `invokeExact`, so there's no need to do the `instanceof` ourselves, and we can entirely avoid talking about the `BaseVirtualThread` class.

```suggestion
    /**
     * @param thread The thread to be checked.
     * @return {@code true}: if this {@code thread} is a virtual thread.
     */
    static boolean isVirtualThread(Thread thread) {
        // Quick exclusion.
        if (thread == null || IS_VIRTUAL_THREAD_METHOD_HANDLE == null) {
            return false;
        }
        // Check path:
        try {
            return (boolean) IS_VIRTUAL_THREAD_METHOD_HANDLE.invokeExact(thread);
        } catch (Throwable t) {
            // Should not happen.
            if (t instanceof Error) {
                throw (Error) t;
            }
            throw new Error(t);
        }
```

Code after:
        try {
            return (boolean) IS_VIRTUAL_THREAD_METHOD_HANDLE.invokeExact(thread);
        } catch (Throwable t) {
            // Should not happen.
            if (t instanceof Error) {
                throw (Error) t;
            }
            throw new Error(t);
        }
    }

    private static boolean unsafeStaticFieldOffsetSupported() {
        return !RUNNING_IN_NATIVE_IMAGE;
    }

    static boolean isExplicitNoUnsafe() {
        return EXPLICIT_NO_UNSAFE_CAUSE != null;
    }

    private static Throwable explicitNoUnsafeCause0() {
        boolean noUnsafe = SystemPropertyUtil.getBoolean("io.netty.noUnsafe", false);
        logger.debug("-Dio.netty.noUnsafe: {}", noUnsafe);

        // See JDK 23 JEP 471 https://openjdk.org/jeps/471 and sun.misc.Unsafe.beforeMemoryAccess() on JDK 23+.
        String unsafeMemoryAccess = SystemPropertyUtil.get("sun.misc.unsafe.memory.access", "<unspecified>");
        if (!("allow".equals(unsafeMemoryAccess) || "<unspecified>".equals(unsafeMemoryAccess))) {
            logger.debug("--sun-misc-unsafe-memory-access={}", unsafeMemoryAccess);
            noUnsafe = true;
        }

        if (noUnsafe) {
            logger.debug("sun.misc.Unsafe: unavailable (io.netty.noUnsafe)");
            return new UnsupportedOperationException("sun.misc.Unsafe: unavailable (io.netty.noUnsafe)");
        }

        // Legacy properties
        String unsafePropName;
        if (SystemPropertyUtil.contains("io.netty.tryUnsafe")) {
            unsafePropName = "io.netty.tryUnsafe";
        } else {
            unsafePropName = "org.jboss.netty.tryUnsafe";
        }

        if (!SystemPropertyUtil.getBoolean(unsafePropName, true)) {
            String msg = "sun.misc.Unsafe: unavailable (" + unsafePropName + ')';
            logger.debug(msg);
            return new UnsupportedOperationException(msg);
        }

        return null;
    }