|
|
|
@ -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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|