@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2018 the original author or authors .
* Copyright 2002 - 2019 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -83,25 +83,25 @@ public final class SpringFactoriesLoader {
@@ -83,25 +83,25 @@ public final class SpringFactoriesLoader {
* < p > The returned factories are sorted through { @link AnnotationAwareOrderComparator } .
* < p > If a custom instantiation strategy is required , use { @link # loadFactoryNames }
* to obtain all registered factory names .
* @param factoryClass the interface or abstract class representing the factory
* @param factoryType the interface or abstract class representing the factory
* @param classLoader the ClassLoader to use for loading ( can be { @code null } to use the default )
* @throws IllegalArgumentException if any factory implementation class cannot
* be loaded or if an error occurs while instantiating any factory
* @see # loadFactoryNames
* /
public static < T > List < T > loadFactories ( Class < T > factoryClass , @Nullable ClassLoader classLoader ) {
Assert . notNull ( factoryClass , "'factoryClass ' must not be null" ) ;
public static < T > List < T > loadFactories ( Class < T > factoryType , @Nullable ClassLoader classLoader ) {
Assert . notNull ( factoryType , "'factoryType ' must not be null" ) ;
ClassLoader classLoaderToUse = classLoader ;
if ( classLoaderToUse = = null ) {
classLoaderToUse = SpringFactoriesLoader . class . getClassLoader ( ) ;
}
List < String > factoryNames = loadFactoryNames ( factoryClass , classLoaderToUse ) ;
List < String > factoryImplementation Names = loadFactoryNames ( factoryType , classLoaderToUse ) ;
if ( logger . isTraceEnabled ( ) ) {
logger . trace ( "Loaded [" + factoryClass . getName ( ) + "] names: " + factoryNames ) ;
logger . trace ( "Loaded [" + factoryType . getName ( ) + "] names: " + factoryImplementation Names ) ;
}
List < T > result = new ArrayList < > ( factoryNames . size ( ) ) ;
for ( String factoryName : factoryNames ) {
result . add ( instantiateFactory ( factoryName , factoryClass , classLoaderToUse ) ) ;
List < T > result = new ArrayList < > ( factoryImplementation Names . size ( ) ) ;
for ( String factoryImplementation Name : factoryImplementation Names ) {
result . add ( instantiateFactory ( factoryImplementation Name , factoryType , classLoaderToUse ) ) ;
}
AnnotationAwareOrderComparator . sort ( result ) ;
return result ;
@ -111,15 +111,15 @@ public final class SpringFactoriesLoader {
@@ -111,15 +111,15 @@ public final class SpringFactoriesLoader {
* Load the fully qualified class names of factory implementations of the
* given type from { @value # FACTORIES_RESOURCE_LOCATION } , using the given
* class loader .
* @param factoryClass the interface or abstract class representing the factory
* @param factoryType the interface or abstract class representing the factory
* @param classLoader the ClassLoader to use for loading resources ; can be
* { @code null } to use the default
* @throws IllegalArgumentException if an error occurs while loading factory names
* @see # loadFactories
* /
public static List < String > loadFactoryNames ( Class < ? > factoryClass , @Nullable ClassLoader classLoader ) {
String factoryClassName = factoryClass . getName ( ) ;
return loadSpringFactories ( classLoader ) . getOrDefault ( factoryClass Name , Collections . emptyList ( ) ) ;
public static List < String > loadFactoryNames ( Class < ? > factoryType , @Nullable ClassLoader classLoader ) {
String factoryTypeName = factoryType . getName ( ) ;
return loadSpringFactories ( classLoader ) . getOrDefault ( factoryType Name , Collections . emptyList ( ) ) ;
}
private static Map < String , List < String > > loadSpringFactories ( @Nullable ClassLoader classLoader ) {
@ -138,9 +138,9 @@ public final class SpringFactoriesLoader {
@@ -138,9 +138,9 @@ public final class SpringFactoriesLoader {
UrlResource resource = new UrlResource ( url ) ;
Properties properties = PropertiesLoaderUtils . loadProperties ( resource ) ;
for ( Map . Entry < ? , ? > entry : properties . entrySet ( ) ) {
String factoryClass Name = ( ( String ) entry . getKey ( ) ) . trim ( ) ;
for ( String factoryName : StringUtils . commaDelimitedListToStringArray ( ( String ) entry . getValue ( ) ) ) {
result . add ( factoryClass Name , factoryName . trim ( ) ) ;
String factoryType Name = ( ( String ) entry . getKey ( ) ) . trim ( ) ;
for ( String factoryImplementation Name : StringUtils . commaDelimitedListToStringArray ( ( String ) entry . getValue ( ) ) ) {
result . add ( factoryType Name , factoryImplementation Name . trim ( ) ) ;
}
}
}
@ -154,17 +154,19 @@ public final class SpringFactoriesLoader {
@@ -154,17 +154,19 @@ public final class SpringFactoriesLoader {
}
@SuppressWarnings ( "unchecked" )
private static < T > T instantiateFactory ( String instanceClass Name, Class < T > factoryClass , ClassLoader classLoader ) {
private static < T > T instantiateFactory ( String factoryImplementation Name, Class < T > factoryType , ClassLoader classLoader ) {
try {
Class < ? > instance Class = ClassUtils . forName ( instanceClass Name, classLoader ) ;
if ( ! factoryClass . isAssignableFrom ( instance Class) ) {
Class < ? > factoryImplementation Class = ClassUtils . forName ( factoryImplementation Name, classLoader ) ;
if ( ! factoryType . isAssignableFrom ( factoryImplementation Class) ) {
throw new IllegalArgumentException (
"Class [" + instanceClass Name + "] is not assignable to [" + factoryClass . getName ( ) + "]" ) ;
"Class [" + factoryImplementation Name + "] is not assignable to factory type [" + factoryType . getName ( ) + "]" ) ;
}
return ( T ) ReflectionUtils . accessibleConstructor ( instance Class) . newInstance ( ) ;
return ( T ) ReflectionUtils . accessibleConstructor ( factoryImplementation Class) . newInstance ( ) ;
}
catch ( Throwable ex ) {
throw new IllegalArgumentException ( "Unable to instantiate factory class: " + factoryClass . getName ( ) , ex ) ;
throw new IllegalArgumentException (
"Unable to instantiate factory class [" + factoryImplementationName + "] for factory type [" + factoryType . getName ( ) + "]" ,
ex ) ;
}
}