This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JavaCompilerUtility and deprecate JavaCompilerUtil
- Loading branch information
Showing
4 changed files
with
359 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
test-utils-core/src/main/java/uk/gov/justice/services/test/utils/core/compiler/Compiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package uk.gov.justice.services.test.utils.core.compiler; | ||
|
||
import static java.text.MessageFormat.format; | ||
import static java.util.Arrays.stream; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.stream.Stream; | ||
|
||
import javax.tools.Diagnostic; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
import javax.tools.ToolProvider; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Compiler { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(Compiler.class); | ||
|
||
private final JavaCompiler javaCompiler; | ||
|
||
public Compiler() { | ||
this.javaCompiler = ToolProvider.getSystemJavaCompiler(); | ||
} | ||
|
||
public void compile(final File codegenOutputDir, final File compilationOutputDir) { | ||
|
||
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); | ||
|
||
try (final StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnostics, Locale.getDefault(), null)) { | ||
|
||
final List<JavaFileObject> javaObjects = scanForJavaObjects(codegenOutputDir, fileManager); | ||
|
||
if (javaObjects.isEmpty()) { | ||
throw new CompilationException( | ||
format("There are no source files to compile in {0}", codegenOutputDir.getAbsolutePath())); | ||
} | ||
|
||
final String[] compileOptions = new String[]{"-d", compilationOutputDir.getAbsolutePath()}; | ||
final Iterable<String> compilationOptions = Arrays.asList(compileOptions); | ||
|
||
final JavaCompiler.CompilationTask compilerTask = javaCompiler.getTask(null, fileManager, diagnostics, compilationOptions, null, | ||
javaObjects); | ||
|
||
if (!compilerTask.call()) { | ||
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) { | ||
LOGGER.error("Error on line {} in {}", diagnostic.getLineNumber(), diagnostic); | ||
} | ||
throw new CompilationException("Could not compile project"); | ||
} | ||
|
||
} catch (final IOException e) { | ||
throw new CompilationException("Could not compile project", e); | ||
} | ||
} | ||
|
||
private List<JavaFileObject> scanForJavaObjects(final File dir, final StandardJavaFileManager fileManager) { | ||
return scanRecursivelyForJavaObjects(dir, fileManager).collect(toList()); | ||
} | ||
|
||
private Stream<JavaFileObject> scanRecursivelyForJavaObjects(final File dir, final StandardJavaFileManager fileManager) { | ||
|
||
final File[] fileList = dir.listFiles(); | ||
|
||
if (null == fileList) { | ||
return Stream.empty(); | ||
} | ||
|
||
return stream(fileList) | ||
.flatMap(file -> processFileForJavaObjects(fileManager, file)); | ||
|
||
} | ||
|
||
private Stream<? extends JavaFileObject> processFileForJavaObjects(final StandardJavaFileManager fileManager, final File file) { | ||
if (file.isDirectory()) { | ||
return scanRecursivelyForJavaObjects(file, fileManager); | ||
} else if (file.isFile() && file.getName().toLowerCase().endsWith(".java")) { | ||
|
||
try { | ||
FileUtils.readFileToString(file); | ||
} catch (final IOException e) { | ||
LOGGER.warn(format("Could not read file : %s", file.getAbsolutePath()), e); | ||
} | ||
|
||
return Stream.of(readJavaObject(file, fileManager)); | ||
} | ||
|
||
return Stream.empty(); | ||
} | ||
|
||
private JavaFileObject readJavaObject(final File file, final StandardJavaFileManager fileManager) { | ||
final Iterator<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(file).iterator(); | ||
|
||
if (javaFileObjects.hasNext()) { | ||
return javaFileObjects.next(); | ||
} | ||
|
||
throw new CompilationException(format("Could not load {0} java file object", file.getAbsolutePath())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.