@ SuppressWarnings ( "unchecked" ) public static < T > List < T > getInstanceList ( Class < T > type ) { if ( type == null ) { throw new IllegalArgumentException ( "Type should not be empty" ) ; } List < T > loader = ( List < T > ) EXTENSION_LOCATOR . get ( type ) ; if ( loader == null ) { try { lock . lock ( ) ; List < T > instanceList = new ArrayList < T > ( ) ; ServiceLoader < T > serviceLoader = ServiceLoader . load ( type ) ; for ( T service : serviceLoader ) { instanceList . add ( service ) ; } loader = instanceList ; if ( EXTENSION_LOCATOR . putIfAbsent ( type , instanceList ) != instanceList ) { loader = ( List < T > ) EXTENSION_LOCATOR . get ( type ) ; } } catch ( Exception e ) { LOG . error ( "Error to load SPI instances from META-INF/services/" + type , e ) ; } finally { lock . unlock ( ) ; } } return loader ; }
Get instances dynamically for a class type