/*
 * Copyright 2014-2021 Real Logic Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.agrona;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

/**
 * Obtain access the the {@link Unsafe} class for direct memory operations.
 */
public final class UnsafeAccess
{
    /**
     * Reference to the {@link Unsafe} instance.
     */
    public static final Unsafe UNSAFE;
    /**
     * Byte array base offset.
     */
    public static final int ARRAY_BYTE_BASE_OFFSET;

    static
    {
        Unsafe unsafe = null;
        try
        {
            final PrivilegedExceptionAction<Unsafe> action =
                () ->
                {
                    final Field f = Unsafe.class.getDeclaredField("theUnsafe");
                    f.setAccessible(true);

                    return (Unsafe)f.get(null);
                };

            unsafe = AccessController.doPrivileged(action);
        }
        catch (final Exception ex)
        {
            LangUtil.rethrowUnchecked(ex);
        }

        UNSAFE = unsafe;
        ARRAY_BYTE_BASE_OFFSET = Unsafe.ARRAY_BYTE_BASE_OFFSET;
    }

    private UnsafeAccess()
    {
    }
}

@@ -679,95 +679,8 @@
 eld;
-%0Aimport java.security.AccessController;%0Aimport java.security.PrivilegedExceptionAction;
 %0A%0A/*
@@ -1084,56 +1084,81 @@
     
-final PrivilegedExceptionAction%3CUnsafe%3E ac
+unsafe = Unsafe.getUnsafe();%0A        %7D%0A        catch (final Excep
 tion 
-=
+ex)
 %0A   
@@ -1162,24 +1162,26 @@
         
+%7B%0A
         
 () -%3E%0A  
@@ -1176,18 +1176,16 @@
     
-() -%3E%0A
     
+try%0A
     
@@ -1210,20 +1210,16 @@
         
-    
 final Fi
@@ -1286,20 +1286,16 @@
         
-    
 f.setAcc
@@ -1330,18 +1330,16 @@
     
-    return
+unsafe =
  (Un
@@ -1356,20 +1356,16 @@
 (null);%0A
-    
         
@@ -1373,81 +1373,13 @@
    %7D
-;%0A
 %0A    
-        unsafe = AccessController.doPrivileged(action);%0A        %7D%0A
     
@@ -1399,32 +1399,33 @@
 nal Exception ex
+2
 )%0A        %7B%0A    
@@ -1410,34 +1410,42 @@
 on ex2)%0A        
+   
+ 
 %7B%0A
+    
             Lang
@@ -1471,16 +1471,30 @@
 ed(ex);%0A
+            %7D%0A
         

Remove AccessController as it was deprecated in JDK 17.