Search in sources :

Example 56 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.

the class TestSuperclass method main.

public static void main(String... args) throws Exception {
    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    int errors = 0;
    for (ClassKind ck : ClassKind.values()) {
        for (GenericKind gk : GenericKind.values()) {
            for (SuperKind sk : SuperKind.values()) {
                errors += new TestSuperclass(ck, gk, sk).run(comp, fm);
            }
        }
    }
    if (errors > 0)
        throw new Exception(errors + " errors found");
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) IOException(java.io.IOException)

Example 57 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project zoj by licheng.

the class CustomJavaCompiler method main.

public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Invalid arg length " + args.length);
        System.exit(-1);
    }
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
    JavaFileManager fileManager = new CustomJavaFileManager(stdFileManager);
    System.out.println(new File(args[0]).getAbsolutePath());
    System.out.println(compiler.getTask(null, fileManager, null, null, null, stdFileManager.getJavaFileObjects(args[0])).call());
}
Also used : ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavaFileManager(javax.tools.JavaFileManager) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File)

Example 58 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project gradle by gradle.

the class SimpleGeneratedJavaClassCompiler method compile.

/**
 * Compiles generated Java source files.
 *
 * @param srcDir where the compiler will output the sources
 * @param dstDir where the compiler will output the class files
 * @param classes the classes to compile
 * @param classPath the classpath to use for compilation
 */
public static void compile(File srcDir, File dstDir, List<ClassSource> classes, ClassPath classPath) throws GeneratedClassCompilationException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new GeneratedClassCompilationException("No Java compiler found, please ensure you are running Gradle with a JDK");
    }
    DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
    try (StandardJavaFileManager mgr = compiler.getStandardFileManager(ds, null, null)) {
        List<String> options = buildOptions(dstDir, classPath);
        List<File> filesToCompile = outputSourceFilesToSourceDir(srcDir, classes);
        if (dstDir.exists() || dstDir.mkdirs()) {
            Iterable<? extends JavaFileObject> sources = mgr.getJavaFileObjectsFromFiles(filesToCompile);
            JavaCompiler.CompilationTask task = compiler.getTask(null, mgr, ds, options, null, sources);
            task.call();
        } else {
            throw new GeneratedClassCompilationException("Unable to create output classes directory");
        }
    } catch (IOException e) {
        throw new GeneratedClassCompilationException("Unable to compile generated classes", e);
    }
    List<Diagnostic<? extends JavaFileObject>> diagnostics = ds.getDiagnostics().stream().filter(d -> d.getKind() == Diagnostic.Kind.ERROR).collect(Collectors.toList());
    if (!diagnostics.isEmpty()) {
        throwCompilationError(diagnostics);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Files(java.nio.file.Files) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) List(java.util.List) Lists(com.google.common.collect.Lists) Diagnostic(javax.tools.Diagnostic) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) ClassPath(org.gradle.internal.classpath.ClassPath) DiagnosticCollector(javax.tools.DiagnosticCollector) ToolProvider(javax.tools.ToolProvider) JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 59 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project gradle by gradle.

the class JdkJavaCompiler method createCompileTask.

private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec, ApiCompilerResult result) {
    List<String> options = new JavaCompilerArgumentsBuilder(spec).build();
    JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create();
    MinimalJavaCompileOptions compileOptions = spec.getCompileOptions();
    Charset charset = compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null;
    StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, charset);
    Iterable<? extends JavaFileObject> compilationUnits = standardFileManager.getJavaFileObjectsFromFiles(spec.getSourceFiles());
    boolean hasEmptySourcepaths = JavaVersion.current().isJava9Compatible() && emptySourcepathIn(options);
    JavaFileManager fileManager = GradleStandardJavaFileManager.wrap(standardFileManager, DefaultClassPath.of(spec.getAnnotationProcessorPath()), hasEmptySourcepaths);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, spec.getClasses(), compilationUnits);
    if (compiler instanceof IncrementalCompilationAwareJavaCompiler) {
        task = ((IncrementalCompilationAwareJavaCompiler) compiler).makeIncremental(task, result.getSourceClassesMapping(), result.getConstantsAnalysisResult(), new CompilationSourceDirs(spec));
    }
    Set<AnnotationProcessorDeclaration> annotationProcessors = spec.getEffectiveAnnotationProcessors();
    task = new AnnotationProcessingCompileTask(task, annotationProcessors, spec.getAnnotationProcessorPath(), result.getAnnotationProcessingResult());
    task = new ResourceCleaningCompilationTask(task, fileManager);
    return task;
}
Also used : JavaFileManager(javax.tools.JavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) GradleStandardJavaFileManager(org.gradle.api.internal.tasks.compile.reflect.GradleStandardJavaFileManager) JavaCompiler(javax.tools.JavaCompiler) Charset(java.nio.charset.Charset) AnnotationProcessorDeclaration(org.gradle.api.internal.tasks.compile.processing.AnnotationProcessorDeclaration) StandardJavaFileManager(javax.tools.StandardJavaFileManager) GradleStandardJavaFileManager(org.gradle.api.internal.tasks.compile.reflect.GradleStandardJavaFileManager)

