what-the-bank/sources/com/google/gson/internal/UnsafeAllocator.java

98 lines
4.5 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.gson.internal;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/* loaded from: classes.dex */
public abstract class UnsafeAllocator {
public abstract <T> T newInstance(Class<T> cls) throws Exception;
public static UnsafeAllocator create() {
try {
Class<?> cls = Class.forName("sun.misc.Unsafe");
Field declaredField = cls.getDeclaredField("theUnsafe");
declaredField.setAccessible(true);
return new UnsafeAllocator(cls.getMethod("allocateInstance", Class.class), declaredField.get(null)) { // from class: com.google.gson.internal.UnsafeAllocator.1
final Method val$allocateInstance;
final Object val$unsafe;
{
this.val$allocateInstance = r1;
this.val$unsafe = r2;
}
@Override // com.google.gson.internal.UnsafeAllocator
public <T> T newInstance(Class<T> cls2) throws Exception {
assertInstantiable(cls2);
return (T) this.val$allocateInstance.invoke(this.val$unsafe, cls2);
}
};
} catch (Exception unused) {
try {
try {
Method declaredMethod = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
declaredMethod.setAccessible(true);
int intValue = ((Integer) declaredMethod.invoke(null, Object.class)).intValue();
Method declaredMethod2 = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, Integer.TYPE);
declaredMethod2.setAccessible(true);
return new UnsafeAllocator(declaredMethod2, intValue) { // from class: com.google.gson.internal.UnsafeAllocator.2
final int val$constructorId;
final Method val$newInstance;
{
this.val$newInstance = declaredMethod2;
this.val$constructorId = intValue;
}
@Override // com.google.gson.internal.UnsafeAllocator
public <T> T newInstance(Class<T> cls2) throws Exception {
assertInstantiable(cls2);
return (T) this.val$newInstance.invoke(null, cls2, Integer.valueOf(this.val$constructorId));
}
};
} catch (Exception unused2) {
return new UnsafeAllocator() { // from class: com.google.gson.internal.UnsafeAllocator.4
@Override // com.google.gson.internal.UnsafeAllocator
public <T> T newInstance(Class<T> cls2) {
throw new UnsupportedOperationException("Cannot allocate ".concat(String.valueOf(cls2)));
}
};
}
} catch (Exception unused3) {
Method declaredMethod3 = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
declaredMethod3.setAccessible(true);
return new UnsafeAllocator(declaredMethod3) { // from class: com.google.gson.internal.UnsafeAllocator.3
final Method val$newInstance;
{
this.val$newInstance = declaredMethod3;
}
@Override // com.google.gson.internal.UnsafeAllocator
public <T> T newInstance(Class<T> cls2) throws Exception {
assertInstantiable(cls2);
return (T) this.val$newInstance.invoke(null, cls2, Object.class);
}
};
}
}
}
static void assertInstantiable(Class<?> cls) {
int modifiers = cls.getModifiers();
if (Modifier.isInterface(modifiers)) {
StringBuilder sb = new StringBuilder("Interface can't be instantiated! Interface name: ");
sb.append(cls.getName());
throw new UnsupportedOperationException(sb.toString());
}
if (Modifier.isAbstract(modifiers)) {
StringBuilder sb2 = new StringBuilder("Abstract class can't be instantiated! Class name: ");
sb2.append(cls.getName());
throw new UnsupportedOperationException(sb2.toString());
}
}
}