Add Reflect class for getting constructors
This commit is contained in:
parent
1675f166fa
commit
18069b3248
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue