Class ConduitGenerator

java.lang.Object
org.geysermc.conduit.generate.ConduitGenerator

public final class ConduitGenerator extends Object
Generates Conduit interfaces from real classes.

The generator reads the classes a conduit mirrors, and works out from their constructors, factories and methods where each field's value comes from at a call site. That becomes the Conduit.Bind entries on each method. Only the generator needs the real classes. Commit its output, and scanning afterwards needs nothing but the jar being scanned.

ConduitContext context = ConduitContext.load(Path.of("library.jar"));
new ConduitGenerator(context).generateAll(
        Path.of("src/main/java/com/example/conduit"), "com.example.conduit",
        List.of("com.example.Color", "com.example.Palette"));

Each file is stamped with a checksum. Regenerating replaces files that are unchanged since they were generated, and leaves edited ones alone, so a binding the analysis can't derive can be added by hand and survives the next run.

Also runnable from the command line, or through the dev.opencollab.conduit Gradle plugin.

  • Constructor Details

    • ConduitGenerator

      public ConduitGenerator(@NotNull @NotNull ConduitContext context)
      Main constructor.
      Parameters:
      context - the jars holding the classes to generate from, and anything they reference
  • Method Details

    • generate

      public String generate(@NotNull @NotNull String className, @NotNull @NotNull String targetPackage)
      Generates the source of a single conduit.
      Parameters:
      className - the binary name of the class to mirror
      targetPackage - the package to generate into
      Returns:
      the source, or null if the class is not in the context
    • generate

      public String generate(@NotNull @NotNull String className, @NotNull @NotNull String targetPackage, @NotNull @NotNull Set<String> referenced)
      Generates the source of a single conduit, collecting the classes its methods refer to.
      Parameters:
      className - the binary name of the class to mirror
      targetPackage - the package to generate into
      referenced - receives the binary names of non-JDK classes the conduit refers to
      Returns:
      the source, or null if the class is not in the context
    • isUnmodified

      public static boolean isUnmodified(@NotNull @NotNull String source)
      Checks a generated file's checksum against its contents, to tell whether it was edited since it was generated.
      Parameters:
      source - the contents of a generated file
      Returns:
      whether it is unchanged, false if it has no checksum
    • generateAll

      public List<Path> generateAll(@NotNull @NotNull Path directory, @NotNull @NotNull String targetPackage, @NotNull @NotNull List<String> classNames) throws IOException
      Generates a set of conduits that refer to each other. A method whose type is outside the set is left out and reported, rather than generated against a type that won't exist.
      Parameters:
      directory - the directory to write into
      targetPackage - the package to generate into
      classNames - the binary names of the classes to mirror
      Returns:
      the files written
      Throws:
      IOException - if a file can't be read or written
    • main

      public static void main(String[] args) throws IOException
      Generates conduits from the command line, the same way as generateAll(Path, String, List).
      Parameters:
      args - the output directory, the package, a comma-separated list of jars, then each class
      Throws:
      IOException - if a file can't be read or written