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