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:
Jesse Wilson
2019-10-26 22:02:49 -04:00
parent 2a79428891
commit eea2981fac

View File

@@ -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,45 +54,29 @@ 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 the JVM's Unsafe mechanism.
try { // public class Unsafe {
Class<?> androidBuildClass = Class.forName("android.os.Build$VERSION"); // public Object allocateInstance(Class<?> type);
Field sdkIntField = androidBuildClass.getDeclaredField("SDK_INT"); // }
int sdk = (int) sdkIntField.get(null); try {
if (sdk >= 29) { Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
tryUnsafe = false; 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);
} }
} catch (Exception ignored) { @Override public String toString() {
} finally { return rawType.getName();
androidChecked = true; }
} };
} } catch (IllegalAccessException e) {
if (tryUnsafe) { throw new AssertionError();
// Try the JVM's Unsafe mechanism. } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException ignored) {
// public class Unsafe { // Not the expected version of the Oracle Java library!
// 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.