Class ClassPathLoader

java.lang.Object
party.iroiro.luajava.ClassPathLoader
All Implemented Interfaces:
ExternalLoader

public class ClassPathLoader extends Object implements ExternalLoader
An ExternalLoader that loads modules from classpath

The path to the resource is converted from module name with getPath(String). For example, loading a abc.def module will load a Lua file at classpath://abc/def.lua.

To use this class, call Lua.setExternalLoader(ExternalLoader) with an instance of this class, and then you can load modules with the Lua require function from classpath.

  • Field Details

    • classLoader

      protected final ClassLoader classLoader
      The class loader used to load Lua modules from the classpath.
  • Constructor Details

    • ClassPathLoader

      public ClassPathLoader()
      Use ClassUtils.getDefaultClassLoader() for resource loading
    • ClassPathLoader

      public ClassPathLoader(ClassLoader classLoader)
      Creates a new ClassPathLoader with the specified class loader.
      Parameters:
      classLoader - the classloader for resource loading
  • Method Details

    • load

      public @Nullable Buffer load(String module, Lua ignored)
      Description copied from interface: ExternalLoader
      Reads an external Lua module file into a direct buffer

      This is used internally by Lua implementations and should not be called directly.

      Specified by:
      load in interface ExternalLoader
      Parameters:
      module - the module
      ignored - the Lua state requesting the module
      Returns:
      a direct buffer containing the module file, with position at zero, limit as the length
    • getPath

      protected String getPath(String module)
      Converts a dot-separated module path to a file system path.
      Parameters:
      module - dot separated module path
      Returns:
      module path with . replaced by /, appended with .lua