Class ResourceResolver

java.lang.Object
eu.cloudnetservice.utils.base.resource.ResourceResolver

public final class ResourceResolver extends Object
A utility class to resolve the location of class resource on the class path or file system.
Since:
4.0
  • Constructor Details

    • ResourceResolver

      private ResourceResolver()
  • Method Details

    • resolveCodeSourceOfClass

      @NonNull public static @NonNull URI resolveCodeSourceOfClass(@NonNull @NonNull Class<?> caller)
      Resolves the parent location of the given class on the file system or on the class path. This means that the method either returns the jar file that contains the given class or the directory in which the given class is located.
      Parameters:
      caller - the class to get the parent location of.
      Returns:
      a URI pointing to the parent location of the given class.
      Throws:
      NullPointerException - if the given class is null.
      IllegalStateException - if there is an error resolving the parent class uri.
      IllegalArgumentException - if a parent uri candidate was found but couldn't be converted into a URI object.
    • openCodeSourceRoot

      public static void openCodeSourceRoot(@NonNull @NonNull Class<?> caller, @NonNull @NonNull CheckedConsumer<Path> consumer)
      Finds the code source of the caller and passes it to the given consumer. The passed path can either be the root of a zip file system (if the code source is located in a jar) or the absolute path to a directory on the system (if the source is a file on the default file system).
      Parameters:
      caller - the class to get the parent location of.
      consumer - the consumer to which the code source root path should be passed.
      Throws:
      NullPointerException - if the given caller or consumer is null.
      IllegalStateException - if an exception occurs while executing the given consumer.
    • resolveError

      @NonNull private static @NonNull IllegalStateException resolveError(@NonNull @NonNull Class<?> caller, @NonNull @NonNull String reason)
      Constructs a new, unified illegal state exception that can be thrown to indicate that there was some error resolving the URI of a class. The returned exceptions contains the class name and the given error as its detail message.
      Parameters:
      caller - the class to get the parent location of.
      reason - the reason why the uri resolution process failed.
      Returns:
      a new illegal state exception using a unified detail message based on the given caller class and reason.
      Throws:
      NullPointerException - if the given caller class or reason is null.