use of javax.tools.StandardJavaFileManager in project auto by google.
the class GeneratedAnnotationsTest method runProcessor.
/**
* Run {@link TestProcessor} in a compilation with the given {@code options}, and prevent the
* compilation from accessing classes with the qualified names in {@code maskFromClasspath}.
*/
private String runProcessor(ImmutableList<String> options, @Nullable String packageToMask) throws IOException {
File tempDir = temporaryFolder.newFolder();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(/* diagnosticListener= */
null, /* locale= */
null, UTF_8);
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(tempDir));
StandardJavaFileManager proxyFileManager = Reflection.newProxy(StandardJavaFileManager.class, new FileManagerInvocationHandler(standardFileManager, packageToMask));
CompilationTask task = compiler.getTask(/* out= */
null, proxyFileManager, /* diagnosticListener= */
null, options, /* classes= */
null, ImmutableList.of(new SimpleJavaFileObject(URI.create("test"), Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return "class Test {}";
}
}));
task.setProcessors(ImmutableList.of(new TestProcessor()));
assertThat(task.call()).isTrue();
return new String(Files.readAllBytes(tempDir.toPath().resolve("G.java")), UTF_8);
}
use of javax.tools.StandardJavaFileManager in project j2objc by google.
the class JavacParser method createEnvironment.
// Creates a javac environment from a collection of files and/or file objects.
private JavacEnvironment createEnvironment(List<File> files, List<JavaFileObject> fileObjects, boolean processAnnotations) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = getFileManager(compiler, diagnostics);
List<String> javacOptions = getJavacOptions(processAnnotations);
if (fileObjects == null) {
fileObjects = new ArrayList<>();
}
for (JavaFileObject jfo : fileManager.getJavaFileObjectsFromFiles(files)) {
fileObjects.add(filterJavaFileObject(jfo));
}
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, diagnostics, javacOptions, null, fileObjects);
return new JavacEnvironment(task, fileManager, diagnostics);
}
use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.
the class GenericConstructorAndDiamondTest method main.
public static void main(String... args) throws Exception {
// create default shared JavaCompiler - reused across multiple compilations
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
for (BoundKind boundKind : BoundKind.values()) {
for (ConstructorKind constructorKind : ConstructorKind.values()) {
for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
for (TypeArgArity arity : TypeArgArity.values()) {
for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
for (ArgumentKind argKind : ArgumentKind.values()) {
new GenericConstructorAndDiamondTest(boundKind, constructorKind, declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
}
}
}
}
}
}
}
}
use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.
the class T6993305 method run.
void run() throws Exception {
File testSrc = new File(System.getProperty("test.src"));
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
JavacTask task = tool.getTask(null, fm, null, null, null, fos);
Iterable<? extends CompilationUnitTree> cus = task.parse();
TestScanner s = new TestScanner();
s.scan(cus, task);
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.
the class GenStubs method run.
boolean run(String sourcepath, File outdir, List<String> classes) {
// System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
if (sourcepath == null)
throw new IllegalArgumentException("sourcepath not set");
if (outdir == null)
throw new IllegalArgumentException("source output dir not set");
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
try {
fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
for (String c : classes) {
JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
if (fo == null)
error("class not found: " + c);
else
files.add(fo);
}
JavacTask t = tool.getTask(null, fm, null, null, null, files);
Iterable<? extends CompilationUnitTree> trees = t.parse();
for (CompilationUnitTree tree : trees) {
makeStub(fm, tree);
}
} catch (IOException e) {
error("IO error " + e, e);
}
return (errors == 0);
}
Aggregations