From 18069b3248551df96a2f10c3f148a45d6385eab9 Mon Sep 17 00:00:00 2001 From: JFronny Date: Tue, 10 May 2022 11:21:13 +0200 Subject: [PATCH] Add Reflect class for getting constructors --- .../jfronny/commons/log/LoggerHolder.java | 9 ++-- .../jfronny/commons/reflect/Reflect.java | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/gitlab/jfronny/commons/reflect/Reflect.java diff --git a/src/main/java/io/gitlab/jfronny/commons/log/LoggerHolder.java b/src/main/java/io/gitlab/jfronny/commons/log/LoggerHolder.java index 7c991a8..c7315bb 100644 --- a/src/main/java/io/gitlab/jfronny/commons/log/LoggerHolder.java +++ b/src/main/java/io/gitlab/jfronny/commons/log/LoggerHolder.java @@ -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 constructor = (Constructor) Class.forName("io.gitlab.jfronny.commons.log.SLF4JLogger") - .getConstructor(String.class); - LOGGER_BUILDER = Coerce.function((ThrowingFunction) constructor::newInstance).orThrow(); + LOGGER_BUILDER = Reflect.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; diff --git a/src/main/java/io/gitlab/jfronny/commons/reflect/Reflect.java b/src/main/java/io/gitlab/jfronny/commons/reflect/Reflect.java new file mode 100644 index 0000000..026a8a2 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/commons/reflect/Reflect.java @@ -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 ThrowingSupplier getConstructor(Class toConstruct) throws NoSuchMethodException { + Constructor constructor = (Constructor) toConstruct.getDeclaredConstructor(); + constructor.setAccessible(true); + return constructor::newInstance; + } + + public static ThrowingSupplier getConstructor(String targetClassName) throws NoSuchMethodException, ClassNotFoundException { + return getConstructor(Class.forName(targetClassName)); + } + + // Constructor with one parameter + public static ThrowingFunction getConstructor(Class toConstruct, Class parameterType) throws NoSuchMethodException { + Constructor constructor = (Constructor) toConstruct.getDeclaredConstructor(parameterType); + constructor.setAccessible(true); + return constructor::newInstance; + } + + public static ThrowingFunction getConstructor(String targetClassName, Class parameterType) throws NoSuchMethodException, ClassNotFoundException { + return getConstructor(Class.forName(targetClassName), parameterType); + } + + // Constructor with two parameters + public static ThrowingBiFunction getConstructor(Class toConstruct, Class parameterType1, Class parameterType2) throws NoSuchMethodException { + Constructor constructor = (Constructor) toConstruct.getDeclaredConstructor(parameterType1, parameterType2); + constructor.setAccessible(true); + return constructor::newInstance; + } + + public static ThrowingBiFunction getConstructor(String targetClassName, Class parameterType1, Class parameterType2) throws NoSuchMethodException, ClassNotFoundException { + return getConstructor(Class.forName(targetClassName), parameterType1, parameterType2); + } +}