private < T > T pluginProxy ( final Class < T > type ) { Object proxy = Proxy . newProxyInstance ( classLoader , new Class < ? > [ ] { type } , new InvocationHandler ( ) { @ Override public Object invoke ( Object target , Method method , Object [ ] args ) throws Throwable { for ( Object plugin : getPlugins ( ) ) { if ( type . isInstance ( plugin ) ) { try { Utils . invoke ( plugin , method , 0 , args ) ; } catch ( Throwable t ) { if ( ! method . getName ( ) . equals ( "startOfScenarioLifeCycle" ) && ! method . getName ( ) . equals ( "endOfScenarioLifeCycle" ) ) { // IntelliJ has its own formatter which doesn't yet implement these methods. throw t ; } } } } return null ; } } ) ; return type . cast ( proxy ) ; }
Creates a dynamic proxy that multiplexes method invocations to all plugins of the same type .