Add Reflect class for getting constructors

This commit is contained in:
Johannes Frohnmeyer 2022-05-10 11:21:13 +02:00
parent 1675f166fa
commit 18069b3248
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 45 additions and 6 deletions

View File

@ -1,10 +1,8 @@
package io.gitlab.jfronny.commons.log;
import io.gitlab.jfronny.commons.throwable.Coerce;
import io.gitlab.jfronny.commons.throwable.ThrowingFunction;
import io.gitlab.jfronny.commons.reflect.Reflect;
import org.jetbrains.annotations.ApiStatus;
import java.lang.reflect.Constructor;
import java.util.function.Function;
@ApiStatus.Internal
@ -17,9 +15,8 @@ class LoggerHolder {
public static void reset() {
try {
Constructor<Logger> constructor = (Constructor<Logger>) Class.forName("io.gitlab.jfronny.commons.log.SLF4JLogger")
.getConstructor(String.class);
LOGGER_BUILDER = Coerce.function((ThrowingFunction<String, Logger, Exception>) constructor::newInstance).orThrow();
LOGGER_BUILDER = Reflect.<String, Logger>getConstructor("io.gitlab.jfronny.commons.log.SLF4JLogger", String.class)
.orThrow();
} catch (ClassNotFoundException | NoSuchMethodException e) {
// SLF4J logger is unavailable, use java.util.logging
LOGGER_BUILDER = JavaUtilLogger::new;

View File

@ -0,0 +1,42 @@
package io.gitlab.jfronny.commons.reflect;
import io.gitlab.jfronny.commons.throwable.ThrowingBiFunction;
import io.gitlab.jfronny.commons.throwable.ThrowingFunction;
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
import java.lang.reflect.Constructor;
public class Reflect {
// Constructor without parameters
public static <TOut> ThrowingSupplier<TOut, ReflectiveOperationException> getConstructor(Class<?> toConstruct) throws NoSuchMethodException {
Constructor<TOut> constructor = (Constructor<TOut>) toConstruct.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor::newInstance;
}
public static <TOut> ThrowingSupplier<TOut, ReflectiveOperationException> getConstructor(String targetClassName) throws NoSuchMethodException, ClassNotFoundException {
return getConstructor(Class.forName(targetClassName));
}
// Constructor with one parameter
public static <TIn, TOut> ThrowingFunction<TIn, TOut, ReflectiveOperationException> getConstructor(Class<?> toConstruct, Class<TIn> parameterType) throws NoSuchMethodException {
Constructor<TOut> constructor = (Constructor<TOut>) toConstruct.getDeclaredConstructor(parameterType);
constructor.setAccessible(true);
return constructor::newInstance;
}
public static <TIn, TOut> ThrowingFunction<TIn, TOut, ReflectiveOperationException> getConstructor(String targetClassName, Class<TIn> parameterType) throws NoSuchMethodException, ClassNotFoundException {
return getConstructor(Class.forName(targetClassName), parameterType);
}
// Constructor with two parameters
public static <TIn1, TIn2, TOut> ThrowingBiFunction<TIn1, TIn2, TOut, ReflectiveOperationException> getConstructor(Class<?> toConstruct, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws NoSuchMethodException {
Constructor<TOut> constructor = (Constructor<TOut>) toConstruct.getDeclaredConstructor(parameterType1, parameterType2);
constructor.setAccessible(true);
return constructor::newInstance;
}
public static <TIn1, TIn2, TOut> ThrowingBiFunction<TIn1, TIn2, TOut, ReflectiveOperationException> getConstructor(String targetClassName, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws NoSuchMethodException, ClassNotFoundException {
return getConstructor(Class.forName(targetClassName), parameterType1, parameterType2);
}
}