Class JavaFileWriter

java.lang.Object
ru.yojo.codegen.writer.JavaFileWriter

public class JavaFileWriter extends Object
Encapsulates file system operations for writing generated Java source files.

Handles:

  • Directory creation
  • Writing files with UTF-8 encoding
  • Dry-run mode for testing (returns content without writing)
  • Constructor Details

    • JavaFileWriter

      public JavaFileWriter()
      Creates a file writer that actually writes to disk.
    • JavaFileWriter

      public JavaFileWriter(boolean dryRun)
      Creates a file writer with optional dry-run mode.
      Parameters:
      dryRun - if true, write operations return the content without writing to disk
  • Method Details

    • writeJavaFile

      public String writeJavaFile(String outputDirectory, String packageName, String className, String content) throws IOException
      Writes the given Java source content to a file in the specified directory.

      The file name is derived from the class name (appended with ".java"). Directories are created if they don't exist.

      Parameters:
      outputDirectory - the base directory for output
      packageName - the package name (used to create subdirectories)
      className - the class name (becomes file name)
      content - the Java source code to write
      Returns:
      the path to the written file (or would-be path in dry-run mode)
      Throws:
      IOException - if directory creation or file writing fails
    • createDirectories

      public void createDirectories(String path) throws IOException
      Creates directories for the given path.
      Parameters:
      path - the directory path to create
      Throws:
      IOException - if creation fails
    • isDryRun

      public boolean isDryRun()
      Checks if dry-run mode is enabled.
      Returns:
      true if in dry-run mode