Browse Source

Use `getDeclaredConstructor(…)` in `RepositoryBeanDefinitionReader` to avoid failures due to package-private constructors.

Closes #3325
pull/3337/head
Mark Paluch 5 months ago
parent
commit
915dea06fc
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 3
      src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionReader.java
  2. 26
      src/main/java/org/springframework/data/util/ClassUtils.java

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

@ -184,7 +184,8 @@ class RepositoryBeanDefinitionReader {
Assert.state(beanDefinition.getBeanClassName() != null, "No Repository BeanFactory set"); Assert.state(beanDefinition.getBeanClassName() != null, "No Repository BeanFactory set");
Class<?> repositoryFactoryBean = forName(beanDefinition.getBeanClassName()); Class<?> repositoryFactoryBean = forName(beanDefinition.getBeanClassName());
Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(repositoryFactoryBean, Class.class); Constructor<?> constructor = org.springframework.data.util.ClassUtils
.getDeclaredConstructorIfAvailable(repositoryFactoryBean, Class.class);
if (constructor == null) { if (constructor == null) {
throw new IllegalStateException("No constructor accepting Class in " + repositoryFactoryBean.getName()); throw new IllegalStateException("No constructor accepting Class in " + repositoryFactoryBean.getName());

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

@ -15,10 +15,13 @@
*/ */
package org.springframework.data.util; package org.springframework.data.util;
import java.lang.reflect.Constructor;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
/** /**
* Utility class to work with classes. * Utility class to work with classes.
* *
@ -74,4 +77,27 @@ public abstract class ClassUtils {
} }
} }
/**
* Determine whether the given class has a public constructor with the given signature, and return it if available
* (else return {@code null}).
* <p>
* Essentially translates {@code NoSuchMethodException} to {@code null}.
*
* @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method
* @return the constructor, or {@code null} if not found
* @see Class#getDeclaredConstructor
* @since 4.0
*/
public static <T> @Nullable Constructor<T> getDeclaredConstructorIfAvailable(Class<T> clazz, Class<?>... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
try {
return clazz.getDeclaredConstructor(paramTypes);
} catch (NoSuchMethodException ex) {
return null;
}
}
} }

Loading…
Cancel
Save