Package cs1302.tracer
Class CompilationHelper
java.lang.Object
cs1302.tracer.CompilationHelper
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA collection of information from the successful compilation of a Java program.static final recordRepresents an individual source file from a single-file or multi-file input stream. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic com.github.javaparser.ast.CompilationUnitcombineCompilationUnits(List<CompilationHelper.SourceFile> sourceFiles) Combine all compilation units from a multi-file stream into a single compilation unit.Compile a Java program.Compile a Java program with an optional source root for resolving dependencies.static booleandetectPreviewUsage(com.github.javaparser.ast.CompilationUnit cu) Checks whether a single compilation unit uses preview language or library features.static booleandetectPreviewUsage(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.static CompilationHelper.SourceFilefindEntryPoint(List<CompilationHelper.SourceFile> sourceFiles) Find the entry point source file containing the main method among multiple files.findSourceRoot(com.github.javaparser.ast.CompilationUnit cu, Optional<Path> inputPath) Automatically infer the source root directory for a given compilation unit and path.static booleanisMainMethod(com.github.javaparser.ast.body.MethodDeclaration m) Check whether a method declaration matches standard main method signature.static List<CompilationHelper.SourceFile> parseMultiFileStream(String rawInput) Parse a single-file or multi-file Java stream demarcated by comment delimiters.
-
Field Details
-
DELIMITER_PATTERN
Deprecated.UseSourceDelimiters.scan(CharSequence)for delimiter offsets and paths.Compatibility matcher for source delimiters, with nonbacktracking whitespace and markers.
-
-
Method Details
-
parseMultiFileStream
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.SourceFileobjects 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
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.
-
SourceDelimiters.scan(CharSequence)for delimiter offsets and paths.