Search in sources :

Example 16 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class CeylonDocToolTests method compileJavaModule.

private void compileJavaModule(String pathname, String... fileNames) throws Exception {
    CeyloncTool compiler = new CeyloncTool();
    List<String> options = Arrays.asList("-src", pathname, "-out", "build/ceylon-cars", "-cp", CompilerTests.getClassPathAsPath());
    JavacFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    List<String> qualifiedNames = new ArrayList<String>(fileNames.length);
    for (String name : fileNames) {
        qualifiedNames.add(pathname + File.separator + name);
    }
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromStrings(qualifiedNames);
    JavacTask task = compiler.getTask(null, null, null, options, null, fileObjects);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) CeyloncTool(com.redhat.ceylon.compiler.java.tools.CeyloncTool) ArrayList(java.util.ArrayList) JavacTask(com.sun.source.util.JavacTask)

Example 17 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class T7021650 method run.

/**
     * Perform a compilation with custom factories registered in the context,
     * and verify that corresponding objects are created in each round.
     */
void run() throws Exception {
    Counter demoCounter = new Counter();
    Counter myAttrCounter = new Counter();
    Context context = new Context();
    // Use a custom file manager which creates classloaders for annotation
    // processors with a sensible delegation parent, so that all instances
    // of test classes come from the same class loader. This is important
    // because the test performs class checks on the instances of classes
    // found in the context for each round or processing.
    context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {

        public JavaFileManager make(Context c) {
            return new JavacFileManager(c, true, null) {

                @Override
                protected ClassLoader getClassLoader(URL[] urls) {
                    return new URLClassLoader(urls, T7021650.class.getClassLoader());
                }
            };
        }
    });
    Demo.preRegister(context, demoCounter);
    MyAttr.preRegister(context, myAttrCounter);
    String[] args = { "-d", ".", "-processor", T7021650.class.getName(), "-XprintRounds", new File(testSrc, T7021650.class.getName() + ".java").getPath() };
    compile(context, args);
    // Expect to create Demo for initial round, then MAX_ROUNDS in which
    // GenX files are generated, then standard final round of processing.
    checkEqual("demoCounter", demoCounter.count, MAX_ROUNDS + 2);
    // Expect to create MyAttr for same processing rounds as for Demo,
    // plus additional context for final compilation.
    checkEqual("myAttrCounter", myAttrCounter.count, MAX_ROUNDS + 3);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager)

Example 18 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class LanguageCompiler method findModuleDescriptorForFile.

private JavaFileObject findModuleDescriptorForFile(File file) {
    JavacFileManager dfm = (JavacFileManager) fileManager;
    File dir = file.getParentFile();
    while (dir != null) {
        try {
            String name = dir.getPath() + "/module";
            JavaFileObject fo = dfm.getJavaFileForInput(StandardLocation.SOURCE_PATH, name, Kind.SOURCE);
            if (fo != null) {
                return fo;
            }
        } catch (IOException e) {
        // Ignore
        }
        dir = dir.getParentFile();
    }
    return null;
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavaFileObject(javax.tools.JavaFileObject) IOException(java.io.IOException) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 19 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class LanguageCompiler method isResource.

private boolean isResource(JavaFileObject fo) {
    // make sure we get a proper normalized abslute path
    String fileName = FileUtil.absoluteFile(new File(fo.toUri().getPath())).getPath();
    // now see if it's in any of the resource paths
    JavacFileManager dfm = (JavacFileManager) fileManager;
    for (File dir : dfm.getLocation(CeylonLocation.RESOURCE_PATH)) {
        String prefix = FileUtil.absoluteFile(dir).getPath();
        if (fileName.startsWith(prefix)) {
            return true;
        }
    }
    return false;
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 20 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project ceylon-compiler by ceylon.

the class LanguageCompiler method addModuleDescriptors.

// This is a bit of a hack, but if we got passed a list of resources
// without any accompaning source files we'll not be able to determine
// the module to which the resource files belong. So to try to fix that
// we see if a module file exists in the source folders and add it to
// the list of source files
private List<JavaFileObject> addModuleDescriptors(List<JavaFileObject> sourceFiles, List<JavaFileObject> resourceFiles) {
    List<JavaFileObject> result = sourceFiles;
    JavacFileManager dfm = (JavacFileManager) fileManager;
    for (JavaFileObject fo : resourceFiles) {
        String resName = JarUtils.toPlatformIndependentPath(dfm.getLocation(CeylonLocation.RESOURCE_PATH), fo.getName());
        JavaFileObject moduleFile = findModuleDescriptorForFile(new File(resName));
        if (moduleFile != null && !result.contains(moduleFile)) {
            result = result.append(moduleFile);
        }
    }
    return result;
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavaFileObject(javax.tools.JavaFileObject) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Aggregations

JavacFileManager (com.sun.tools.javac.file.JavacFileManager)35 Context (com.sun.tools.javac.util.Context)19 JavaFileObject (javax.tools.JavaFileObject)10 File (java.io.File)9 IOException (java.io.IOException)7 PrintWriter (java.io.PrintWriter)7 JavaFileManager (javax.tools.JavaFileManager)7 VirtualFile (com.redhat.ceylon.compiler.typechecker.io.VirtualFile)5 Path (java.nio.file.Path)5 JavacTask (com.sun.source.util.JavacTask)4 Options (com.sun.tools.javac.util.Options)4 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedWriter (java.io.BufferedWriter)3 Deps (com.google.devtools.build.lib.view.proto.Deps)2 JavacTool (com.sun.tools.javac.api.JavacTool)2 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)2 AnnotationProcessingError (com.sun.tools.javac.processing.AnnotationProcessingError)2 PropagatedException (com.sun.tools.javac.util.PropagatedException)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 Test (org.junit.Test)2