what-the-bank/sources/com/huawei/hms/framework/common/ReflectionUtils.java

272 lines
9.2 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.huawei.hms.framework.common;
import android.text.TextUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
/* loaded from: classes2.dex */
public class ReflectionUtils {
private static final String TAG = "ReflectionUtils";
private static Object invoke(Object obj, Method method, Object... objArr) throws UnsupportedOperationException {
if (method == null) {
return null;
}
try {
return method.invoke(obj, objArr);
} catch (RuntimeException e) {
Logger.e(TAG, "RuntimeException in invoke:", e);
return null;
} catch (Exception e2) {
Logger.e(TAG, "Exception in invoke:", e2);
return null;
}
}
public static Method getMethod(Class<?> cls, String str, Class<?>... clsArr) {
if (cls == null || str == null) {
Logger.w(TAG, "targetClass is null pr name is null:");
return null;
}
try {
return cls.getDeclaredMethod(str, clsArr);
} catch (NoSuchMethodException e) {
Logger.e(TAG, "NoSuchMethodException:", e);
return null;
} catch (SecurityException e2) {
Logger.e(TAG, "SecurityException:", e2);
return null;
}
}
private static void setClassType(Class<?>[] clsArr, Object obj, int i) {
if (obj instanceof Integer) {
clsArr[i] = Integer.TYPE;
return;
}
if (obj instanceof Long) {
clsArr[i] = Long.TYPE;
return;
}
if (obj instanceof Double) {
clsArr[i] = Double.TYPE;
return;
}
if (obj instanceof Float) {
clsArr[i] = Float.TYPE;
return;
}
if (obj instanceof Boolean) {
clsArr[i] = Boolean.TYPE;
return;
}
if (obj instanceof Character) {
clsArr[i] = Character.TYPE;
return;
}
if (obj instanceof Byte) {
clsArr[i] = Byte.TYPE;
return;
}
if (obj instanceof Void) {
clsArr[i] = Void.TYPE;
} else if (obj instanceof Short) {
clsArr[i] = Short.TYPE;
} else {
clsArr[i] = obj.getClass();
}
}
public static Object invokeStaticMethod(String str, String str2, Object... objArr) {
Class[] clsArr;
if (str == null) {
return null;
}
if (objArr != null) {
int length = objArr.length;
clsArr = new Class[length];
for (int i = 0; i < length; i++) {
setClassType(clsArr, objArr[i], i);
}
} else {
clsArr = null;
}
Method method = getMethod(getClass(str), str2, clsArr);
if (method == null) {
return null;
}
return invoke(null, method, objArr);
}
public static Object invokeStaticMethod(String str, String str2, Class<?>[] clsArr, Object... objArr) {
Method method = getMethod(getClass(str), str2, clsArr);
if (method == null) {
return null;
}
return invoke(null, method, objArr);
}
public static Object getFieldObj(Object obj, String str) {
if (obj == null || TextUtils.isEmpty(str)) {
return null;
}
try {
Field declaredField = obj.getClass().getDeclaredField(str);
AccessController.doPrivileged(new PrivilegedAction(declaredField) { // from class: com.huawei.hms.framework.common.ReflectionUtils.1
final Field val$field;
{
this.val$field = declaredField;
}
@Override // java.security.PrivilegedAction
public Object run() {
this.val$field.setAccessible(true);
return null;
}
});
return declaredField.get(obj);
} catch (IllegalAccessException e) {
Logger.e(TAG, "Exception in getFieldObj :: IllegalAccessException:", e);
return null;
} catch (IllegalArgumentException e2) {
Logger.e(TAG, "Exception in getFieldObj :: IllegalArgumentException:", e2);
return null;
} catch (NoSuchFieldException e3) {
Logger.e(TAG, "Exception in getFieldObj :: NoSuchFieldException:", e3);
return null;
} catch (SecurityException e4) {
Logger.e(TAG, "not security int method getFieldObj,SecurityException:", e4);
return null;
}
}
public static Field getField(Object obj, String str) {
if (obj == null || TextUtils.isEmpty(str)) {
return null;
}
try {
Field declaredField = obj.getClass().getDeclaredField(str);
AccessController.doPrivileged(new PrivilegedAction(declaredField) { // from class: com.huawei.hms.framework.common.ReflectionUtils.2
final Field val$field;
{
this.val$field = declaredField;
}
@Override // java.security.PrivilegedAction
public Object run() {
this.val$field.setAccessible(true);
return null;
}
});
return declaredField;
} catch (IllegalArgumentException e) {
Logger.e(TAG, "Exception in getField :: IllegalArgumentException:", e);
return null;
} catch (NoSuchFieldException e2) {
Logger.e(TAG, "Exception in getField :: NoSuchFieldException:", e2);
return null;
} catch (SecurityException e3) {
Logger.e(TAG, "not security int method getField,SecurityException:", e3);
return null;
}
}
public static Object getStaticFieldObj(String str, String str2) {
Class<?> cls;
if (str == null || (cls = getClass(str)) == null || TextUtils.isEmpty(str2)) {
return null;
}
try {
Field declaredField = cls.getDeclaredField(str2);
AccessController.doPrivileged(new PrivilegedAction(declaredField) { // from class: com.huawei.hms.framework.common.ReflectionUtils.3
final Field val$field;
{
this.val$field = declaredField;
}
@Override // java.security.PrivilegedAction
public Object run() {
this.val$field.setAccessible(true);
return null;
}
});
return declaredField.get(cls);
} catch (IllegalAccessException e) {
Logger.e(TAG, "Exception in getFieldObj :: IllegalAccessException:", e);
return null;
} catch (IllegalArgumentException e2) {
Logger.e(TAG, "Exception in getFieldObj :: IllegalArgumentException:", e2);
return null;
} catch (NoSuchFieldException e3) {
Logger.e(TAG, "Exception in getFieldObj :: NoSuchFieldException:", e3);
return null;
} catch (SecurityException e4) {
Logger.e(TAG, "not security int method getStaticFieldObj,SecurityException:", e4);
return null;
}
}
public static boolean checkCompatible(String str) {
try {
tryLoadClass(str);
return true;
} catch (Exception unused) {
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.append("ClassNotFoundException");
Logger.w(TAG, sb.toString());
return false;
}
}
public static boolean checkCompatible(String str, String str2, Class<?>... clsArr) {
try {
if (str == null || str2 == null) {
Logger.w(TAG, "targetClass is null or name is null:");
return false;
}
Class.forName(str).getDeclaredMethod(str2, clsArr);
StringBuilder sb = new StringBuilder("has method : ");
sb.append(str2);
Logger.v(TAG, sb.toString());
return true;
} catch (RuntimeException unused) {
StringBuilder sb2 = new StringBuilder();
sb2.append(str);
sb2.append(" RuntimeException");
Logger.w(TAG, sb2.toString());
return false;
} catch (Exception unused2) {
StringBuilder sb3 = new StringBuilder();
sb3.append(str2);
sb3.append(" NoSuchMethodException");
Logger.w(TAG, sb3.toString());
return false;
}
}
private static void tryLoadClass(String str) throws ClassNotFoundException {
ClassLoader classLoader = ReflectionUtils.class.getClassLoader();
if (classLoader == null) {
throw new ClassNotFoundException("not found classloader");
}
classLoader.loadClass(str);
}
private static Class<?> getClass(String str) {
if (str == null) {
return null;
}
try {
return Class.forName(str);
} catch (ClassNotFoundException unused) {
return null;
}
}
}