Uploaded image for project: 'mdsal'
  1. mdsal
  2. MDSAL-562 Eliminate use of Xtend in mdsal-binding-java-api-generator
  3. MDSAL-734

Add utility classes for generating indented file content

XMLWordPrintable

    • Icon: Sub-task Sub-task
    • Resolution: Unresolved
    • Icon: Medium Medium
    • 14.0.0
    • None
    • Binding codegen
    • None

      In order to migrate from Xtend we need to replace StringConcatenation with a sharp tool and use that to emit formatted blocks.
      We do not need to handle all the complexity of StringConcatenation, as we are always generating files top-down, appending intermediate code blocks, always in a linear fashion.

      Since we have modern Java, we can use try-with-resources to give us the basic building block for embedding. The code flow should look something like:

       

      var builder = new ContentBuilder(4)
          .addLine("public class Foo {");
      
      try (var block = builder.openBlock()) {
          for (var field : fields) {
              generateFieldDeclaration(block, field);
          }
      
          builder.ensureEmptyLine();
      
          for (var ctor : constructors) {
              generateConstructor(block, ctor);
          }
      }
      
      String content = builder.addLine("}").build();
      

      Lines can be appended in whole, or built from components which do not contain a newline (we should be able to guarantee that 95% of time and we can split the remaining 5%). It remains to be seen what Text Blocks bring in terms of value and performance characteristics. ContentBuilder can have at most one block open and that block has to be closed by the time build() is called. Blocks can nested blocks created in much the same way.

      Note that the 'AutoCloseable' interface exposed from openBlock() needs to be separate from the interface passed to 'generateFieldDeclaration', so that it is not possible to close it by mistake.

            Unassigned Unassigned
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: