private < T > Creator < T > find ( final ConstructorKey key ) { for ( ConstructorKey candidate : lookup . getCandidates ( key ) ) { if ( key . isAssignable ( candidate ) ) { return get ( candidate ) ; } } // if the key has uniform parameter types if ( key . isUniform ( ) ) { // convert the key parameter types (length doesn't matter) Object types = Array . newInstance ( key . type ( 0 ) , 0 ) ; final ConstructorKey alt = new ObjectBasedKey ( key . intf ( ) , new Object [ ] { types } ) ; // find the creator for the new alternate key Creator < T > creator = get ( alt ) ; // if found -> get/find will return the correct constructor, we then need // to wrap the constructor with an creator that will // wrap the objects in an array // if not found -> the method will return a non-null creator that // will throw an exception (see. below) - wrapping // has no harm as the params never get used // return new ArrayWrapCreator < T > ( creator ) ; } LOGGER . debug ( "no instance handler found for " , key ) ; // return an instance handler that will throw an exception when invoked return new Creator < T > ( ) { @ Override public T create ( Object [ ] objects ) { throw new IllegalArgumentException ( getSuggestionMessage ( key ) ) ; } @ Override public Class < T > getDeclaringClass ( ) { throw new IllegalArgumentException ( "missing declaring class" ) ; } } ; }
Find a constructor whose parameters are assignable from the provided key .