use of javax.tools.StandardJavaFileManager in project buck by facebook.
the class Jsr199Javac method buildWithClasspath.
@Override
public int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, Optional<Path> workingDirectory, JavacOptions.AbiGenerationMode abiGenerationMode) {
JavaCompiler compiler = createCompiler(context);
CustomZipOutputStream jarOutputStream = null;
StandardJavaFileManager fileManager = null;
JavaInMemoryFileManager inMemoryFileManager = null;
try {
fileManager = compiler.getStandardFileManager(null, null, null);
Supplier<ImmutableSet<String>> alreadyAddedFilesAvailableAfterCompilation = Suppliers.ofInstance(ImmutableSet.of());
if (context.getDirectToJarOutputSettings().isPresent()) {
Path path = context.getProjectFilesystem().getPathForRelativePath(context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath());
jarOutputStream = ZipOutputStreams.newOutputStream(path, ZipOutputStreams.HandleDuplicates.APPEND_TO_ZIP);
inMemoryFileManager = new JavaInMemoryFileManager(fileManager, path, jarOutputStream, context.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar());
alreadyAddedFilesAvailableAfterCompilation = inMemoryFileManager::getEntries;
fileManager = inMemoryFileManager;
}
Iterable<? extends JavaFileObject> compilationUnits;
try {
compilationUnits = createCompilationUnits(fileManager, context.getProjectFilesystem()::resolve, javaSourceFilePaths);
} catch (IOException e) {
LOG.warn(e, "Error building compilation units");
return 1;
}
try {
int result = buildWithClasspath(context, invokingRule, options, annotationProcessors, javaSourceFilePaths, pathToSrcsList, compiler, fileManager, compilationUnits, abiGenerationMode);
if (result != 0 || !context.getDirectToJarOutputSettings().isPresent()) {
return result;
}
return JarDirectoryStepHelper.createJarFile(context.getProjectFilesystem(), context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath(), Preconditions.checkNotNull(jarOutputStream), context.getDirectToJarOutputSettings().get().getEntriesToJar(), alreadyAddedFilesAvailableAfterCompilation.get(), context.getDirectToJarOutputSettings().get().getMainClass(), context.getDirectToJarOutputSettings().get().getManifestFile(), /* mergeManifests */
true, /* blacklist */
ImmutableSet.of(), context.getEventSink(), context.getStdErr());
} finally {
close(compilationUnits);
}
} catch (IOException e) {
LOG.warn(e, "Unable to create jarOutputStream");
} finally {
closeResources(fileManager, inMemoryFileManager, jarOutputStream);
}
return 1;
}
use of javax.tools.StandardJavaFileManager in project jdk8u_jdk by JetBrains.
the class JavaToolUtils method compileFiles.
/**
* Takes a list of files and compile these files into the working directory.
*
* @param files
* @throws IOException
*/
public static void compileFiles(List<File> files) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(files);
compiler.getTask(null, fileManager, null, null, null, compilationUnit).call();
}
}
use of javax.tools.StandardJavaFileManager in project mastering-java by Kingminghuang.
the class Compiler method main.
public static void main(String[] args) {
String source = "public class Main{" + "public static void main(String[] args){System.out.println(\"Hello World!\");}}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new StringSourceJavaObject("Main", source);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(sourceObject));
boolean result = task.call();
if (result) {
System.out.println("Compiled successfully");
}
}
use of javax.tools.StandardJavaFileManager in project logging-log4j2 by apache.
the class PluginManagerPackagesTest method compile.
static void compile(final File f) throws IOException {
// set up compiler
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
final List<String> errors = new ArrayList<>();
try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f));
// compile generated source
// (switch off annotation processing: no need to create Log4j2Plugins.dat)
final List<String> options = Arrays.asList("-proc:none");
compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();
// check we don't have any compilation errors
for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault())));
}
}
}
assertTrue(errors.toString(), errors.isEmpty());
}
use of javax.tools.StandardJavaFileManager in project jadx by skylot.
the class StaticCompiler method compile.
public static List<File> compile(List<File> files, File outDir, boolean includeDebugInfo) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
StaticFileManager staticFileManager = new StaticFileManager(fileManager, outDir);
List<String> options = new ArrayList<String>();
options.add(includeDebugInfo ? "-g" : "-g:none");
options.addAll(COMMON_ARGS);
CompilationTask task = compiler.getTask(null, staticFileManager, null, options, null, compilationUnits);
Boolean result = task.call();
fileManager.close();
if (Boolean.TRUE.equals(result)) {
return staticFileManager.outputFiles();
}
return Collections.emptyList();
}
Aggregations