what-the-bank/sources/io/grpc/okhttp/internal/OptionalMethod.java

114 lines
3.8 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package io.grpc.okhttp.internal;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/* loaded from: classes6.dex */
public class OptionalMethod<T> {
private final String methodName;
private final Class[] methodParams;
private final Class<?> returnType;
public OptionalMethod(Class<?> cls, String str, Class... clsArr) {
this.returnType = cls;
this.methodName = str;
this.methodParams = clsArr;
}
public boolean isSupported(T t) {
return getMethod(t.getClass()) != null;
}
public Object invokeOptional(T t, Object... objArr) throws InvocationTargetException {
Method method = getMethod(t.getClass());
if (method == null) {
return null;
}
try {
return method.invoke(t, objArr);
} catch (IllegalAccessException unused) {
return null;
}
}
public Object invokeOptionalWithoutCheckedException(T t, Object... objArr) {
try {
return invokeOptional(t, objArr);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (targetException instanceof RuntimeException) {
throw ((RuntimeException) targetException);
}
AssertionError assertionError = new AssertionError("Unexpected exception");
assertionError.initCause(targetException);
throw assertionError;
}
}
public Object invoke(T t, Object... objArr) throws InvocationTargetException {
Method method = getMethod(t.getClass());
if (method == null) {
StringBuilder sb = new StringBuilder("Method ");
sb.append(this.methodName);
sb.append(" not supported for object ");
sb.append(t);
throw new AssertionError(sb.toString());
}
try {
return method.invoke(t, objArr);
} catch (IllegalAccessException e) {
AssertionError assertionError = new AssertionError("Unexpectedly could not call: ".concat(String.valueOf(method)));
assertionError.initCause(e);
throw assertionError;
}
}
public Object invokeWithoutCheckedException(T t, Object... objArr) {
try {
return invoke(t, objArr);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (targetException instanceof RuntimeException) {
throw ((RuntimeException) targetException);
}
AssertionError assertionError = new AssertionError("Unexpected exception");
assertionError.initCause(targetException);
throw assertionError;
}
}
private Method getMethod(Class<?> cls) {
Class<?> cls2;
String str = this.methodName;
if (str == null) {
return null;
}
Method publicMethod = getPublicMethod(cls, str, this.methodParams);
if (publicMethod == null || (cls2 = this.returnType) == null || cls2.isAssignableFrom(publicMethod.getReturnType())) {
return publicMethod;
}
return null;
}
private static Method getPublicMethod(Class<?> cls, String str, Class[] clsArr) {
if (cls == null) {
return null;
}
try {
if ((cls.getModifiers() & 1) == 0) {
return getPublicMethod(cls.getSuperclass(), str, clsArr);
}
Method method = cls.getMethod(str, clsArr);
try {
if ((method.getModifiers() & 1) == 0) {
return null;
}
} catch (NoSuchMethodException unused) {
}
return method;
} catch (NoSuchMethodException unused2) {
return null;
}
}
}