Class ReflectionUtils
java.lang.Object
de.craftsblock.craftsnet.utils.reflection.ReflectionUtils
Some reflection utilities.
- Since:
- 3.2.0-SNAPSHOT
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareArgsCompatible(Executable executable, Class<?>... args) Checks whether the provided argument types are compatible with the parameter types of the executable.static <T> TChecks if the object can be cast to a targeted type and casts it.static <T> Class<T>extractGeneric(Class<?> clazz, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic type parameter from a Class.static <T> Class<T>extractGeneric(Class<?> clazz, Class<?> base, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic type parameter from a Class.static <T> @Nullable Class<T>extractGenericInterface(Class<?> clazz, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic typeReflectionUtilsfrom a given class.static <T> Constructor<T>findConstructor(Class<T> clazz, Class<?>... args) Gets the constructor of the specified class with the provided argument types.static FieldFinds a field with the given name in the specified class, including inherited classes.static MethodfindMethod(Class<?> type, String name, Class<?>... args) Finds a method by name and argument types in the given class, its superclasses, and interfaces.static Class<?>Retrieves the class of the immediate caller of the method in which this method is called.static Class<?>getCallerClass(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int level) Retrieves the class of the caller at a specified depth in the current thread's stack trace.static <T> TgetNewInstance(@NotNull Class<T> type, @NotNull Object... args) Creates a new instance of a class.static ObjectinvokeMethod(Object owner, Method method, Object... args) Invokes a specific method with the given arguments on the specified owner object.static ObjectinvokeMethod(Object owner, String name, Object... args) Invokes a method with the given name and arguments on the specified owner object.static <A extends Annotation>
booleanisAnnotationPresent(Object obj, Class<A> annotation) Checks if a given annotation is present on the object.static booleanisConstructorPresent(Class<?> clazz, Class<?>... args) Checks if a constructor is present in the specified class with the provided argument types.static voidrestrictToCallers(Class<?>... allowed) Ensures that the caller's caller is of one of the specified allowed types.static <A extends Annotation>
AretrieveRawAnnotation(AnnotatedElement element, Class<A> annotation) This method searches for an annotation of the specified type in an object.static <A extends Annotation,T>
TretrieveValueOfAnnotation(AnnotatedElement element, Class<A> annotationClass, Class<T> type, boolean fallback) Retrieves the value of a specified annotation attribute from an object.static voidSets a field in the given object using reflection.
-
Method Details
-
getCallerClass
Retrieves the class of the immediate caller of the method in which this method is called.- Returns:
- The
Classobject of the immediate caller. - Throws:
SecurityException- if a security manager is present and denies access to the stack trace.- Since:
- 3.3.1-SNAPSHOT
-
getCallerClass
public static Class<?> getCallerClass(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int level) Retrieves the class of the caller at a specified depth in the current thread's stack trace.The depth is controlled by the
levelparameter, which specifies how many frames to skip in the stack trace. Alevelof 1 corresponds to the immediate caller of this method, alevelof 2 corresponds to the caller's caller, and so on.- Parameters:
level- The number of stack frames to skip to find the desired caller's class. Must be greater than or equal to 1. Usually needs to be 2.- Returns:
- The
Classobject of the caller at the specified depth. - Throws:
IllegalArgumentException- If the providedlevelis less than 1.SecurityException- If a security manager is present and denies access to the stack trace.- Since:
- 3.3.1-SNAPSHOT
-
restrictToCallers
Ensures that the caller's caller is of one of the specified allowed types.This method is intended to restrict access to internal APIs or methods, by validating that the method is only invoked (indirectly) by allowed classes. If the caller's caller is not assignable to any of the specified classes, an
IllegalStateExceptionis thrown.- Parameters:
allowed- The list of classes that are allowed to indirectly call the current method.- Throws:
IllegalStateException- If the caller's caller is not permitted to call the method.- Since:
- 3.5.0
-
isConstructorPresent
Checks if a constructor is present in the specified class with the provided argument types.- Parameters:
clazz- The class to check for the constructor.args- The argument types to check for.- Returns:
- true if the constructor is present, false otherwise.
-
findConstructor
Gets the constructor of the specified class with the provided argument types.- Parameters:
clazz- The class to get the constructor for.args- The argument types for the constructor.- Returns:
- The constructor of the class.
- Throws:
RuntimeException- If no constructor is found.
-
getNewInstance
@NotNull public static <T> T getNewInstance(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... args) Creates a new instance of a class.- Type Parameters:
T- The desired result type.- Parameters:
type- The class type of the desired class.args- The args for the constructor.- Returns:
- A new instance of the given class type.
- Throws:
IllegalStateException- if no matching constructor is found for the args.RuntimeException- if the instantiation failed.- Since:
- 3.4.0-SNAPSHOT
-
setField
Sets a field in the given object using reflection.- Parameters:
name- The name of the field to set.target- The object whose field needs to be set.value- The value to set the field to.
-
findField
Finds a field with the given name in the specified class, including inherited classes.- Parameters:
clazz- The class to search for the field.name- The name of the field to find.- Returns:
- The field if found, otherwise null.
-
invokeMethod
Invokes a method with the given name and arguments on the specified owner object.- Parameters:
owner- The object on which the method is to be invoked.name- The name of the method.args- The arguments to pass to the method.- Returns:
- The result returned by the invoked method.
- Throws:
IllegalStateException- If no matching method is found.RuntimeException- If the method invocation fails.- Since:
- 3.5.0
-
invokeMethod
Invokes a specific method with the given arguments on the specified owner object.- Parameters:
owner- The object on which the method is to be invoked.method- The method to invoke.args- The arguments to pass to the method.- Returns:
- The result returned by the invoked method.
- Since:
- 3.5.1
-
findMethod
Finds a method by name and argument types in the given class, its superclasses, and interfaces.- Parameters:
type- The class to search for the method.name- The name of the method.args- The argument types of the method.- Returns:
- The
Methodif found;nullotherwise. - Since:
- 3.5.0
-
areArgsCompatible
Checks whether the provided argument types are compatible with the parameter types of the executable.Supports checking compatibility for varargs methods.
- Parameters:
executable- TheExecutable(method or constructor) to check against.args- The argument types to verify.- Returns:
trueif the argument types are compatible;falseotherwise.- Since:
- 3.5.0
-
extractGeneric
public static <T> Class<T> extractGeneric(Class<?> clazz, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic type parameter from a Class.- Type Parameters:
T- The type of handler.- Parameters:
clazz- The Class to extract the generic type from.index- The index of the generic.- Returns:
- The class type corresponding to the handler's generic type.
- Since:
- 3.4.3-SNAPSHOT
-
extractGeneric
public static <T> Class<T> extractGeneric(Class<?> clazz, Class<?> base, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic type parameter from a Class.- Type Parameters:
T- The type of handler.- Parameters:
clazz- The Class to extract the generic type from.base- The base class at which will be stopped, when no generics are found. (exklusive)index- The index of the generic.- Returns:
- The class type corresponding to the handler's generic type.
-
extractGenericInterface
@Nullable public static <T> @Nullable Class<T> extractGenericInterface(Class<?> clazz, @org.jetbrains.annotations.Range(from=0L, to=2147483647L) int index) Extracts the generic typeReflectionUtilsfrom a given class.- Type Parameters:
T- The type of service handled by the service loader.- Parameters:
clazz- The class loader from which to extract the generic type.index- The index of the generic.- Returns:
- The
Classobject representing the generic typeReflectionUtils, or null if unable to extract. - See Also:
-
isAnnotationPresent
Checks if a given annotation is present on the object.- Type Parameters:
A- The type of the annotation.- Parameters:
obj- The object in which to search for the annotation.annotation- The class of the annotation to search for.- Returns:
trueif the annotation is present,falseotherwise.- Since:
- 3.4.0-SNAPSHOT
-
retrieveRawAnnotation
public static <A extends Annotation> A retrieveRawAnnotation(AnnotatedElement element, Class<A> annotation) This method searches for an annotation of the specified type in an object.- Type Parameters:
A- The type of the annotation.- Parameters:
element- The element in which to search for the annotation.annotation- The class of the annotation to search for.- Returns:
- The found annotation or null if none is found.
-
retrieveValueOfAnnotation
public static <A extends Annotation,T> T retrieveValueOfAnnotation(AnnotatedElement element, Class<A> annotationClass, Class<T> type, boolean fallback) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException Retrieves the value of a specified annotation attribute from an object.- Type Parameters:
A- The type of the annotation.T- The type of the attribute value.- Parameters:
element- The element containing the annotation.annotationClass- The class of the annotation.type- The class of the targeted type.fallback- Defines if the default value should be returned.- Returns:
- The value of the specified annotation attribute.
- Throws:
NoSuchMethodException- If the attribute's getter method is not found.InvocationTargetException- If there is an issue invoking the getter method.IllegalAccessException- If there is an access issue with the getter method.
-
castTo
Checks if the object can be cast to a targeted type and casts it.- Type Parameters:
T- The targeted type- Parameters:
o- The value which should be cast to the targeted type.type- The class of the targeted type- Returns:
- Returns the cast value or null if not cast able.
-