Here you can find the source of getConstructorWithLeastParametersFromList( ArrayList
public static <T> Constructor getConstructorWithLeastParametersFromList( ArrayList<Constructor> constructorsFromType)
//package com.java2s; /**//from w w w . j a v a 2 s . co m * Copyright 2013 Hunter Hegler * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.lang.reflect.Constructor; import java.util.ArrayList; public class Main { public static <T> Constructor getConstructorWithLeastParametersFromList( ArrayList<Constructor> constructorsFromType) { Constructor planBConstructor = Object.class .getDeclaredConstructors()[0]; Constructor retVal; if (!constructorsFromType.isEmpty()) { //initially assume that first constructor is best int index = 0; retVal = constructorsFromType.get(index); index++; //look for constructor that does not throw exceptions while (retVal.getExceptionTypes().length > 0 && index < constructorsFromType.size()) { retVal = constructorsFromType.get(index); index++; } if (!(retVal.getExceptionTypes().length > 0)) { //Try to find constructor with less number of parameters for (int i = 1; i < constructorsFromType.size(); i++) { Constructor next = constructorsFromType.get(i); if (next.getParameterTypes().length < retVal .getParameterTypes().length) { retVal = next; } } } } else { retVal = planBConstructor; } // if (retVal.getParameterTypes().length == 1 && retVal.getParameterTypes()[0] == type) { // retVal = ArrayList.class.getDeclaredConstructors()[0]; // // } return retVal; } }