mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Don't try to access unsafe APIs on android 29+ (#913)
* Don't try to access unsafe APIs on android 29+ Resolves #872. Not sure if we want to try caching some of these lookups * Checkstyle * Use correct inner class naming Co-Authored-By: Jake Wharton <jakew@google.com> * Name variable ignored * Cache android check
This commit is contained in:
@@ -34,6 +34,9 @@ abstract class ClassFactory<T> {
|
|||||||
abstract T newInstance() throws
|
abstract T newInstance() throws
|
||||||
InvocationTargetException, IllegalAccessException, InstantiationException;
|
InvocationTargetException, IllegalAccessException, InstantiationException;
|
||||||
|
|
||||||
|
private static volatile boolean androidChecked = false;
|
||||||
|
private static volatile boolean tryUnsafe = true;
|
||||||
|
|
||||||
public static <T> ClassFactory<T> get(final Class<?> rawType) {
|
public static <T> ClassFactory<T> get(final Class<?> rawType) {
|
||||||
// Try to find a no-args constructor. May be any visibility including private.
|
// Try to find a no-args constructor. May be any visibility including private.
|
||||||
try {
|
try {
|
||||||
@@ -54,29 +57,45 @@ abstract class ClassFactory<T> {
|
|||||||
// No no-args constructor. Fall back to something more magical...
|
// No no-args constructor. Fall back to something more magical...
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try the JVM's Unsafe mechanism.
|
if (!androidChecked) {
|
||||||
// public class Unsafe {
|
try {
|
||||||
// public Object allocateInstance(Class<?> type);
|
Class<?> androidBuildClass = Class.forName("android.os.Build$VERSION");
|
||||||
// }
|
Field sdkIntField = androidBuildClass.getDeclaredField("SDK_INT");
|
||||||
try {
|
int sdk = (int) sdkIntField.get(null);
|
||||||
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
|
if (sdk >= 29) {
|
||||||
Field f = unsafeClass.getDeclaredField("theUnsafe");
|
tryUnsafe = false;
|
||||||
f.setAccessible(true);
|
|
||||||
final Object unsafe = f.get(null);
|
|
||||||
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
|
|
||||||
return new ClassFactory<T>() {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override public T newInstance() throws InvocationTargetException, IllegalAccessException {
|
|
||||||
return (T) allocateInstance.invoke(unsafe, rawType);
|
|
||||||
}
|
}
|
||||||
@Override public String toString() {
|
} catch (Exception ignored) {
|
||||||
return rawType.getName();
|
} finally {
|
||||||
}
|
androidChecked = true;
|
||||||
};
|
}
|
||||||
} catch (IllegalAccessException e) {
|
}
|
||||||
throw new AssertionError();
|
if (tryUnsafe) {
|
||||||
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException ignored) {
|
// Try the JVM's Unsafe mechanism.
|
||||||
// Not the expected version of the Oracle Java library!
|
// public class Unsafe {
|
||||||
|
// public Object allocateInstance(Class<?> type);
|
||||||
|
// }
|
||||||
|
try {
|
||||||
|
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
|
||||||
|
Field f = unsafeClass.getDeclaredField("theUnsafe");
|
||||||
|
f.setAccessible(true);
|
||||||
|
final Object unsafe = f.get(null);
|
||||||
|
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
|
||||||
|
return new ClassFactory<T>() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public T newInstance() throws InvocationTargetException, IllegalAccessException {
|
||||||
|
return (T) allocateInstance.invoke(unsafe, rawType);
|
||||||
|
}
|
||||||
|
@Override public String toString() {
|
||||||
|
return rawType.getName();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new AssertionError();
|
||||||
|
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException ignored) {
|
||||||
|
// Not the expected version of the Oracle Java library!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try (post-Gingerbread) Dalvik/libcore's ObjectStreamClass mechanism.
|
// Try (post-Gingerbread) Dalvik/libcore's ObjectStreamClass mechanism.
|
||||||
|
Reference in New Issue
Block a user