Class CodeSourceLocations

java.lang.Object
com.ultikits.ultitools.utils.CodeSourceLocations

@Internal public final class CodeSourceLocations extends Object
Turns a module's CodeSource.getLocation() URL into the File it names. Message catalogue loading and module resource extraction use this same resolver for JAR paths and exploded class directories, including paths with spaces or non-ASCII characters.

Internal to the framework; not a public API.

Since:
6.3.0
  • Constructor Details

    • CodeSourceLocations

      private CodeSourceLocations()
  • Method Details

    • findOnSuperclassChain

      @Nullable public static @Nullable URL findOnSuperclassChain(Class<?> runtimeClass)
      Finds the first usable code location from the runtime class through its superclasses. A normal Paper plugin has its own location, so its JAR remains the result without visiting any superclass. A generated subclass may have no CodeSource or a CodeSource without a location; both allow the search to continue, stopping before the Paper API.
      Parameters:
      runtimeClass - the plugin's runtime class
      Returns:
      the nearest non-null code location, or null if none exists
    • findLocatedClass

      @Nullable public static @Nullable Class<?> findLocatedClass(Class<?> runtimeClass)
      Returns the class that owns the location, so directory scans can also use its defining loader.
    • toFile

      public static File toFile(URL location)
      Resolves a code-source URL as a File, decoding percent escapes through the URL's URI. URL.getPath() does not decode: a server installed under a path with a space or a non-ASCII character yields %20/%E6... escapes that name no file, so new File(url.getPath()) finds nothing there. URI decoding, unlike URLDecoder, also leaves a literal '+' in a folder name as a plus sign.

      Falls back to the raw-path rule the framework used before (the URL's undecoded path, with a leading character dropped when the path is not empty and does not start with '/') in two cases, so resolving a location never adds a failure path where reading the raw path never failed:

      Parameters:
      location - the CodeSource.getLocation() URL, never null
      Returns:
      the location as a File; never null
    • isDefinedInDirectory

      public static boolean isDefinedInDirectory(Class<?> entity, Path directory)
      Checks the defining (not inherited) code source against a normalized scan root.
    • directoryOf

      @Nullable public static @Nullable Path directoryOf(Class<?> type)
      Returns the defining class directory, or null for a JAR or an unavailable code source.
    • packageDirectory

      @Nullable public static @Nullable Path packageDirectory(Path codeRoot, String basePackage)
      Resolves a dotted package against a normalized class directory. The returned path need not exist; component discovery may fall back to another root, whereas entity ownership may not. Returns null if the requested path would leave the class directory.
    • isCandidateClassFile

      public static boolean isCandidateClassFile(String name)
      Returns true if the file name or slash-separated JAR entry names a .class file other than a package descriptor (package-info.class) or module descriptor (module-info.class). Callers apply their own META-INF rule.
    • classNamesInDirectory

      public static Stream<String> classNamesInDirectory(Path codeRoot, Path subtree) throws IOException
      Enumerates binary class names rooted at codeRoot, visiting only subtree. Excludes package descriptors (package-info.class), module descriptors (module-info.class), and entries under the root META-INF directory. The caller closes the returned stream. Names are candidates only: for source-local discovery, compare the resolved class's directoryOf(Class) with codeRoot.
      Throws:
      IOException