Search in sources :

Example 21 with StandardJavaFileManager

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

the class GenericConstructorAndDiamondTest method main.

public static void main(String... args) throws Exception {
    //create default shared JavaCompiler - reused across multiple compilations
    JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
    for (BoundKind boundKind : BoundKind.values()) {
        for (ConstructorKind constructorKind : ConstructorKind.values()) {
            for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
                for (TypeArgArity arity : TypeArgArity.values()) {
                    for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
                        for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
                            for (ArgumentKind argKind : ArgumentKind.values()) {
                                new GenericConstructorAndDiamondTest(boundKind, constructorKind, declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 22 with StandardJavaFileManager

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

the class TestJavacTask method getTask.

static JavacTaskImpl getTask(JavaCompiler compiler, File... file) {
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
    return (JavacTaskImpl) compiler.getTask(null, fm, null, null, null, files);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) StandardJavaFileManager(javax.tools.StandardJavaFileManager)

Example 23 with StandardJavaFileManager

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

the class T6993305 method run.

void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    Iterable<? extends CompilationUnitTree> cus = task.parse();
    TestScanner s = new TestScanner();
    s.scan(cus, task);
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) IOException(java.io.IOException)

Example 24 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 25 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)57 JavaCompiler (javax.tools.JavaCompiler)42 JavaFileObject (javax.tools.JavaFileObject)24 File (java.io.File)23 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)17 DiagnosticCollector (javax.tools.DiagnosticCollector)15 CompilationTask (javax.tools.JavaCompiler.CompilationTask)9 JavacTask (com.sun.source.util.JavacTask)7 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 JavacTool (com.sun.tools.javac.api.JavacTool)4 FileOutputStream (java.io.FileOutputStream)4 StringWriter (java.io.StringWriter)3 ZipFile (java.util.zip.ZipFile)3 Test (org.junit.Test)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 PrintWriter (java.io.PrintWriter)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2