Search in sources :

Example 26 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project bazel by bazelbuild.

the class TreePrunerTest method parseLines.

JCCompilationUnit parseLines(String... lines) {
    Context context = new Context();
    try (JavacFileManager fm = new JavacFileManager(context, true, UTF_8)) {
        ParserFactory parserFactory = ParserFactory.instance(context);
        String input = Joiner.on('\n').join(lines);
        JavacParser parser = parserFactory.newParser(input, /*keepDocComments=*/
        false, /*keepEndPos=*/
        false, /*keepLineMap=*/
        false);
        return parser.parseCompilationUnit();
    } catch (IOException e) {
        throw new IOError(e);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavacParser(com.sun.tools.javac.parser.JavacParser) IOError(java.io.IOError) ParserFactory(com.sun.tools.javac.parser.ParserFactory) IOException(java.io.IOException)

Example 27 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project bazel by bazelbuild.

the class JavacTurbineTest method compileLib.

private void compileLib(Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException {
    final Path outdir = temp.newFolder().toPath();
    JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
    fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
    fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
    List<String> options = Arrays.asList("-d", outdir.toString());
    JavacTool tool = JavacTool.create();
    JavacTask task = tool.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units);
    assertThat(task.call()).isTrue();
    try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
        Files.walkFileTree(outdir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                JarEntry je = new JarEntry(outdir.relativize(path).toString());
                jos.putNextEntry(je);
                Files.copy(path, jos);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
Also used : Path(java.nio.file.Path) Context(com.sun.tools.javac.util.Context) JavacTool(com.sun.tools.javac.api.JavacTool) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedWriter(java.io.BufferedWriter) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) OutputStreamWriter(java.io.OutputStreamWriter) JavacTask(com.sun.source.util.JavacTask) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PrintWriter(java.io.PrintWriter)

Example 28 with JavacFileManager

use of com.sun.tools.javac.file.JavacFileManager in project bazel by bazelbuild.

the class JavacTurbineTest method reducedClasspathFallback.

@Test
public void reducedClasspathFallback() throws Exception {
    Path libD = temp.newFile("libd.jar").toPath();
    compileLib(libD, Collections.<Path>emptyList(), Arrays.asList(new StringJavaFileObject("D.java", "public class D { static final int CONST = 42; }")));
    Path libC = temp.newFile("libc.jar").toPath();
    compileLib(libC, Collections.singleton(libD), Arrays.asList(new StringJavaFileObject("C.java", "class C extends D {}")));
    Path libB = temp.newFile("libb.jar").toPath();
    compileLib(libB, Arrays.asList(libC, libD), Arrays.asList(new StringJavaFileObject("B.java", "class B extends C {}")));
    Path libA = temp.newFile("liba.jar").toPath();
    compileLib(libA, Arrays.asList(libB, libC, libD), Arrays.asList(new StringJavaFileObject("A.java", "class A extends B {}")));
    Path depsA = writedeps("liba.jdeps", Deps.Dependencies.newBuilder().setSuccess(true).setRuleLabel("//lib:a").addDependency(Deps.Dependency.newBuilder().setPath(libB.toString()).setKind(Deps.Dependency.Kind.EXPLICIT)).build());
    optionsBuilder.addClassPathEntries(ImmutableList.of(libA.toString(), libB.toString(), libC.toString(), libD.toString()));
    optionsBuilder.addAllDepsArtifacts(ImmutableList.of(depsA.toString()));
    optionsBuilder.addDirectJarToTarget(libA.toString(), "//lib:a");
    optionsBuilder.addIndirectJarToTarget(libB.toString(), "//lib:b");
    optionsBuilder.addIndirectJarToTarget(libC.toString(), "//lib:c");
    optionsBuilder.addIndirectJarToTarget(libD.toString(), "//lib:d");
    optionsBuilder.setTargetLabel("//my:target");
    addSourceLines("Hello.java", "class Hello {", "  public static final int CONST = A.CONST;", "  public static void main(String[] args) {}", "}");
    optionsBuilder.addSources(ImmutableList.copyOf(Iterables.transform(sources, TO_STRING)));
    try (JavacTurbine turbine = new JavacTurbine(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8))), optionsBuilder.build())) {
        assertThat(turbine.compile()).isEqualTo(Result.OK_WITH_FULL_CLASSPATH);
        Context context = turbine.context;
        JavacFileManager fm = (JavacFileManager) context.get(JavaFileManager.class);
        assertThat(fm.getLocationAsPaths(StandardLocation.CLASS_PATH)).containsExactly(libA, libB, libC, libD);
        Deps.Dependencies depsProto = getDeps();
        assertThat(depsProto.getSuccess()).isTrue();
        assertThat(depsProto.getRuleLabel()).isEqualTo("//my:target");
        assertThat(getEntries(depsProto)).containsExactlyEntriesIn(ImmutableMap.of(libA.toString(), Deps.Dependency.Kind.EXPLICIT, libB.toString(), Deps.Dependency.Kind.IMPLICIT, libC.toString(), Deps.Dependency.Kind.IMPLICIT, libD.toString(), Deps.Dependency.Kind.IMPLICIT));
    }
}
Also used : Path(java.nio.file.Path) Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Deps(com.google.devtools.build.lib.view.proto.Deps) JavaFileManager(javax.tools.JavaFileManager) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 29 with JavacFileManager

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

the class LanguageCompiler method getPackage.

private String getPackage(JavaFileObject file) throws IOException {
    Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
    // Figure out the package name by stripping the "-src" prefix and
    // extracting
    // the package part of the fullname.
    String filePath = file.toUri().getPath();
    // go absolute
    filePath = new File(filePath).getCanonicalPath();
    int srcDirLength = 0;
    for (File prefixFile : prefixes) {
        String prefix = prefixFile.getCanonicalPath();
        if (filePath.startsWith(prefix) && prefix.length() > srcDirLength) {
            srcDirLength = prefix.length();
        }
    }
    if (srcDirLength > 0) {
        String fullname = filePath.substring(srcDirLength);
        assert fullname.endsWith(".ceylon");
        fullname = fullname.substring(0, fullname.length() - ".ceylon".length());
        fullname = fullname.replace(File.separator, ".");
        if (fullname.startsWith("."))
            fullname = fullname.substring(1);
        String packageName = Convert.packagePart(fullname);
        if (!packageName.equals(""))
            return packageName;
    }
    return null;
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) VirtualFile(com.redhat.ceylon.compiler.typechecker.io.VirtualFile) File(java.io.File)

Example 30 with JavacFileManager

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

the class LanguageCompiler method getSrcDir.

// FIXME: this function is terrible, possibly refactor it with getPackage?
private File getSrcDir(File sourceFile) throws IOException {
    Iterable<? extends File> prefixes = ((JavacFileManager) fileManager).getLocation(StandardLocation.SOURCE_PATH);
    File srcDirFile = FileUtil.selectPath(prefixes, sourceFile.getPath());
    if (srcDirFile != null) {
        return srcDirFile;
    } else {
        // This error should have been caught by the tool chain
        throw new RuntimeException(sourceFile.getPath() + " is not in the current source path");
    }
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) 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