Class ClassGeneratorOptionsBuilder

java.lang.Object
org.jfuncmachine.compiler.classgen.ClassGeneratorOptionsBuilder

public class ClassGeneratorOptionsBuilder extends Object
A builder used to create a ClassGeneratorOptions class. This builder allows you to easily override just one or two defaults in the ClassGeneratorOptions without having to provide all the constructor parameters. Example: ClassGeneratorOptions options = new ClassGeneratorOptionsBuilder().withAutobox(false).javaVersion(20).build();
  • Field Details

    • javaVersion

      public int javaVersion
      The Java version to generate class files for
    • localTailCallsToLoops

      public boolean localTailCallsToLoops
      Convert local recursive calls (method calling itself) to loops
    • fullTailCalls

      public boolean fullTailCalls
      Generate non-Java-callable methods with tail-call optimization. This requires changing the return type of each method to return an object that contains a reference to the next function to call, or a value representing the return value of the function. This option should not be used unless it is really needed as it adds overhead to each function call.
    • autobox

      public boolean autobox
      Indicates whether the code generator should automatically convert between native types and boxed types (Object-versions of each type, e.g. java.lang.Byte for byte, java.lang.Integer for int).
    • sharedLambdaInterfaces

      public boolean sharedLambdaInterfaces
      Indicates that any lambdas generated for one class can be shared by all classes in the same package. Since the names of the lambdas are dependent on their types, two classes using the sames types for a lambda should be able to share a single interface definition.
    • lambdaMethodName

      public String lambdaMethodName
      The name used for the single method in a lambda interface.
    • genericLambdas

      public boolean genericLambdas
      Indicate whether to generate Java-compatible generic object interfaces for each lambda. This facilitates lambdas that are compatible with the java.util.function package.
    • usePreviewFeatures

      public boolean usePreviewFeatures
      Currently JFuncMachine uses two features that were previews between Java 17 and Java 21. If this option is true, JFuncMachine will use these features if generating for Java versions 17, 18, 19, and 20.
    • convertSwitchesToIf

      public boolean convertSwitchesToIf
      The TypeSwitch and EnumSwitch expressions use a feature that was a preview in Java 17, and became fully supported in Java 21. If this flag is true, JFuncMachine will convert these expressions to If expressions if generating for a JVM where the feature is not supported. If usePreviewFeatures is true, then this conversion will only happen for Java 16 or lower. If usePreviewFeatures is false, then this conversion will happen for Java 20 or lower.
  • Constructor Details

    • ClassGeneratorOptionsBuilder

      public ClassGeneratorOptionsBuilder()
      Create a ClassGeneratorOptionsBuilder instance with the default settings
  • Method Details

    • withJavaVersion

      public ClassGeneratorOptionsBuilder withJavaVersion(int version)
      Change the Java version in the options
      Parameters:
      version - The version of Java to generate class files for
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withLocalTailCallsToLoops

      public ClassGeneratorOptionsBuilder withLocalTailCallsToLoops(boolean option)
      Change the option to replace local recursive method calls with loops
      Parameters:
      option - True if local recursive calls should be turned into loops
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withFullTailCalls

      public ClassGeneratorOptionsBuilder withFullTailCalls(boolean option)
      Change the option to generate full tail calls for all methods (except ones marked as Java-callable)
      Parameters:
      option - True if each non-Java-callable method should be generated to do tail-call optimization
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withAutobox

      public ClassGeneratorOptionsBuilder withAutobox(boolean option)
      Change the option to generate autoboxing to automatically convert between native and box types.
      Parameters:
      option - True if autoboxing should be on
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withSharedLambdas

      public ClassGeneratorOptionsBuilder withSharedLambdas(boolean option)
      Change the option indicating whether all classes in the package should share generated lambda interfaces.
      Parameters:
      option - True if all the classes in the the package share the same lambda interfaces
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withLambdaMethodName

      public ClassGeneratorOptionsBuilder withLambdaMethodName(String lambdaMethodName)
      Change the name of the single method in a generated lambda interface
      Parameters:
      lambdaMethodName - The name to use for lambda interface methods
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withGenericLambdas

      public ClassGeneratorOptionsBuilder withGenericLambdas(boolean option)
      Change the option to generate generic lambda interfaces that use boxed types
      Parameters:
      option - True if lambda interfaces should always box native types
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withUsePreviewFeatures

      public ClassGeneratorOptionsBuilder withUsePreviewFeatures(boolean option)
      Change the option to use preview features
      Parameters:
      option - True if JFuncMachine should use JVM features that are currently in preview
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • withConvertSwitchesToIf

      public ClassGeneratorOptionsBuilder withConvertSwitchesToIf(boolean option)
      Change the option to convert TypeSwitch and EnumSwitch expressions to If expressions if the JVM doesn't support them (Java 21 is required if usePreviewFeatures is false, or Java 17 if usePreviewFeatures is true).
      Parameters:
      option - True if JFuncMachine should use preview features
      Returns:
      The current ClassGeneratorOptionsBuilder (for chaining calls)
    • build

      public ClassGeneratorOptions build()
      Generate a new ClassGeneratorOptions object with the desired changes.
      Returns:
      a new ClassGeneratorOptions containing all the options set by this builder