Class ReflectionUtils

java.lang.Object
de.craftsblock.craftsnet.utils.reflection.ReflectionUtils

public class ReflectionUtils extends Object
Some reflection utilities.
Since:
3.2.0-SNAPSHOT
  • Method Details

    • getCallerClass

      public static Class<?> getCallerClass()
      Retrieves the class of the immediate caller of the method in which this method is called.
      Returns:
      The Class object 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 level parameter, which specifies how many frames to skip in the stack trace. A level of 1 corresponds to the immediate caller of this method, a level of 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 Class object of the caller at the specified depth.
      Throws:
      IllegalArgumentException - If the provided level is less than 1.
      SecurityException - If a security manager is present and denies access to the stack trace.
      Since:
      3.3.1-SNAPSHOT
    • restrictToCallers

      public static void restrictToCallers(Class<?>... allowed)
      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 IllegalStateException is 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

      public static boolean isConstructorPresent(Class<?> clazz, Class<?>... args)
      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

      public static <T> Constructor<T> findConstructor(Class<T> clazz, Class<?>... args)
      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

      public static void setField(String name, Object target, Object value)
      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

      public static Field findField(Class<?> clazz, String name)
      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

      public static Object invokeMethod(Object owner, String name, Object... args)
      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

      public static Object invokeMethod(Object owner, Method method, Object... args)
      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

      public static Method findMethod(Class<?> type, String name, Class<?>... args)
      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 Method if found; null otherwise.
      Since:
      3.5.0
    • areArgsCompatible

      public static boolean areArgsCompatible(Executable executable, Class<?>... args)
      Checks whether the provided argument types are compatible with the parameter types of the executable.

      Supports checking compatibility for varargs methods.

      Parameters:
      executable - The Executable (method or constructor) to check against.
      args - The argument types to verify.
      Returns:
      true if the argument types are compatible; false otherwise.
      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 type ReflectionUtils from 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 Class object representing the generic type ReflectionUtils, or null if unable to extract.
      See Also:
    • isAnnotationPresent

      public static <A extends Annotation> boolean isAnnotationPresent(Object obj, Class<A> annotation)
      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:
      true if the annotation is present, false otherwise.
      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

      @Nullable public static <T> T castTo(Object o, Class<T> type)
      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.