Class ReflectiveAccess
java.lang.Object
dev.ua.theroer.magicutils.reflect.ReflectiveAccess
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceMaps source names from an external namespace to names visible in the current runtime. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T> Casts an object to the expected type safely.firstMethod(Class<?> type, Predicate<Method> predicate) Finds the first public method in the given type that matches the predicate.firstPublicStaticField(Class<?> type, Predicate<Field> predicate) Finds the first public static field in the given type that matches the predicate.Invokes a method on a target object with the given arguments.Attempts to load a class by its fully qualified name.loadFirstAvailable(String... classNames) Attempts to load the first available class from a list of names.loadMappedClass(String className) Attempts to load a class directly and then through registered name resolvers.loadMappedClass(String namespace, String className) Attempts to load a class directly and then through registered name resolvers.publicField(Class<?> type, String name) Attempts to find a public field in the given type by its name.publicMappedField(Class<?> type, String name) Attempts to find a public field directly and then through registered name resolvers.publicMappedField(Class<?> type, String namespace, String ownerClassName, String fieldName, String descriptor) Attempts to find a public field directly and then through registered name resolvers.publicMappedMethod(Class<?> type, String name, Class<?>... parameterTypes) Attempts to find a public method directly and then through registered name resolvers.publicMappedMethod(Class<?> type, String namespace, String ownerClassName, String methodName, String descriptor, Class<?>... parameterTypes) Attempts to find a public method directly and then through registered name resolvers.publicMethod(Class<?> type, String name, Class<?>... parameterTypes) Attempts to find a public method in the given type.Reads the value of a field from a target object.static voidRegisters a runtime name resolver used by mapped lookup helpers.Creates a predicate to match a method signature.
-
Method Details
-
registerNameResolver
Registers a runtime name resolver used by mapped lookup helpers.- Parameters:
resolver- resolver to add
-
loadClass
-
loadMappedClass
-
loadMappedClass
Attempts to load a class directly and then through registered name resolvers.- Parameters:
namespace- source namespace for mapped lookupclassName- class name to load- Returns:
- an Optional containing the class if found, otherwise empty
-
loadFirstAvailable
Attempts to load the first available class from a list of names. Useful for handling different environment-specific class names (e.g., Yarn vs Mojang mappings).- Parameters:
classNames- list of class names to try- Returns:
- an Optional containing the first successfully loaded class, otherwise empty
-
publicMethod
Attempts to find a public method in the given type. The method is automatically made accessible.- Parameters:
type- the class to search inname- the name of the methodparameterTypes- the parameter types of the method- Returns:
- an Optional containing the method if found, otherwise empty
-
publicMappedMethod
public static Optional<Method> publicMappedMethod(Class<?> type, String name, Class<?>... parameterTypes) Attempts to find a public method directly and then through registered name resolvers.- Parameters:
type- the class to search inname- method name to findparameterTypes- the parameter types of the method- Returns:
- an Optional containing the method if found, otherwise empty
-
publicMappedMethod
public static Optional<Method> publicMappedMethod(Class<?> type, String namespace, String ownerClassName, String methodName, String descriptor, Class<?>... parameterTypes) Attempts to find a public method directly and then through registered name resolvers.- Parameters:
type- the class to search innamespace- source namespace for mapped lookupownerClassName- fully qualified source owner class namemethodName- method name to finddescriptor- JVM method descriptor in the source namespace, when availableparameterTypes- runtime parameter types of the method- Returns:
- an Optional containing the method if found, otherwise empty
-
firstMethod
Finds the first public method in the given type that matches the predicate.- Parameters:
type- the class to search inpredicate- the condition to match- Returns:
- an Optional containing the first matching method, otherwise empty
-
publicField
Attempts to find a public field in the given type by its name. The field is automatically made accessible.- Parameters:
type- the class to search inname- the name of the field- Returns:
- an Optional containing the field if found, otherwise empty
-
publicMappedField
-
publicMappedField
public static Optional<Field> publicMappedField(Class<?> type, String namespace, String ownerClassName, String fieldName, String descriptor) Attempts to find a public field directly and then through registered name resolvers.- Parameters:
type- the class to search innamespace- source namespace for mapped lookupownerClassName- fully qualified source owner class namefieldName- field name to finddescriptor- JVM field descriptor in the source namespace, when available- Returns:
- an Optional containing the field if found, otherwise empty
-
firstPublicStaticField
Finds the first public static field in the given type that matches the predicate.- Parameters:
type- the class to search inpredicate- the condition to match- Returns:
- an Optional containing the first matching field, otherwise empty
-
readField
-
invoke
Invokes a method on a target object with the given arguments.- Parameters:
method- the method to invoketarget- the object instance (null for static methods)args- the arguments for the invocation- Returns:
- an Optional containing the invocation result if successful, otherwise empty
-
cast
Casts an object to the expected type safely. Handles primitive-to-wrapper conversions.- Type Parameters:
T- the target type- Parameters:
value- the object to castexpectedType- the class to cast to- Returns:
- an Optional containing the casted value if successful, otherwise empty
-
signature
Creates a predicate to match a method signature.- Parameters:
name- the method name (null to ignore)returnType- the return type (null to ignore)params- the parameter types- Returns:
- a predicate for method matching
-