254 lines
10 KiB
Java
254 lines
10 KiB
Java
|
package com.google.common.reflect;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.collect.ImmutableList;
|
||
|
import java.lang.annotation.Annotation;
|
||
|
import java.lang.reflect.AccessibleObject;
|
||
|
import java.lang.reflect.Constructor;
|
||
|
import java.lang.reflect.GenericDeclaration;
|
||
|
import java.lang.reflect.InvocationTargetException;
|
||
|
import java.lang.reflect.Member;
|
||
|
import java.lang.reflect.Method;
|
||
|
import java.lang.reflect.Modifier;
|
||
|
import java.lang.reflect.Type;
|
||
|
import java.lang.reflect.TypeVariable;
|
||
|
import java.util.Arrays;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public abstract class Invokable<T, R> extends Element implements GenericDeclaration {
|
||
|
abstract Type[] getGenericExceptionTypes();
|
||
|
|
||
|
abstract Type[] getGenericParameterTypes();
|
||
|
|
||
|
abstract Type getGenericReturnType();
|
||
|
|
||
|
abstract Annotation[][] getParameterAnnotations();
|
||
|
|
||
|
abstract Object invokeInternal(Object obj, Object[] objArr) throws InvocationTargetException, IllegalAccessException;
|
||
|
|
||
|
public abstract boolean isOverridable();
|
||
|
|
||
|
public abstract boolean isVarArgs();
|
||
|
|
||
|
@Override // com.google.common.reflect.Element
|
||
|
public /* bridge */ /* synthetic */ boolean equals(Object obj) {
|
||
|
return super.equals(obj);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Element
|
||
|
public /* bridge */ /* synthetic */ int hashCode() {
|
||
|
return super.hashCode();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Element
|
||
|
public /* bridge */ /* synthetic */ String toString() {
|
||
|
return super.toString();
|
||
|
}
|
||
|
|
||
|
<M extends AccessibleObject & Member> Invokable(M m) {
|
||
|
super(m);
|
||
|
}
|
||
|
|
||
|
public static Invokable<?, Object> from(Method method) {
|
||
|
return new MethodInvokable(method);
|
||
|
}
|
||
|
|
||
|
public static <T> Invokable<T, T> from(Constructor<T> constructor) {
|
||
|
return new ConstructorInvokable(constructor);
|
||
|
}
|
||
|
|
||
|
public final R invoke(T t, Object... objArr) throws InvocationTargetException, IllegalAccessException {
|
||
|
return (R) invokeInternal(t, (Object[]) Preconditions.checkNotNull(objArr));
|
||
|
}
|
||
|
|
||
|
public final TypeToken<? extends R> getReturnType() {
|
||
|
return (TypeToken<? extends R>) TypeToken.of(getGenericReturnType());
|
||
|
}
|
||
|
|
||
|
public final ImmutableList<Parameter> getParameters() {
|
||
|
Type[] genericParameterTypes = getGenericParameterTypes();
|
||
|
Annotation[][] parameterAnnotations = getParameterAnnotations();
|
||
|
ImmutableList.Builder builder = ImmutableList.builder();
|
||
|
for (int i = 0; i < genericParameterTypes.length; i++) {
|
||
|
builder.add((ImmutableList.Builder) new Parameter(this, i, TypeToken.of(genericParameterTypes[i]), parameterAnnotations[i]));
|
||
|
}
|
||
|
return builder.build();
|
||
|
}
|
||
|
|
||
|
public final ImmutableList<TypeToken<? extends Throwable>> getExceptionTypes() {
|
||
|
ImmutableList.Builder builder = ImmutableList.builder();
|
||
|
for (Type type : getGenericExceptionTypes()) {
|
||
|
builder.add((ImmutableList.Builder) TypeToken.of(type));
|
||
|
}
|
||
|
return builder.build();
|
||
|
}
|
||
|
|
||
|
public final <R1 extends R> Invokable<T, R1> returning(Class<R1> cls) {
|
||
|
return returning(TypeToken.of((Class) cls));
|
||
|
}
|
||
|
|
||
|
/* JADX WARN: Multi-variable type inference failed */
|
||
|
public final <R1 extends R> Invokable<T, R1> returning(TypeToken<R1> typeToken) {
|
||
|
if (typeToken.isSupertypeOf(getReturnType())) {
|
||
|
return this;
|
||
|
}
|
||
|
String valueOf = String.valueOf(getReturnType());
|
||
|
String valueOf2 = String.valueOf(typeToken);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 35 + String.valueOf(valueOf2).length());
|
||
|
sb.append("Invokable is known to return ");
|
||
|
sb.append(valueOf);
|
||
|
sb.append(", not ");
|
||
|
sb.append(valueOf2);
|
||
|
throw new IllegalArgumentException(sb.toString());
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Element, java.lang.reflect.Member
|
||
|
public final Class<? super T> getDeclaringClass() {
|
||
|
return (Class<? super T>) super.getDeclaringClass();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Element
|
||
|
public TypeToken<T> getOwnerType() {
|
||
|
return TypeToken.of((Class) getDeclaringClass());
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
static class MethodInvokable<T> extends Invokable<T, Object> {
|
||
|
final Method method;
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public MethodInvokable(Method method) {
|
||
|
super(method);
|
||
|
this.method = method;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
final Object invokeInternal(Object obj, Object[] objArr) throws InvocationTargetException, IllegalAccessException {
|
||
|
return this.method.invoke(obj, objArr);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type getGenericReturnType() {
|
||
|
return this.method.getGenericReturnType();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type[] getGenericParameterTypes() {
|
||
|
return this.method.getGenericParameterTypes();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type[] getGenericExceptionTypes() {
|
||
|
return this.method.getGenericExceptionTypes();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
final Annotation[][] getParameterAnnotations() {
|
||
|
return this.method.getParameterAnnotations();
|
||
|
}
|
||
|
|
||
|
@Override // java.lang.reflect.GenericDeclaration
|
||
|
public final TypeVariable<?>[] getTypeParameters() {
|
||
|
return this.method.getTypeParameters();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public final boolean isOverridable() {
|
||
|
return (isFinal() || isPrivate() || isStatic() || Modifier.isFinal(getDeclaringClass().getModifiers())) ? false : true;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public final boolean isVarArgs() {
|
||
|
return this.method.isVarArgs();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
static class ConstructorInvokable<T> extends Invokable<T, T> {
|
||
|
final Constructor<?> constructor;
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public final boolean isOverridable() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public ConstructorInvokable(Constructor<?> constructor) {
|
||
|
super(constructor);
|
||
|
this.constructor = constructor;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
final Object invokeInternal(Object obj, Object[] objArr) throws InvocationTargetException, IllegalAccessException {
|
||
|
try {
|
||
|
return this.constructor.newInstance(objArr);
|
||
|
} catch (InstantiationException e) {
|
||
|
String valueOf = String.valueOf(this.constructor);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 8);
|
||
|
sb.append(valueOf);
|
||
|
sb.append(" failed.");
|
||
|
throw new RuntimeException(sb.toString(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type getGenericReturnType() {
|
||
|
Class<? super T> declaringClass = getDeclaringClass();
|
||
|
TypeVariable<Class<? super T>>[] typeParameters = declaringClass.getTypeParameters();
|
||
|
return typeParameters.length > 0 ? Types.newParameterizedType(declaringClass, typeParameters) : declaringClass;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type[] getGenericParameterTypes() {
|
||
|
Type[] genericParameterTypes = this.constructor.getGenericParameterTypes();
|
||
|
if (genericParameterTypes.length <= 0 || !mayNeedHiddenThis()) {
|
||
|
return genericParameterTypes;
|
||
|
}
|
||
|
Class<?>[] parameterTypes = this.constructor.getParameterTypes();
|
||
|
return (genericParameterTypes.length == parameterTypes.length && parameterTypes[0] == getDeclaringClass().getEnclosingClass()) ? (Type[]) Arrays.copyOfRange(genericParameterTypes, 1, genericParameterTypes.length) : genericParameterTypes;
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public Type[] getGenericExceptionTypes() {
|
||
|
return this.constructor.getGenericExceptionTypes();
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
final Annotation[][] getParameterAnnotations() {
|
||
|
return this.constructor.getParameterAnnotations();
|
||
|
}
|
||
|
|
||
|
@Override // java.lang.reflect.GenericDeclaration
|
||
|
public final TypeVariable<?>[] getTypeParameters() {
|
||
|
TypeVariable<Class<? super T>>[] typeParameters = getDeclaringClass().getTypeParameters();
|
||
|
TypeVariable<Constructor<?>>[] typeParameters2 = this.constructor.getTypeParameters();
|
||
|
TypeVariable<?>[] typeVariableArr = new TypeVariable[typeParameters.length + typeParameters2.length];
|
||
|
System.arraycopy(typeParameters, 0, typeVariableArr, 0, typeParameters.length);
|
||
|
System.arraycopy(typeParameters2, 0, typeVariableArr, typeParameters.length, typeParameters2.length);
|
||
|
return typeVariableArr;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.reflect.Invokable
|
||
|
public final boolean isVarArgs() {
|
||
|
return this.constructor.isVarArgs();
|
||
|
}
|
||
|
|
||
|
private boolean mayNeedHiddenThis() {
|
||
|
Class<?> declaringClass = this.constructor.getDeclaringClass();
|
||
|
if (declaringClass.getEnclosingConstructor() != null) {
|
||
|
return true;
|
||
|
}
|
||
|
if (declaringClass.getEnclosingMethod() != null) {
|
||
|
return !Modifier.isStatic(r1.getModifiers());
|
||
|
}
|
||
|
return (declaringClass.getEnclosingClass() == null || Modifier.isStatic(declaringClass.getModifiers())) ? false : true;
|
||
|
}
|
||
|
}
|
||
|
}
|