141 lines
5.5 KiB
Java
141 lines
5.5 KiB
Java
package com.squareup.leakcanary;
|
|
|
|
import com.baidu.cloud.media.player.BDCloudMediaPlayer;
|
|
import com.squareup.haha.perflib.ArrayInstance;
|
|
import com.squareup.haha.perflib.ClassInstance;
|
|
import com.squareup.haha.perflib.ClassObj;
|
|
import com.squareup.haha.perflib.Instance;
|
|
import com.squareup.haha.perflib.Type;
|
|
import com.unity3d.ads.metadata.MediationMetaData;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.nio.charset.Charset;
|
|
import java.util.Arrays;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
/* loaded from: classes.dex */
|
|
public final class HahaHelper {
|
|
private static final Set<String> WRAPPER_TYPES = new HashSet(Arrays.asList(Boolean.class.getName(), Character.class.getName(), Float.class.getName(), Double.class.getName(), Byte.class.getName(), Short.class.getName(), Integer.class.getName(), Long.class.getName()));
|
|
|
|
private HahaHelper() {
|
|
throw new AssertionError();
|
|
}
|
|
|
|
static String asString(Object obj) {
|
|
Preconditions.checkNotNull(obj, "stringObject");
|
|
Instance instance = (Instance) obj;
|
|
List<ClassInstance.FieldValue> classInstanceValues = classInstanceValues(instance);
|
|
Integer num = (Integer) fieldValue(classInstanceValues, "count");
|
|
Preconditions.checkNotNull(num, "count");
|
|
if (num.intValue() == 0) {
|
|
return "";
|
|
}
|
|
Object fieldValue = fieldValue(classInstanceValues, "value");
|
|
Preconditions.checkNotNull(fieldValue, "value");
|
|
if (isCharArray(fieldValue)) {
|
|
ArrayInstance arrayInstance = (ArrayInstance) fieldValue;
|
|
Integer num2 = 0;
|
|
if (hasField(classInstanceValues, BDCloudMediaPlayer.OnNativeInvokeListener.ARG_OFFSET)) {
|
|
num2 = (Integer) fieldValue(classInstanceValues, BDCloudMediaPlayer.OnNativeInvokeListener.ARG_OFFSET);
|
|
Preconditions.checkNotNull(num2, BDCloudMediaPlayer.OnNativeInvokeListener.ARG_OFFSET);
|
|
}
|
|
return new String(arrayInstance.asCharArray(num2.intValue(), num.intValue()));
|
|
}
|
|
if (!isByteArray(fieldValue)) {
|
|
throw new UnsupportedOperationException("Could not find char array in " + instance);
|
|
}
|
|
ArrayInstance arrayInstance2 = (ArrayInstance) fieldValue;
|
|
try {
|
|
Method declaredMethod = ArrayInstance.class.getDeclaredMethod("asRawByteArray", Integer.TYPE, Integer.TYPE);
|
|
declaredMethod.setAccessible(true);
|
|
return new String((byte[]) declaredMethod.invoke(arrayInstance2, 0, num), Charset.forName("UTF-8"));
|
|
} catch (IllegalAccessException e) {
|
|
throw new RuntimeException(e);
|
|
} catch (NoSuchMethodException e2) {
|
|
throw new RuntimeException(e2);
|
|
} catch (InvocationTargetException e3) {
|
|
throw new RuntimeException(e3);
|
|
}
|
|
}
|
|
|
|
static List<ClassInstance.FieldValue> classInstanceValues(Instance instance) {
|
|
return ((ClassInstance) instance).getValues();
|
|
}
|
|
|
|
static boolean extendsThread(ClassObj classObj) {
|
|
while (classObj.getSuperClassObj() != null) {
|
|
if (classObj.getClassName().equals(Thread.class.getName())) {
|
|
return true;
|
|
}
|
|
classObj = classObj.getSuperClassObj();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static <T> T fieldValue(List<ClassInstance.FieldValue> list, String str) {
|
|
for (ClassInstance.FieldValue fieldValue : list) {
|
|
if (fieldValue.getField().getName().equals(str)) {
|
|
return (T) fieldValue.getValue();
|
|
}
|
|
}
|
|
throw new IllegalArgumentException("Field " + str + " does not exists");
|
|
}
|
|
|
|
static boolean hasField(List<ClassInstance.FieldValue> list, String str) {
|
|
Iterator<ClassInstance.FieldValue> it = list.iterator();
|
|
while (it.hasNext()) {
|
|
if (it.next().getField().getName().equals(str)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static boolean isByteArray(Object obj) {
|
|
return (obj instanceof ArrayInstance) && ((ArrayInstance) obj).getArrayType() == Type.BYTE;
|
|
}
|
|
|
|
private static boolean isCharArray(Object obj) {
|
|
return (obj instanceof ArrayInstance) && ((ArrayInstance) obj).getArrayType() == Type.CHAR;
|
|
}
|
|
|
|
public static boolean isPrimitiveOrWrapperArray(Object obj) {
|
|
if (!(obj instanceof ArrayInstance)) {
|
|
return false;
|
|
}
|
|
ArrayInstance arrayInstance = (ArrayInstance) obj;
|
|
if (arrayInstance.getArrayType() != Type.OBJECT) {
|
|
return true;
|
|
}
|
|
return WRAPPER_TYPES.contains(arrayInstance.getClassObj().getClassName());
|
|
}
|
|
|
|
public static boolean isPrimitiveWrapper(Object obj) {
|
|
if (obj instanceof ClassInstance) {
|
|
return WRAPPER_TYPES.contains(((ClassInstance) obj).getClassObj().getClassName());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static String threadName(Instance instance) {
|
|
Object fieldValue = fieldValue(classInstanceValues(instance), MediationMetaData.KEY_NAME);
|
|
return fieldValue == null ? "Thread name not available" : asString(fieldValue);
|
|
}
|
|
|
|
static String valueAsString(Object obj) {
|
|
if (obj == null) {
|
|
return "null";
|
|
}
|
|
if (!(obj instanceof ClassInstance)) {
|
|
return obj.toString();
|
|
}
|
|
if (!((ClassInstance) obj).getClassObj().getClassName().equals(String.class.getName())) {
|
|
return obj.toString();
|
|
}
|
|
return '\"' + asString(obj) + '\"';
|
|
}
|
|
}
|