public static Class < ? > [ ] getParameterTypes ( Object ... arguments ) { Class < ? > [ ] types = new Class < ? > [ arguments . length ] ; for ( int i = 0 ; i < arguments . length ; i ++ ) { Object argument = arguments [ i ] ; if ( argument == null ) { types [ i ] = Object . class ; continue ; } if ( ! ( argument instanceof VarArgs ) ) { types [ i ] = argument . getClass ( ) ; if ( types [ i ] . isAnonymousClass ( ) ) { Class < ? > [ ] interfaces = types [ i ] . getInterfaces ( ) ; Class < ? > superclass = interfaces . length > 0 ? interfaces [ 0 ] : null ; if ( superclass == null ) { superclass = types [ i ] . getSuperclass ( ) ; } types [ i ] = superclass ; } continue ; } // here we have VarArgs that must be the last in arguments list if ( i != arguments . length - 1 ) { throw new BugError ( "Variable arguments must be the last in arguments list." ) ; } @ SuppressWarnings ( "unchecked" ) VarArgs < Object > varArgs = ( VarArgs < Object > ) argument ; int index = arguments . length - 1 ; types [ index ] = varArgs . getType ( ) ; arguments [ index ] = varArgs . getArguments ( ) ; } return types ; }
Get method formal parameter types inferred from actual invocation arguments . This utility is a helper for method discovery when have access to the actual invocation argument but not the formal parameter types list declared by method signature .