package com.github.snakeice.aloklib.constructortools;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Rodrigo on 10/04/2016.
 */
public class ConstructorsHelper<T> {
    Constructor[] constructors;

    public ConstructorsHelper(Constructor[] constructors) {
        this.constructors = constructors;
    }

    public Constructor findByParam(Object... objects){
        List<Class> types;
        types = new ArrayList<>();
        for (Object o:objects) {
            types.add(o.getClass());
        }
        for (Constructor c:constructors) {
            if (Arrays.equals(c.getParameterTypes(), types.toArray())){
                return c;
            }
        }
        return constructors[0];
    }

    public T newInstance(Object... objects){
        try {
           return (T) findByParam(objects).newInstance(objects);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        try {
            return (T) constructors[0].newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -516,32 +516,33 @@
 ject... objects)
+ 
 %7B%0A        List%3CC
@@ -610,17 +610,19 @@
 Object o
-:
+ : 
 objects)
@@ -697,17 +697,19 @@
 ructor c
-:
+ : 
 construc
@@ -786,16 +786,17 @@
 rray()))
+ 
 %7B%0A      
@@ -858,31 +858,49 @@
 urn 
-constructors%5B0%5D;%0A    %7D%0A
+null;%0A    %7D%0A%0A    @SuppressWarnings(%22All%22)
 %0A   
@@ -939,16 +939,17 @@
 objects)
+ 
 %7B%0A      
@@ -971,297 +971,141 @@
     
-return (T) findByParam(objects).newInstance(objects);%0A        %7D catch (InstantiationException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace();%0A        %7D catch (InvocationTargetException e) %7B%0A            e.printStackTrace()
+ Constructor constructor = findByParam(objects);%0A            if (constructor == null) %7B%0A                constructor = constructors%5B0%5D
 ;%0A  
@@ -1114,23 +1114,13 @@
     
-%7D%0A
     
-    try %7B
+%7D
 %0A   
@@ -1150,20 +1150,16 @@
 structor
-s%5B0%5D
 .newInst
@@ -1167,164 +1167,15 @@
 nce(
-);%0A        %7D catch (InstantiationException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IllegalAccessException e) %7B%0A            e.printStackTrace(
+objects
 );%0A 
@@ -1190,32 +1190,16 @@
  catch (
-InvocationTarget
 Exceptio

Update ConstructorsHelper.java