private static void lookupCleanMethodPrivileged() {
        if (VersionFinder.JAVA_MAJOR_VERSION < 9) {
            try {
                // See: https://stackoverflow.com/a/19447758/3950982
                cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");
                cleanMethod.setAccessible(true);
                final Class<?> directByteBufferClass = Class.forName("sun.nio.ch.DirectBuffer");
                attachmentMethod = directByteBufferClass.getMethod("attachment");
                attachmentMethod.setAccessible(true);
            } catch (final SecurityException e) {
                throw ClassGraphException.newClassGraphException(
                        "You need to grant classgraph RuntimePermission(\"accessClassInPackage.sun.misc\") "
                                + "and ReflectPermission(\"suppressAccessChecks\")");
            } catch (final ReflectiveOperationException | LinkageError ex) {
                // Ignore
            }
        } else {
            try {
                Class<?> unsafeClass;
                try {
                    unsafeClass = Class.forName("sun.misc.Unsafe");
                } catch (final ReflectiveOperationException | LinkageError e) {
                    // jdk.internal.misc.Unsafe doesn't yet have an invokeCleaner() method,
                    // but that method should be added if sun.misc.Unsafe is removed.
                    unsafeClass = Class.forName("jdk.internal.misc.Unsafe");
                }
                cleanMethod = unsafeClass.getMethod("invokeCleaner", ByteBuffer.class);
                cleanMethod.setAccessible(true);
                final Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe");
                theUnsafeField.setAccessible(true);
                theUnsafe = theUnsafeField.get(null);
            } catch (final SecurityException e) {
                throw ClassGraphException.newClassGraphException(
                        "You need to grant classgraph RuntimePermission(\"accessClassInPackage.sun.misc\"), "
                                + "RuntimePermission(\"accessClassInPackage.jdk.internal.misc\") "
                                + "and ReflectPermission(\"suppressAccessChecks\")");
            } catch (final ReflectiveOperationException | LinkageError ex) {
                // Ignore
            }
        }
    }
private static void lookupCleanMethodPrivileged ( ) { if ( VersionFinder . JAVA_MAJOR_VERSION < 9 ) { try { cleanMethod = Class . forName ( "sun.misc.Cleaner" ) . getMethod ( "clean" ) ; cleanMethod . setAccessible ( true ) ; final Class < ? > directByteBufferClass = Class . forName ( "sun.nio.ch.DirectBuffer" ) ; attachmentMethod = directByteBufferClass . getMethod ( "attachment" ) ; attachmentMethod . setAccessible ( true ) ; } catch ( final SecurityException e ) { throw ClassGraphException . newClassGraphException ( "You need to grant classgraph RuntimePermission(\"accessClassInPackage.sun.misc\") " + "and ReflectPermission(\"suppressAccessChecks\")" ) ; } catch ( final ReflectiveOperationException | LinkageError ex ) { } } else { try { Class < ? > unsafeClass ; try { unsafeClass = Class . forName ( "sun.misc.Unsafe" ) ; } catch ( final ReflectiveOperationException | LinkageError e ) { unsafeClass = Class . forName ( "jdk.internal.misc.Unsafe" ) ; } cleanMethod = unsafeClass . getMethod ( "invokeCleaner" , ByteBuffer . class ) ; cleanMethod . setAccessible ( true ) ; final Field theUnsafeField = unsafeClass . getDeclaredField ( "theUnsafe" ) ; theUnsafeField . setAccessible ( true ) ; theUnsafe = theUnsafeField . get ( null ) ; } catch ( final SecurityException e ) { throw ClassGraphException . newClassGraphException ( "You need to grant classgraph RuntimePermission(\"accessClassInPackage.sun.misc\"), " + "RuntimePermission(\"accessClassInPackage.jdk.internal.misc\") " + "and ReflectPermission(\"suppressAccessChecks\")" ) ; } catch ( final ReflectiveOperationException | LinkageError ex ) { } } }
Get the clean() method, attachment() method, and theUnsafe field, called inside doPrivileged.