mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Go back to using Unsafe APIs to create instances without constructors.
The attempted fix replaced a warning with a crash. I believe that a
warning is a better developer experience than a crash.
This reverts commit c0639316b1
.
This commit is contained in:
@@ -34,9 +34,6 @@ 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 {
|
||||||
@@ -57,20 +54,6 @@ abstract class ClassFactory<T> {
|
|||||||
// No no-args constructor. Fall back to something more magical...
|
// No no-args constructor. Fall back to something more magical...
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!androidChecked) {
|
|
||||||
try {
|
|
||||||
Class<?> androidBuildClass = Class.forName("android.os.Build$VERSION");
|
|
||||||
Field sdkIntField = androidBuildClass.getDeclaredField("SDK_INT");
|
|
||||||
int sdk = (int) sdkIntField.get(null);
|
|
||||||
if (sdk >= 29) {
|
|
||||||
tryUnsafe = false;
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
} finally {
|
|
||||||
androidChecked = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (tryUnsafe) {
|
|
||||||
// Try the JVM's Unsafe mechanism.
|
// Try the JVM's Unsafe mechanism.
|
||||||
// public class Unsafe {
|
// public class Unsafe {
|
||||||
// public Object allocateInstance(Class<?> type);
|
// public Object allocateInstance(Class<?> type);
|
||||||
@@ -83,8 +66,7 @@ abstract class ClassFactory<T> {
|
|||||||
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
|
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
|
||||||
return new ClassFactory<T>() {
|
return new ClassFactory<T>() {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override public T newInstance() throws InvocationTargetException, IllegalAccessException {
|
||||||
public T newInstance() throws InvocationTargetException, IllegalAccessException {
|
|
||||||
return (T) allocateInstance.invoke(unsafe, rawType);
|
return (T) allocateInstance.invoke(unsafe, rawType);
|
||||||
}
|
}
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
@@ -96,7 +78,6 @@ abstract class ClassFactory<T> {
|
|||||||
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException ignored) {
|
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException ignored) {
|
||||||
// Not the expected version of the Oracle Java library!
|
// 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.
|
||||||
// public class ObjectStreamClass {
|
// public class ObjectStreamClass {
|
||||||
|
Reference in New Issue
Block a user