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);
}
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);
}
}
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;
}
});
}
}
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));
}
}
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);
}
}
Aggregations