Example 60 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project bazel by bazelbuild.

the class VanillaJavaBuilder method run.

public VanillaJavaBuilderResult run(List<String> args) throws IOException {
    OptionsParser optionsParser;
    try {
        optionsParser = new OptionsParser(args);
    } catch (InvalidCommandLineException e) {
        return new VanillaJavaBuilderResult(false, e.getMessage());
    }
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    StringWriter output = new StringWriter();
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnosticCollector, ENGLISH, UTF_8);
    setLocations(optionsParser, fileManager);
    ImmutableList<JavaFileObject> sources = getSources(optionsParser, fileManager);
    boolean ok;
    if (sources.isEmpty()) {
        ok = true;
    } else {
        CompilationTask task = javaCompiler.getTask(new PrintWriter(output, true), fileManager, diagnosticCollector, JavacOptions.removeBazelSpecificFlags(optionsParser.getJavacOpts()), ImmutableList.<String>of(), /*classes*/
        sources);
        setProcessors(optionsParser, fileManager, task);
        ok = task.call();
    }
    if (ok) {
        writeOutput(optionsParser);
    }
    writeGeneratedSourceOutput(optionsParser);
    // the file to be created
    if (optionsParser.getOutputDepsProtoFile() != null) {
        try (OutputStream os = Files.newOutputStream(Paths.get(optionsParser.getOutputDepsProtoFile()))) {
            Deps.Dependencies.newBuilder().setRuleLabel(optionsParser.getTargetLabel()).setSuccess(ok).build().writeTo(os);
        }
    }
    // TODO(cushon): support manifest protos & genjar
    if (optionsParser.getManifestProtoPath() != null) {
        try (OutputStream os = Files.newOutputStream(Paths.get(optionsParser.getManifestProtoPath()))) {
            Manifest.getDefaultInstance().writeTo(os);
        }
    }
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
        StringBuilder message = new StringBuilder();
        if (diagnostic.getSource() != null) {
            message.append(diagnostic.getSource().getName());
            if (diagnostic.getLineNumber() != -1) {
                message.append(':').append(diagnostic.getLineNumber());
            }
            message.append(": ");
        }
        message.append(diagnostic.getKind().toString().toLowerCase(ENGLISH));
        message.append(": ").append(diagnostic.getMessage(ENGLISH)).append(System.lineSeparator());
        output.write(message.toString());
    }
    return new VanillaJavaBuilderResult(ok, output.toString());
}
Also used : OutputStream(java.io.OutputStream) JavaCompiler(javax.tools.JavaCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) PrintWriter(java.io.PrintWriter)

Aggregations

StandardJavaFileManager (javax.tools.StandardJavaFileManager)124 JavaCompiler (javax.tools.JavaCompiler)99 File (java.io.File)60 JavaFileObject (javax.tools.JavaFileObject)50 IOException (java.io.IOException)37 DiagnosticCollector (javax.tools.DiagnosticCollector)36 ArrayList (java.util.ArrayList)33 CompilationTask (javax.tools.JavaCompiler.CompilationTask)25 JavacTask (com.sun.source.util.JavacTask)17 StringWriter (java.io.StringWriter)10 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)9 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 FileOutputStream (java.io.FileOutputStream)4 PrintWriter (java.io.PrintWriter)4 Diagnostic (javax.tools.Diagnostic)4 Charset (java.nio.charset.Charset)3 HashSet (java.util.HashSet)3