Skip to content

Commit 915dea0

Browse files
committed
Use getDeclaredConstructor(…) in RepositoryBeanDefinitionReader to avoid failures due to package-private constructors.
Closes #3325
1 parent 1fae9b7 commit 915dea0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ private RepositoryFragmentsContributor getFragmentsContributor(Class<?> reposito
184184
Assert.state(beanDefinition.getBeanClassName() != null, "No Repository BeanFactory set");
185185

186186
Class<?> repositoryFactoryBean = forName(beanDefinition.getBeanClassName());
187-
Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(repositoryFactoryBean, Class.class);
187+
Constructor<?> constructor = org.springframework.data.util.ClassUtils
188+
.getDeclaredConstructorIfAvailable(repositoryFactoryBean, Class.class);
188189

189190
if (constructor == null) {
190191
throw new IllegalStateException("No constructor accepting Class in " + repositoryFactoryBean.getName());

src/main/java/org/springframework/data/util/ClassUtils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*/
1616
package org.springframework.data.util;
1717

18+
import java.lang.reflect.Constructor;
1819
import java.util.function.Consumer;
1920

2021
import org.jspecify.annotations.Nullable;
2122

23+
import org.springframework.util.Assert;
24+
2225
/**
2326
* Utility class to work with classes.
2427
*
@@ -74,4 +77,27 @@ public static void ifPresent(String className, @Nullable ClassLoader classLoader
7477
}
7578
}
7679

80+
/**
81+
* Determine whether the given class has a public constructor with the given signature, and return it if available
82+
* (else return {@code null}).
83+
* <p>
84+
* Essentially translates {@code NoSuchMethodException} to {@code null}.
85+
*
86+
* @param clazz the clazz to analyze
87+
* @param paramTypes the parameter types of the method
88+
* @return the constructor, or {@code null} if not found
89+
* @see Class#getDeclaredConstructor
90+
* @since 4.0
91+
*/
92+
public static <T> @Nullable Constructor<T> getDeclaredConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) {
93+
94+
Assert.notNull(clazz, "Class must not be null");
95+
96+
try {
97+
return clazz.getDeclaredConstructor(paramTypes);
98+
} catch (NoSuchMethodException ex) {
99+
return null;
100+
}
101+
}
102+
77103
}

0 commit comments

Comments
 (0)