Search in sources :

Example 86 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class MainTest method testCeylonModule.

@Test
public void testCeylonModule() throws IOException, ModuleNotFoundException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    File moduleFile = new File("test/foo/foo", "$module_.java");
    Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjects(moduleFile);
    File destDir = new File("build/mainTest");
    FileUtil.delete(destDir);
    destDir.mkdirs();
    CompilationTask task = compiler.getTask(null, null, null, Arrays.asList("-d", destDir.getPath(), "-cp", "build/classes" + File.pathSeparator + LANGUAGE_MODULE_CAR + File.pathSeparator + MODEL_MODULE_CAR), null, units);
    Boolean result = task.call();
    assertTrue(result != null && result.booleanValue());
    File compiledModuleFile = new File(destDir, "foo/foo/$module_.class");
    assertTrue(compiledModuleFile.isFile());
    File jar = jar(compiledModuleFile, "foo/foo");
    try {
        checkJarDependencies(jar);
    } finally {
        jar.delete();
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Test(org.junit.Test)

Example 87 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

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 88 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class T6956638 method test.

void test(File... sourceFiles) throws Exception {
    System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
    File classesDir = new File("classes" + count);
    classesDir.mkdirs();
    StringWriter compilerOutputStream = new StringWriter();
    List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
    Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
    System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
    JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
    JavacTask javacTask = (JavacTask) task;
    Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
    Iterable<? extends Element> analyzedTrees = javacTask.analyze();
    Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
    System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
    System.err.print("3-");
    for (JavaFileObject f : generatedFiles) System.err.print(" " + f);
    System.err.println("");
    System.err.print("5-");
    for (File f : classesDir.listFiles()) System.err.print(" " + f);
    System.err.println("");
    System.err.println("----");
    System.err.println(compilerOutputStream.toString());
    if (size(generatedFiles) != size(parsedTrees)) {
        throw new Exception("wrong number of files generated: " + size(generatedFiles) + " expected: " + size(parsedTrees));
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Example 89 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class T6345974 method main.

public static void main(String[] args) throws Exception {
    PrintWriter out = new PrintWriter(System.out, true);
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File testSrc = new File(System.getProperty("test.src"));
    Iterable<? extends JavaFileObject> f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
    JavacTask task = tool.getTask(out, fm, null, null, null, f);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    out.flush();
    Scanner s = new Scanner();
    for (CompilationUnitTree t : trees) s.scan(t, null);
}
Also used : TreeScanner(com.sun.source.util.TreeScanner) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 90 with StandardJavaFileManager

use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.

the class Foo method main.

public static void main(String... args) throws IOException {
    String testSrc = System.getProperty("test.src", ".");
    String testClasses = System.getProperty("test.classes", ".");
    JavacTool tool = JavacTool.create();
    MyDiagListener dl = new MyDiagListener();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName() + ".java")));
    JavacTask task = tool.getTask(null, fm, dl, null, null, files);
    task.parse();
    task.analyze();
    task.generate();
    if (dl.notes != 2)
        throw new AssertionError(dl.notes + " notes given");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

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