package com.amplifyframework.api.aws;

import com.amplifyframework.api.ApiException;
import com.amplifyframework.core.model.ModelProvider;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/* loaded from: classes4.dex */
final class ModelProviderLocator {
    private static final String DEFAULT_MODEL_PROVIDER_CLASS_NAME = "com.amplifyframework.datastore.generated.model.AmplifyModelProvider";
    private static final String GET_INSTANCE_ACCESSOR_METHOD_NAME = "getInstance";

    private ModelProviderLocator() {
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static ModelProvider locate() throws ApiException {
        return locate(DEFAULT_MODEL_PROVIDER_CLASS_NAME);
    }

    static ModelProvider locate(String str) throws ApiException {
        Objects.requireNonNull(str);
        try {
            Class<?> cls = Class.forName(str);
            if (ModelProvider.class.isAssignableFrom(cls)) {
                try {
                    try {
                        return (ModelProvider) cls.getDeclaredMethod(GET_INSTANCE_ACCESSOR_METHOD_NAME, new Class[0]).invoke(null, new Object[0]);
                    } catch (IllegalAccessException e10) {
                        throw new ApiException("Tried to call " + cls.getName() + GET_INSTANCE_ACCESSOR_METHOD_NAME + ", but this method did not have public access.", e10, "Validate that " + cls.getName() + " has not been modified since the time it was code-generated.");
                    } catch (InvocationTargetException e11) {
                        throw new ApiException("An exception was thrown from " + cls.getName() + GET_INSTANCE_ACCESSOR_METHOD_NAME + " while invoking via reflection.", e11, "This is not expected to occur. Contact AWS.");
                    }
                } catch (NoSuchMethodException e12) {
                    throw new ApiException("Found a code-generated model provider = " + cls.getName() + ", however it had no static method named getInstance()!", e12, "Validate that " + cls.getName() + " has not been modified since the time it was code-generated.");
                }
            }
            throw new ApiException("Located class as " + cls.getName() + ", but it does not implement " + ModelProvider.class.getName() + ".", "Validate that " + cls.getName() + " has not been modified since the time it was code-generated.");
        } catch (ClassNotFoundException e13) {
            throw new ApiException("Failed to find code-generated model provider.", e13, "Validate that " + str + " is built into your project.");
        }
    }
}
