382 lines
20 KiB
Java
382 lines
20 KiB
Java
package io.gitlab.jfronny.commons.unsafe.reflect;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
import java.util.function.*;
|
|
|
|
/**
|
|
* A class that provides a set of methods to create functional interfaces for reflective calls.
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public class Reflect {
|
|
/**
|
|
* Creates a supplier that constructs the given class using the default, no-arg constructor.
|
|
* @param toConstruct the class to construct
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a supplier that constructs the class
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TOut> Supplier<TOut> constructor(Class<TOut> toConstruct) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(toConstruct);
|
|
return LambdaFactory.supplier(lookup, lookup.unreflectConstructor(toConstruct.getDeclaredConstructor()), toConstruct);
|
|
}
|
|
|
|
/**
|
|
* Creates a supplier that constructs the given class using the default, no-arg constructor.
|
|
* @param targetClassName the name of the class to construct
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a supplier that constructs the class
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TOut> Supplier<TOut> constructor(String targetClassName) throws Throwable {
|
|
return (Supplier<TOut>) constructor(Class.forName(targetClassName));
|
|
}
|
|
|
|
/**
|
|
* Creates a function that constructs the given class using a constructor with one parameter.
|
|
* @param toConstruct the class to construct
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a function that constructs the class given a parameter
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn, TOut> Function<TIn, TOut> constructor(Class<TOut> toConstruct, Class<TIn> parameterType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(toConstruct);
|
|
return LambdaFactory.function(lookup, lookup.unreflectConstructor(toConstruct.getDeclaredConstructor(parameterType)), toConstruct, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that constructs the given class using a constructor with one parameter.
|
|
* @param targetClassName the name of the class to construct
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a function that constructs the class given a parameter
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn, TOut> Function<TIn, TOut> constructor(String targetClassName, Class<TIn> parameterType) throws Throwable {
|
|
return (Function<TIn, TOut>) constructor(Class.forName(targetClassName), parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that constructs the given class using a constructor with two parameters.
|
|
* @param toConstruct the class to construct
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a function that constructs the class given two parameters
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2, TOut> BiFunction<TIn1, TIn2, TOut> constructor(Class<TOut> toConstruct, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(toConstruct);
|
|
return LambdaFactory.function(lookup, lookup.unreflectConstructor(toConstruct.getDeclaredConstructor(parameterType1, parameterType2)), toConstruct, parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that constructs the given class using a constructor with two parameters.
|
|
* @param targetClassName the name of the class to construct
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @param <TOut> the type of the class to construct
|
|
* @return a function that constructs the class given two parameters
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2, TOut> BiFunction<TIn1, TIn2, TOut> constructor(String targetClassName, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
return constructor((Class<TOut>) Class.forName(targetClassName), parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a runnable that calls the given static method, which returns void and takes no arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @return a runnable that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static Runnable staticProcedure(Class<?> targetClass, String name) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.runnable(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name)));
|
|
}
|
|
|
|
/**
|
|
* Creates a runnable that calls the given static method, which returns void and takes no arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @return a runnable that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static Runnable staticProcedure(String targetClassName, String name) throws Throwable {
|
|
return staticProcedure(Class.forName(targetClassName), name);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given static method, which returns void and takes one argument.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn> Consumer<TIn> staticProcedure(Class<?> targetClass, String name, Class<TIn> parameterType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.consumer(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType)), parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given static method, which returns void and takes one argument.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn> Consumer<TIn> staticProcedure(String targetClassName, String name, Class<TIn> parameterType) throws Throwable {
|
|
return staticProcedure(Class.forName(targetClassName), name, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given static method, which returns void and takes two arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2> BiConsumer<TIn1, TIn2> staticProcedure(Class<?> targetClass, String name, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.consumer(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType1, parameterType2)), parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given static method, which returns void and takes two arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2> BiConsumer<TIn1, TIn2> staticProcedure(String targetClassName, String name, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
return staticProcedure(Class.forName(targetClassName), name, parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a supplier that calls the given static method, which returns a value and takes no arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param <TOut> the return type of the method
|
|
* @return a supplier that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TOut> Supplier<TOut> staticFunction(Class<?> targetClass, String name, Class<TOut> returnType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.supplier(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name)), returnType);
|
|
}
|
|
|
|
/**
|
|
* Creates a supplier that calls the given static method, which returns a value and takes no arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param <TOut> the return type of the method
|
|
* @return a supplier that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TOut> Supplier<TOut> staticFunction(String targetClassName, String name, Class<TOut> returnType) throws Throwable {
|
|
return staticFunction(Class.forName(targetClassName), name, returnType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given static method, which returns a value and takes one argument.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn, TOut> Function<TIn, TOut> staticFunction(Class<?> targetClass, String name, Class<TOut> returnType, Class<TIn> parameterType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.function(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType)), returnType, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given static method, which returns a value and takes one argument.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn, TOut> Function<TIn, TOut> staticFunction(String targetClassName, String name, Class<TOut> returnType, Class<TIn> parameterType) throws Throwable {
|
|
return staticFunction(Class.forName(targetClassName), name, returnType, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given static method, which returns a value and takes two arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2, TOut> BiFunction<TIn1, TIn2, TOut> staticFunction(Class<?> targetClass, String name, Class<TOut> returnType, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.function(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType1, parameterType2)), returnType, parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given static method, which returns a value and takes two arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType1 the type of the first parameter
|
|
* @param parameterType2 the type of the second parameter
|
|
* @param <TIn1> the type of the first parameter
|
|
* @param <TIn2> the type of the second parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TIn1, TIn2, TOut> BiFunction<TIn1, TIn2, TOut> staticFunction(String targetClassName, String name, Class<TOut> returnType, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
|
|
return staticFunction(Class.forName(targetClassName), name, returnType, parameterType1, parameterType2);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given instance method, which returns void and takes no arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @return a supplier that constructs the class
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget> Consumer<TTarget> instanceProcedure(Class<TTarget> targetClass, String name) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.consumer(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name)), targetClass);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given instance method, which returns void and takes no arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @return a supplier that constructs the class
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget> Consumer<TTarget> instanceProcedure(String targetClassName, String name) throws Throwable {
|
|
return (Consumer<TTarget>) instanceProcedure(Class.forName(targetClassName), name);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given instance method, which returns void and takes one argument.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TIn> the type of the parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TIn> BiConsumer<TTarget, TIn> instanceProcedure(Class<TTarget> targetClass, String name, Class<TIn> parameterType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.consumer(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType)), targetClass, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a consumer that calls the given instance method, which returns void and takes one argument.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TIn> the type of the parameter
|
|
* @return a consumer that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TIn> BiConsumer<TTarget, TIn> instanceProcedure(String targetClassName, String name, Class<TIn> parameterType) throws Throwable {
|
|
return (BiConsumer<TTarget, TIn>) instanceProcedure(Class.forName(targetClassName), name, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given instance method, which returns a value and takes no arguments.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TOut> Function<TTarget, TOut> instanceFunction(Class<TTarget> targetClass, String name, Class<TOut> returnType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.function(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name)), returnType, targetClass);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given instance method, which returns a value and takes no arguments.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TOut> Function<TTarget, TOut> instanceFunction(String targetClassName, String name, Class<TOut> returnType) throws Throwable {
|
|
return (Function<TTarget, TOut>) instanceFunction(Class.forName(targetClassName), name, returnType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given instance method, which returns a value and takes one argument.
|
|
* @param targetClass the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TIn, TOut> BiFunction<TTarget, TIn, TOut> instanceFunction(Class<TTarget> targetClass, String name, Class<TOut> returnType, Class<TIn> parameterType) throws Throwable {
|
|
MethodHandles.Lookup lookup = LambdaFactory.lookup(targetClass);
|
|
return LambdaFactory.function(lookup, lookup.unreflect(targetClass.getDeclaredMethod(name, parameterType)), returnType, targetClass, parameterType);
|
|
}
|
|
|
|
/**
|
|
* Creates a function that calls the given instance method, which returns a value and takes one argument.
|
|
* @param targetClassName the name of the class containing the method
|
|
* @param name the name of the method
|
|
* @param returnType the return type of the method
|
|
* @param parameterType the type of the parameter
|
|
* @param <TTarget> the type of the class containing the method
|
|
* @param <TIn> the type of the parameter
|
|
* @param <TOut> the return type of the method
|
|
* @return a function that calls the method
|
|
* @throws Throwable if an error occurs
|
|
*/
|
|
public static <TTarget, TIn, TOut> BiFunction<TTarget, TIn, TOut> instanceFunction(String targetClassName, String name, Class<TOut> returnType, Class<TIn> parameterType) throws Throwable {
|
|
return (BiFunction<TTarget, TIn, TOut>) instanceFunction(Class.forName(targetClassName), name, returnType, parameterType);
|
|
}
|
|
}
|