<commit_before>package com.carrotcreative.recyclercore.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.carrotcreative.recyclercore.adapter.RecyclerCoreController;
import com.carrotcreative.recyclercore.adapter.RecyclerCoreModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class InstantiationUtil
{
    private InstantiationUtil() {}

    /**
     *
     * @param modelClass The model class for which we need to instantiate an object.
     * @return
     */
    public static RecyclerCoreModel instantiateModel(Class<?> modelClass)
    {
        String className = modelClass.getCanonicalName();
        Constructor<?>[] consArray = modelClass.getConstructors();
        Constructor noArgConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 0)
            {
                noArgConstructor = constructor;
                break;
            }
        }

        RecyclerCoreModel modelInstance = null;
        if (noArgConstructor != null)
        {
            try
            {
                modelInstance = (RecyclerCoreModel) noArgConstructor.newInstance();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(modelInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a public no-argument constructor!");
        }

        return modelInstance;
    }

    public static RecyclerCoreController instantiateController(Class<?> controllerClass, int resource, ViewGroup parent)
    {
        String className = controllerClass.getCanonicalName();
        Constructor<?>[] consArray = controllerClass.getConstructors();
        Constructor defaultConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 1)
            {
                defaultConstructor = constructor;
                break;
            }
        }

        RecyclerCoreController controllerInstance = null;
        if (defaultConstructor != null)
        {
            try
            {
                Context context = parent.getContext();
                View itemView = LayoutInflater.from(context).inflate(resource, parent, false);
                controllerInstance = (RecyclerCoreController) defaultConstructor.newInstance(itemView);
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(controllerInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a valid public constructor! " +
                    "The constructor should take only View as an argument.");
        }

        return controllerInstance;
    }
}<commit_msg>pass the view when instantiating the controller<commit_after>package com.carrotcreative.recyclercore.util;

import android.view.View;

import com.carrotcreative.recyclercore.adapter.RecyclerCoreController;
import com.carrotcreative.recyclercore.adapter.RecyclerCoreModel;
import com.carrotcreative.recyclercore.inject.InjectController;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class InstantiationUtil
{
    private InstantiationUtil() {}

    /**
     *
     * @param modelClass The model class for which we need to instantiate an object.
     * @return
     */
    public static RecyclerCoreModel instantiateModel(Class<?> modelClass)
    {
        String className = modelClass.getCanonicalName();
        Constructor<?>[] consArray = modelClass.getConstructors();
        Constructor noArgConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 0)
            {
                noArgConstructor = constructor;
                break;
            }
        }

        RecyclerCoreModel modelInstance = null;
        if (noArgConstructor != null)
        {
            try
            {
                modelInstance = (RecyclerCoreModel) noArgConstructor.newInstance();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(modelInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a public no-argument constructor!");
        }

        return modelInstance;
    }

    public static RecyclerCoreController instantiateController(Class<?> controllerClass, View itemView)
    {
        String className = controllerClass.getCanonicalName();
        Constructor<?>[] consArray = controllerClass.getConstructors();
        Constructor defaultConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 1)
            {
                defaultConstructor = constructor;
                break;
            }
        }

        RecyclerCoreController controllerInstance = null;
        if (defaultConstructor != null)
        {
            try
            {

                controllerInstance = (RecyclerCoreController) defaultConstructor.newInstance(itemView);
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(controllerInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a valid public constructor! " +
                    "The constructor should take only View as an argument.");
        }

        return controllerInstance;
    }

    public static InjectController getInjectedController(RecyclerCoreModel model)
    {
        InjectController injectedController = model.getClass().getAnnotation(InjectController.class);
        if(injectedController == null)
        {
            throw new IllegalStateException(" class " + model.getClass().getCanonicalName() + " " +
                    " does not implement InjectController annotation");
        }

        return injectedController;
    }
}
{'fraction_non_alphanumeric': 0.07816091954022988, 'fraction_numerical': 0.0005409060175794456, 'mean_word_length': 1.8611218568665377, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5014148', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1659, 'n_words': 386}