Search in sources :

Example 41 with Context

use of com.sun.tools.javac.util.Context in project bazel by bazelbuild.

the class JavacTurbineCompiler method compile.

static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {
    Map<String, OutputFileObject> files = new LinkedHashMap<>();
    Status status;
    StringWriter sw = new StringWriter();
    Context context = new Context();
    try (PrintWriter pw = new PrintWriter(sw)) {
        setupContext(context, request.strictJavaDepsPlugin());
        CacheFSInfo.preRegister(context);
        try (ZipOutputFileManager fm = new ZipOutputFileManager(files)) {
            JavacTask task = JavacTool.create().getTask(pw, fm, null, /*diagnostics*/
            request.javacOptions(), ImmutableList.of(), /*classes*/
            fm.getJavaFileObjectsFromPaths(request.sources()), context);
            fm.setContext(context);
            fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, Collections.<Path>emptyList());
            fm.setLocationFromPaths(StandardLocation.CLASS_PATH, request.classPath());
            fm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, request.bootClassPath());
            fm.setLocationFromPaths(StandardLocation.ANNOTATION_PROCESSOR_PATH, request.processorClassPath());
            status = task.call() ? Status.OK : Status.ERROR;
        } catch (Throwable t) {
            t.printStackTrace(pw);
            status = Status.ERROR;
        }
    }
    return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context);
}
Also used : Status(com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status) Context(com.sun.tools.javac.util.Context) OutputFileObject(com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) LinkedHashMap(java.util.LinkedHashMap) PrintWriter(java.io.PrintWriter)

Example 42 with Context

use of com.sun.tools.javac.util.Context 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 43 with Context

use of com.sun.tools.javac.util.Context 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 44 with Context

use of com.sun.tools.javac.util.Context 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 45 with Context

use of com.sun.tools.javac.util.Context in project bazel by bazelbuild.

the class TypesUtils method wildUpperBound.

// Version of com.sun.tools.javac.code.Types.wildUpperBound(Type)
// that works with both jdk8 (called upperBound there) and jdk8u.
// TODO: contrast to upperBound.
public static Type wildUpperBound(ProcessingEnvironment env, TypeMirror tm) {
    Type t = (Type) tm;
    if (t.hasTag(TypeTag.WILDCARD)) {
        Context context = ((JavacProcessingEnvironment) env).getContext();
        Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t);
        if (w.isSuperBound()) {
            Symtab syms = Symtab.instance(context);
            return w.bound == null ? syms.objectType : w.bound.bound;
        } else {
            return wildUpperBound(env, w.type);
        }
    } else {
        return TypeAnnotationUtils.unannotatedType(t);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab) ArrayType(javax.lang.model.type.ArrayType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Aggregations

Context (com.sun.tools.javac.util.Context)65 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)19 Test (org.junit.Test)9 Options (com.sun.tools.javac.util.Options)8 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 JavaFileObject (javax.tools.JavaFileObject)7 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JavacTask (com.sun.source.util.JavacTask)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStreamWriter (java.io.OutputStreamWriter)4 Path (java.nio.file.Path)4 DiagnosticListener (javax.tools.DiagnosticListener)4 ImmutableList (com.google.common.collect.ImmutableList)3 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 StringWriter (java.io.StringWriter)3