public static Constructor getCompatibleConstructor ( final Class type , final Class valueType ) { // first try and find a constructor with the exact argument type try { return type . getConstructor ( new Class [ ] { valueType } ) ; } catch ( Exception ignore ) { // if the above failed, then try and find a constructor with // an compatible argument type // get an array of compatible types Class [ ] types = type . getClasses ( ) ; for ( int i = 0 ; i < types . length ; i ++ ) { try { return type . getConstructor ( new Class [ ] { types [ i ] } ) ; } catch ( Exception ignore2 ) { } } } // if we get this far, then we can't find a compatible constructor return null ; }
Get a compatible constructor for the given value type