Search in sources :

Example 1 with StandardJavaFileManager

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;
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) IOException(java.io.IOException)

Example 2 with StandardJavaFileManager

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();
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 3 with StandardJavaFileManager

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");
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 4 with StandardJavaFileManager

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());
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 5 with StandardJavaFileManager

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();
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)125 JavaCompiler (javax.tools.JavaCompiler)100 File (java.io.File)60 JavaFileObject (javax.tools.JavaFileObject)51 IOException (java.io.IOException)40 DiagnosticCollector (javax.tools.DiagnosticCollector)37 ArrayList (java.util.ArrayList)33 CompilationTask (javax.tools.JavaCompiler.CompilationTask)26 JavacTask (com.sun.source.util.JavacTask)17 StringWriter (java.io.StringWriter)10 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)10 JavacTool (com.sun.tools.javac.api.JavacTool)8 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)6 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)5 URL (java.net.URL)5 Diagnostic (javax.tools.Diagnostic)5 FileOutputStream (java.io.FileOutputStream)4 PrintWriter (java.io.PrintWriter)4 Charset (java.nio.charset.Charset)3 HashSet (java.util.HashSet)3