docs(unsafe): Document commons-unsafe
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-03-09 14:08:27 +01:00
parent 3d3c657497
commit 141eb8a280
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 329 additions and 13 deletions

View File

@ -5,6 +5,9 @@ import io.gitlab.jfronny.commons.throwable.Try;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
/**
* A wrapper for sun.misc.Unsafe
*/
@SuppressWarnings({"unchecked", "unused"})
public class Unsafe {
public static final sun.misc.Unsafe theUnsafe = Try.orThrow(() -> {

View File

@ -6,14 +6,30 @@ import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.function.*;
/**
* A class that wraps the LambdaMetafactory to more easily create lambdas from MethodHandles.
*/
@SuppressWarnings("unchecked")
public class LambdaFactory {
private static final MethodHandles.Lookup LOOKUP_ROOT = MethodHandles.lookup();
/**
* Creates a private lookup for the given class.
* @param klazz the class to create the lookup for
* @return a lookup for the given class
* @throws IllegalAccessException if the lookup cannot be created
*/
public static MethodHandles.Lookup lookup(Class<?> klazz) throws IllegalAccessException {
return MethodHandles.privateLookupIn(klazz, LOOKUP_ROOT);
}
/**
* Creates a new Runnable from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @return a new Runnable that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static Runnable runnable(MethodHandles.Lookup lookup, MethodHandle handle) throws Throwable {
return (Runnable) LambdaMetafactory.metafactory(
lookup,
@ -25,6 +41,15 @@ public class LambdaFactory {
).getTarget().invoke();
}
/**
* Creates a new Consumer from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @param parameterType the type of the parameter
* @param <TIn> the type of the parameter
* @return a new Consumer that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static <TIn> Consumer<TIn> consumer(MethodHandles.Lookup lookup, MethodHandle handle, Class<TIn> parameterType) throws Throwable {
return (Consumer<TIn>) LambdaMetafactory.metafactory(
lookup,
@ -36,6 +61,17 @@ public class LambdaFactory {
).getTarget().invoke();
}
/**
* Creates a new BiConsumer from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @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 new BiConsumer that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static <TIn1, TIn2> BiConsumer<TIn1, TIn2> consumer(MethodHandles.Lookup lookup, MethodHandle handle, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
return (BiConsumer<TIn1, TIn2>) LambdaMetafactory.metafactory(
lookup,
@ -47,6 +83,15 @@ public class LambdaFactory {
).getTarget().invoke();
}
/**
* Creates a new Supplier from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @param returnType the return type of the supplier
* @param <TOut> the return type of the supplier
* @return a new Supplier that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static <TOut> Supplier<TOut> supplier(MethodHandles.Lookup lookup, MethodHandle handle, Class<TOut> returnType) throws Throwable {
return (Supplier<TOut>) LambdaMetafactory.metafactory(
lookup,
@ -58,6 +103,17 @@ public class LambdaFactory {
).getTarget().invoke();
}
/**
* Creates a new Function from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @param returnType the return type of the function
* @param parameterType the type of the parameter
* @param <TIn> the type of the parameter
* @param <TOut> the return type of the function
* @return a new Function that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static <TIn, TOut> Function<TIn, TOut> function(MethodHandles.Lookup lookup, MethodHandle handle, Class<TOut> returnType, Class<TIn> parameterType) throws Throwable {
return (Function<TIn, TOut>) LambdaMetafactory.metafactory(
lookup,
@ -69,6 +125,19 @@ public class LambdaFactory {
).getTarget().invoke();
}
/**
* Creates a new BiFunction from the given handle.
* @param lookup the lookup to use
* @param handle the handle to create the lambda from
* @param returnType the return type of the function
* @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 function
* @return a new BiFunction that calls the given handle
* @throws Throwable if the lambda cannot be created
*/
public static <TIn1, TIn2, TOut> BiFunction<TIn1, TIn2, TOut> function(MethodHandles.Lookup lookup, MethodHandle handle, Class<TOut> returnType, Class<TIn1> parameterType1, Class<TIn2> parameterType2) throws Throwable {
return (BiFunction<TIn1, TIn2, TOut>) LambdaMetafactory.metafactory(
lookup,

View File

@ -3,134 +3,378 @@ 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 {
// Constructor without parameters
/**
* 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));
}
// Constructor with one parameter
/**
* 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);
}
// Constructor with two parameters
/**
* 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);
}
// Procedure without parameters
/**
* 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);
}
// Procedure with one parameter
/**
* 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);
}
// Procedure with two parameters
/**
* 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);
}
// Function without parameters
/**
* 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);
}
// Function with one parameter
/**
* 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);
}
// Function with two parameters
/**
* 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);
}
// Procedure without parameters
/**
* 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);
}
// Procedure with one parameter
/**
* 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);
}
// Function without parameters
/**
* 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);
}
// Function with one parameter
/**
* 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);
}