Package cs1302.tracer

Class CompilationHelper

java.lang.Object
cs1302.tracer.CompilationHelper

public class CompilationHelper extends Object
A collection of methods that are used to compile a Java program.

Normative References:

  • JSR-199: The Java Compiler API (package javax.tools).
  • The Java Language Specification (Java SE 21 Edition), §7 (Packages and Compilation Units).
  • Field Details

  • Method Details

    • parseMultiFileStream

      public static List<CompilationHelper.SourceFile> parseMultiFileStream(String rawInput)
      Parse a single-file or multi-file Java stream demarcated by comment delimiters.
      Parameters:
      rawInput - The raw input string from stdin or file.
      Returns:
      A list of CompilationHelper.SourceFile objects parsed from the input stream.
    • findEntryPoint

      public static CompilationHelper.SourceFile findEntryPoint(List<CompilationHelper.SourceFile> sourceFiles)
      Find the entry point source file containing the main method among multiple files.
      Parameters:
      sourceFiles - The list of source files to search.
      Returns:
      The source file containing public static void main(String[] args).
    • combineCompilationUnits

      public static com.github.javaparser.ast.CompilationUnit combineCompilationUnits(List<CompilationHelper.SourceFile> sourceFiles)
      Combine all compilation units from a multi-file stream into a single compilation unit.
      Parameters:
      sourceFiles - The list of source files.
      Returns:
      A single combined CompilationUnit.
    • findSourceRoot

      public static Optional<Path> findSourceRoot(com.github.javaparser.ast.CompilationUnit cu, Optional<Path> inputPath)
      Automatically infer the source root directory for a given compilation unit and path.
      Parameters:
      cu - The parsed compilation unit.
      inputPath - The optional path to the input source file.
      Returns:
      An Optional containing the inferred source root directory, if discovered.
    • compile

      public static CompilationHelper.CompilationResult compile(String javaSource) throws IOException
      Compile a Java program.
      Parameters:
      javaSource - The Java program to compile.
      Returns:
      The CompilationResult for this compilation.
      Throws:
      IOException - If an I/O error occurs.
      IllegalArgumentException - If the Java program failed to compile.
    • compile

      public static CompilationHelper.CompilationResult compile(String javaSource, Optional<Path> sourceRoot) throws IOException
      Compile a Java program with an optional source root for resolving dependencies.
      Parameters:
      javaSource - The Java program to compile.
      sourceRoot - The optional source root directory containing package dependencies.
      Returns:
      The CompilationResult for this compilation.
      Throws:
      IOException - If an I/O error occurs.
      IllegalArgumentException - If the Java program failed to compile.
    • detectPreviewUsage

      public static boolean detectPreviewUsage(List<com.github.javaparser.ast.CompilationUnit> compilationUnits)
      Checks whether the given compilation units use preview language or library features such as java.lang.IO or instance main methods.
      Parameters:
      compilationUnits - List of compilation units to inspect.
      Returns:
      True if preview features are detected, false otherwise.
    • detectPreviewUsage

      public static boolean detectPreviewUsage(com.github.javaparser.ast.CompilationUnit cu)
      Checks whether a single compilation unit uses preview language or library features.
      Parameters:
      cu - The compilation unit to inspect.
      Returns:
      True if preview features are detected.
    • isMainMethod

      public static boolean isMainMethod(com.github.javaparser.ast.body.MethodDeclaration m)
      Check whether a method declaration matches standard main method signature.
      Parameters:
      m - The method declaration to check.
      Returns:
      True if the method is a main method.