package com.github.mustachejava.reflect;

import com.github.mustachejava.MustacheException;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.util.GuardException;
import com.github.mustachejava.util.Wrapper;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class ReflectionWrapper extends GuardedWrapper {
    protected final Object[] arguments;
    protected final Field field;
    protected final Method method;
    protected final ObjectHandler oh;
    protected final int scopeIndex;
    protected final Wrapper[] wrappers;

    public ReflectionWrapper(int i, Wrapper[] wrapperArr, Guard[] guardArr, AccessibleObject accessibleObject, Object[] objArr, ObjectHandler objectHandler) {
        super(guardArr);
        this.wrappers = wrapperArr;
        this.oh = objectHandler;
        if (accessibleObject instanceof Field) {
            this.method = null;
            this.field = (Field) accessibleObject;
        } else {
            this.method = (Method) accessibleObject;
            this.field = null;
        }
        this.arguments = objArr;
        this.scopeIndex = i;
    }

    protected Object unwrap(List<Object> list) {
        Wrapper[] wrapperArr = this.wrappers;
        if (wrapperArr == null || wrapperArr.length == 0) {
            return list.get(this.scopeIndex);
        }
        return ReflectionObjectHandler.unwrap(this.oh, this.scopeIndex, wrapperArr, list);
    }

    @Override // com.github.mustachejava.reflect.GuardedWrapper, com.github.mustachejava.util.Wrapper
    public Object call(List<Object> list) throws GuardException {
        guardCall(list);
        Object objCoerce = this.oh.coerce(unwrap(list));
        if (objCoerce == null) {
            return null;
        }
        try {
            Method method = this.method;
            if (method == null) {
                return this.field.get(objCoerce);
            }
            return method.invoke(objCoerce, this.arguments);
        } catch (IllegalAccessException | IllegalArgumentException e) {
            throw new MustacheException("Error accessing " + getTargetDescription() + " on " + elementToString(objCoerce) + ", scope: [" + elementsToString(list, this.scopeIndex) + "], guards: " + Arrays.toString(this.guards), e);
        } catch (InvocationTargetException e2) {
            throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(objCoerce), e2.getTargetException());
        } catch (Exception e3) {
            throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(objCoerce), e3);
        }
    }

    public Method getMethod() {
        return this.method;
    }

    public Field getField() {
        return this.field;
    }

    public Object[] getArguments() {
        return this.arguments;
    }

    @Override // com.github.mustachejava.reflect.GuardedWrapper
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Field field = this.field;
        if (field == null) {
            sb.append(this.method.toString());
            Object[] objArr = this.arguments;
            if (objArr != null) {
                for (Object obj : objArr) {
                    sb.append(",").append(obj);
                }
            }
        } else {
            sb.append(field);
        }
        return sb.toString();
    }

    public Wrapper[] getWrappers() {
        return this.wrappers;
    }

    private String getTargetDescription() {
        Object[] objArr = this.arguments;
        List<Object> listEmptyList = objArr == null ? Collections.emptyList() : Arrays.asList(objArr);
        if (this.method == null) {
            return "field " + this.field.getDeclaringClass() + "." + this.field.getName();
        }
        return "method " + this.method.getDeclaringClass().getCanonicalName() + "." + this.method.getName() + "(" + elementsToString(listEmptyList, this.method.getParameterTypes().length - 1) + ")";
    }

    private String elementsToString(List<Object> list, int i) {
        if (list == null || list.size() == 0 || i < 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 <= i && i2 < list.size(); i2++) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(elementToString(list.get(i2)));
        }
        return sb.toString();
    }

    private String elementToString(Object obj) {
        if (obj == null) {
            return null;
        }
        return obj.getClass().getCanonicalName() + '@' + obj.hashCode();
    }
}
