Skip to content

Commit 0d4fb60

Browse files
committed
Introduce instantiateClass as replacement for RepositoryFactorySupport.getTargetRepositoryViaReflection.
getTargetRepositoryViaReflection accepting Class has a misleading name as it lets assume that the returned instance is a repository. In fact, this method looks up a matching constructor for the given arguments and creates an instance. Closes #2361
1 parent c94fe6d commit 0d4fb60

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ protected final <R> R getTargetRepositoryViaReflection(RepositoryInformation inf
525525
Object... constructorArguments) {
526526

527527
Class<?> baseClass = information.getRepositoryBaseClass();
528-
return getTargetRepositoryViaReflection(baseClass, constructorArguments);
528+
return instantiateClass(baseClass, constructorArguments);
529529
}
530530

531531
/**
@@ -535,15 +535,36 @@ protected final <R> R getTargetRepositoryViaReflection(RepositoryInformation inf
535535
* @param baseClass
536536
* @param constructorArguments
537537
* @return
538+
* @deprecated since 2.6 because it has a misleading name. Use {@link #instantiateClass(Class, Object...)} instead.
538539
*/
539540
@SuppressWarnings("unchecked")
541+
@Deprecated
540542
protected final <R> R getTargetRepositoryViaReflection(Class<?> baseClass, Object... constructorArguments) {
543+
return instantiateClass(baseClass, constructorArguments);
544+
}
545+
546+
/**
547+
* Convenience method to instantiate a class using the given {@code constructorArguments} by looking up a matching
548+
* constructor.
549+
* <p>
550+
* Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public)
551+
* constructor, and supports Kotlin classes with optional parameters and default values.
552+
*
553+
* @param baseClass
554+
* @param constructorArguments
555+
* @return
556+
* @since 2.6
557+
*/
558+
@SuppressWarnings("unchecked")
559+
protected final <R> R instantiateClass(Class<?> baseClass, Object... constructorArguments) {
560+
541561
Optional<Constructor<?>> constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments);
542562

543563
return constructor.map(it -> (R) BeanUtils.instantiateClass(it, constructorArguments))
544564
.orElseThrow(() -> new IllegalStateException(String.format(
545565
"No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these",
546-
baseClass, Arrays.stream(constructorArguments).map(Object::getClass).collect(Collectors.toList()))));
566+
baseClass, Arrays.stream(constructorArguments).map(Object::getClass).map(ClassUtils::getQualifiedName)
567+
.collect(Collectors.joining(", ")))));
547568
}
548569

549570
private ApplicationStartup getStartup() {

0 commit comments

Comments
 (0)