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

Code before:
                            cls.getDeclaredMethod("staticFieldOffset", Field.class);
                            cls.getDeclaredMethod("staticFieldBase", Field.class);
                            cls.getDeclaredMethod("arrayBaseOffset", Class.class);
                            cls.getDeclaredMethod("arrayIndexScale", Class.class);
                            cls.getDeclaredMethod("allocateMemory", long.class);
                            cls.getDeclaredMethod("reallocateMemory", long.class, long.class);
                            cls.getDeclaredMethod("freeMemory", long.class);
                            cls.getDeclaredMethod("setMemory", long.class, long.class, byte.class);
                            cls.getDeclaredMethod("setMemory", Object.class, long.class, long.class, byte.class);
                            cls.getDeclaredMethod(
                                    "copyMemory", Object.class, long.class, Object.class, long.class, long.class);
                            cls.getDeclaredMethod("getBoolean", Object.class, long.class);
                            cls.getDeclaredMethod("getByte", long.class);
                            cls.getDeclaredMethod("getByte", Object.class, long.class);
                            cls.getDeclaredMethod("getInt", long.class);
                            cls.getDeclaredMethod("getInt", Object.class, long.class);
                            cls.getDeclaredMethod("getLong", long.class);
                            cls.getDeclaredMethod("getLong", Object.class, long.class);
                            cls.getDeclaredMethod("putByte", long.class, byte.class);
                            cls.getDeclaredMethod("putByte", Object.class, long.class, byte.class);
                            cls.getDeclaredMethod("putInt", long.class, int.class);
                            cls.getDeclaredMethod("putInt", Object.class, long.class, int.class);
                            cls.getDeclaredMethod("putLong", long.class, long.class);
                            cls.getDeclaredMethod("putLong", Object.class, long.class, long.class);
                            cls.getDeclaredMethod("addressSize");
                            // The following may throw UnsupportedOperationException:
                            long address = finalUnsafe.allocateMemory(Long.BYTES);
                            finalUnsafe.putLong(address, 42);
                            finalUnsafe.freeMemory(address);
                            return null;
                        } catch (UnsupportedOperationException e) {
                            return e;
                        } catch (NoSuchMethodException e) {
                            return e;
                        } catch (SecurityException e) {
                            return e;
                        }
                    }
                });

                if (maybeException == null) {
                    logger.debug("sun.misc.Unsafe base methods: all available");
                } else {
                    // Unsafe.copyMemory(Object, long, Object, long, long) unavailable.
                    unsafe = null;
                    unsafeUnavailabilityCause = (Throwable) maybeException;
                    if (logger.isTraceEnabled()) {
                        logger.debug("sun.misc.Unsafe method unavailable:", unsafeUnavailabilityCause);
                    } else {
                        logger.debug("sun.misc.Unsafe method unavailable: {}",
                                ((Throwable) maybeException).getMessage());

Review comment (suggestion):
Maybe this could be rephrased to something like "The following tests that we can allocate raw memory and if not we'll expect it to throw an UnsupportedOperationException".

Code after:
                            cls.getDeclaredMethod("staticFieldBase", Field.class);
                            cls.getDeclaredMethod("arrayBaseOffset", Class.class);
                            cls.getDeclaredMethod("arrayIndexScale", Class.class);
                            cls.getDeclaredMethod("allocateMemory", long.class);
                            cls.getDeclaredMethod("reallocateMemory", long.class, long.class);
                            cls.getDeclaredMethod("freeMemory", long.class);
                            cls.getDeclaredMethod("setMemory", long.class, long.class, byte.class);
                            cls.getDeclaredMethod("setMemory", Object.class, long.class, long.class, byte.class);
                            cls.getDeclaredMethod(
                                    "copyMemory", Object.class, long.class, Object.class, long.class, long.class);
                            cls.getDeclaredMethod("getBoolean", Object.class, long.class);
                            cls.getDeclaredMethod("getByte", long.class);
                            cls.getDeclaredMethod("getByte", Object.class, long.class);
                            cls.getDeclaredMethod("getInt", long.class);
                            cls.getDeclaredMethod("getInt", Object.class, long.class);
                            cls.getDeclaredMethod("getLong", long.class);
                            cls.getDeclaredMethod("getLong", Object.class, long.class);
                            cls.getDeclaredMethod("putByte", long.class, byte.class);
                            cls.getDeclaredMethod("putByte", Object.class, long.class, byte.class);
                            cls.getDeclaredMethod("putInt", long.class, int.class);
                            cls.getDeclaredMethod("putInt", Object.class, long.class, int.class);
                            cls.getDeclaredMethod("putLong", long.class, long.class);
                            cls.getDeclaredMethod("putLong", Object.class, long.class, long.class);
                            cls.getDeclaredMethod("addressSize");
                            // The following tests the methods are usable.
                            // Will throw UnsupportedOperationException if unsafe memory access is denied:
                            long address = finalUnsafe.allocateMemory(Long.BYTES);
                            finalUnsafe.putLong(address, 42);
                            finalUnsafe.freeMemory(address);
                            return null;
                        } catch (UnsupportedOperationException e) {
                            return e;
                        } catch (NoSuchMethodException e) {
                            return e;
                        } catch (SecurityException e) {
                            return e;
                        }
                    }
                });

                if (maybeException == null) {
                    logger.debug("sun.misc.Unsafe base methods: all available");
                } else {
                    // Unsafe.copyMemory(Object, long, Object, long, long) unavailable.
                    unsafe = null;
                    unsafeUnavailabilityCause = (Throwable) maybeException;
                    if (logger.isTraceEnabled()) {
                        logger.debug("sun.misc.Unsafe method unavailable:", unsafeUnavailabilityCause);
                    } else {
                        logger.debug("sun.misc.Unsafe method unavailable: {}",
                                ((Throwable) maybeException).getMessage());