Class CodeWriter

java.lang.Object
net.codecrete.windowsapi.writer.CodeWriter

public class CodeWriter extends Object
Generates Java code for a given scope of types, functions, and constants.
  • Field Details

    • generationContext

      protected final net.codecrete.windowsapi.writer.GenerationContext generationContext
      Shared generation context.
    • voidType

      protected final Primitive voidType
      Void type.
    • writer

      protected PrintWriter writer
      Print writer for the current Java file
    • namespace

      protected Namespace namespace
      Namespace of the current Java file
    • type

      protected Type type
      The metadata type for the current Java file (if any)
    • packageName

      protected String packageName
      The Java package name of the current Java file
    • className

      protected String className
      The Java class name of the current Java file
  • Constructor Details

    • CodeWriter

      public CodeWriter(Metadata metadata, Path outputDirectory, EventListener eventListener)
      Creates a new instance.
      Parameters:
      metadata - the metadata
      outputDirectory - the output directory
      eventListener - the event listener to notify about events
  • Method Details

    • getGeneratedFiles

      public Set<Path> getGeneratedFiles()
      Gets the generated files (set of Path instances).

      The path is relative to the output directory.

      Returns:
      generated files, or null for dry runs
    • setBasePackage

      public void setBasePackage(String basePackage)
      Sets the base package.

      The base package is prepended to all package names derived from Microsoft's namespace. The default is an empty string, i.e., no further package name is added.

      Parameters:
      basePackage - the base package name
    • setDryRun

      public void setDryRun(boolean isDryRun)
      Sets if this code writer should execute a dry run without creating files and directories.

      Initially, it is set to false.

      Parameters:
      isDryRun - true for dry run, false for real run
    • write

      public void write(Scope scope)
      Writes the Java code for the specified scope of types, functions, and constants.
      Parameters:
      scope - the scope
    • writeAll

      public void writeAll()
      Writes the Java code for all types, functions, and constants.

      This method is used for tests.

    • generationContext

      protected net.codecrete.windowsapi.writer.GenerationContext generationContext()
      Gets the code generation context.
      Returns:
      the generation context
    • withFile

      protected void withFile(Namespace namespace, Type type, String className, Runnable action)
      Creates a new Java source file for the specified package and class name and executes the action.

      While the action is executed, the namespace, type, package name, class name and the print writer are available through instance variables.

      If the action is successful, the event listener is notified that a new file has been created.

      Parameters:
      namespace - the namespace
      type - the metadata type that is the basis for the Java file (or null).
      className - the Java class name
      action - the action to execute
    • consumeBitFieldNumber

      protected int consumeBitFieldNumber()
      Returns the next bit field number and increases it.
      Returns:
      the bit field number
    • toJavaPackageName

      protected String toJavaPackageName(String namespace)
      Converts the namespace name to a Java package name.
      Parameters:
      namespace - the namespace
      Returns:
      the Java package name
    • writeCommentWithNotes

      protected void writeCommentWithNotes(String comment, String... notes)
      Writes a comment with notes.

      Each note is added in a separate paragraph. If a note is null, it is ignored.

      Parameters:
      comment - the comment
      notes - the notes
    • writeDocumentationUrl

      protected void writeDocumentationUrl(Type type)
      Writes the type's documentation URL to the top level comment (if available).
      Parameters:
      type - the type