UnsafeDetectUtil also checks that the Unsafe class has the field theUnsafe to make it work on Android platform.
This commit is contained in:
parent
59e5f2f262
commit
fbb1432e8e
@ -15,26 +15,44 @@
|
||||
*/
|
||||
package io.netty.util;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
/**
|
||||
* Utility which checks if {@value #UNSAFE} class can be found in the classpath
|
||||
* and that it can be accessed using "theUnsafe" field which is not true for all platforms, i.e Android
|
||||
* where it is called "THE_ONE".
|
||||
*/
|
||||
public final class UnsafeDetectUtil {
|
||||
|
||||
private static final String THE_UNSAFE = "theUnsafe";
|
||||
private static final String UNSAFE = "sun.misc.Unsafe";
|
||||
private static final boolean UNSAFE_FOUND = isUnsafeFound(AtomicInteger.class.getClassLoader());
|
||||
|
||||
public static boolean isUnsafeFound(ClassLoader loader) {
|
||||
try {
|
||||
Class.forName(UNSAFE, true, loader);
|
||||
return true;
|
||||
Class<?> unsafeClazz = Class.forName(UNSAFE, true, loader);
|
||||
return hasUnsafeField(unsafeClazz);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
return false;
|
||||
} catch (PrivilegedActionException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static boolean hasUnsafeField(final Class<?> unsafeClass) throws PrivilegedActionException {
|
||||
return AccessController.doPrivileged (new PrivilegedExceptionAction<Boolean>() {
|
||||
public Boolean run() throws Exception {
|
||||
unsafeClass.getDeclaredField(THE_UNSAFE);
|
||||
return true;
|
||||
}});
|
||||
}
|
||||
|
||||
public static boolean isUnsafeFound() {
|
||||
return UNSAFE_FOUND;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user