Package randoop.compile
Class SequenceCompiler
- java.lang.Object
-
- randoop.compile.SequenceCompiler
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class SequenceCompiler extends Object implements Closeable
Compiles a Java class given as aString
.A simplified version of the
javaxtools.compiler.CharSequenceCompiler
from Create dynamic applications with javax.tools.
-
-
Field Summary
Fields Modifier and Type Field Description private JavaCompiler
compiler
The Java compiler.private List<String>
compilerOptions
The options to the compiler.private static String
debugCompilationFailure
If non-null, do verbose output for compilation failures where the Java source code contains the string.private JavaFileManager
fileManager
TheFileManager
for this compiler.
-
Constructor Summary
Constructors Constructor Description SequenceCompiler()
Creates aSequenceCompiler
.SequenceCompiler(List<String> compilerOptions)
Creates aSequenceCompiler
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Releases any system resources associated with this.private void
compile(String packageName, String classname, String javaSource)
Compiles the given class.private boolean
compile(String packageName, String classname, String javaSource, DiagnosticCollector<JavaFileObject> diagnostics)
A helper method for thecompile(String, String, String)
andisCompilable(String, String, String)
methods: compiles the given class using the given diagnostics collector.Class<?>
compileAndLoad(@DotSeparatedIdentifiers String packageName, @BinaryNameWithoutPackage String classname, String javaSource)
Compiles the given class, leads it, and returns the Class object.(package private) @BinaryName String
fullyQualifiedName(@DotSeparatedIdentifiers String packageName, @BinaryNameWithoutPackage String classname)
Constructs a fully-qualified class name from the given package and unqualified class name.boolean
isCompilable(String packageName, String classname, String javaSource)
Indicates whether the given class is compilable.private static Class<?>
loadClassFile(File directory, @BinaryName String className)
Given a .class file, returns the corresponding Class object.
-
-
-
Field Detail
-
debugCompilationFailure
private static final String debugCompilationFailure
If non-null, do verbose output for compilation failures where the Java source code contains the string.
-
compiler
private final JavaCompiler compiler
The Java compiler.
-
fileManager
private final JavaFileManager fileManager
TheFileManager
for this compiler.
-
-
Constructor Detail
-
SequenceCompiler
public SequenceCompiler()
Creates aSequenceCompiler
.
-
SequenceCompiler
public SequenceCompiler(List<String> compilerOptions)
Creates aSequenceCompiler
.- Parameters:
compilerOptions
- the compiler options
-
-
Method Detail
-
close
public void close() throws IOException
Releases any system resources associated with this.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
isCompilable
public boolean isCompilable(String packageName, String classname, String javaSource)
Indicates whether the given class is compilable.- Parameters:
packageName
- the package name for the class, null if default packageclassname
- the simple name of the classjavaSource
- the source text of the class- Returns:
- true if class source was successfully compiled, false otherwise
-
compile
private void compile(String packageName, String classname, String javaSource) throws SequenceCompilerException
Compiles the given class. If this method returns normally, compilation was successful.- Parameters:
packageName
- the package of the class, null if default packageclassname
- the simple name of the classjavaSource
- the source text of the class- Throws:
SequenceCompilerException
- if the compilation fails
-
compile
private boolean compile(String packageName, String classname, String javaSource, DiagnosticCollector<JavaFileObject> diagnostics)
A helper method for thecompile(String, String, String)
andisCompilable(String, String, String)
methods: compiles the given class using the given diagnostics collector.- Parameters:
packageName
- the package of the class, null if default packageclassname
- the simple name of the classjavaSource
- the source text of the classdiagnostics
- theDiagnosticsCollector
object to use for the compilation. Always use a new diagnostics collector each compilation to avoid accumulating errors.- Returns:
- true if the class source is successfully compiled, false otherwise
-
compileAndLoad
public Class<?> compileAndLoad(@DotSeparatedIdentifiers String packageName, @BinaryNameWithoutPackage String classname, String javaSource) throws SequenceCompilerException
Compiles the given class, leads it, and returns the Class object. If this method returns normally, compilation was successful.- Parameters:
packageName
- the package of the class, null if default packageclassname
- the simple name of the classjavaSource
- the source text of the class- Returns:
- the loaded Class object
- Throws:
SequenceCompilerException
- if the compilation fails
-
loadClassFile
private static Class<?> loadClassFile(File directory, @BinaryName String className)
Given a .class file, returns the corresponding Class object.- Parameters:
directory
- the directory containing the .class file (possibly in a package-named subdirectory)className
- the binary name of the class defined in the file- Returns:
- the loaded Class object
-
fullyQualifiedName
@BinaryName String fullyQualifiedName(@DotSeparatedIdentifiers String packageName, @BinaryNameWithoutPackage String classname)
Constructs a fully-qualified class name from the given package and unqualified class name.- Parameters:
packageName
- the package of the class, null if default packageclassname
- the name of the class, without the package- Returns:
- the fully-qualified class name constructed from the arguments
-
-