use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.
the class T6963934 method main.
public static void main(String[] args) throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
CompilationUnitTree tree = task.parse().iterator().next();
int count = 0;
for (ImportTree importTree : tree.getImports()) {
System.out.println(importTree);
count++;
}
int expected = 7;
if (count != expected)
throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
use of javax.tools.StandardJavaFileManager in project ceylon by eclipse.
the class ToolsTest method compileAndJar.
public static File compileAndJar(String javaModule, String javaClassName, File addToClassPath) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
String javaModulePath = javaModule.replace('.', '/');
File moduleFile = new File("test/" + javaModulePath, javaClassName + ".java");
Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjects(moduleFile);
File destDir = new File("build/javaModules");
FileUtil.delete(destDir);
destDir.mkdirs();
List<String> options = new LinkedList<String>();
options.add("-d");
options.add(destDir.getPath());
if (addToClassPath != null) {
options.add("-cp");
options.add(addToClassPath.getPath());
}
CompilationTask task = compiler.getTask(null, null, null, options, null, units);
Boolean result = task.call();
assertTrue(result != null && result.booleanValue());
File compiledModuleFile = new File(destDir, javaModulePath + "/" + javaClassName + ".class");
assertTrue(compiledModuleFile.isFile());
return jar(compiledModuleFile, javaModulePath);
}
use of javax.tools.StandardJavaFileManager in project antlr4 by antlr.
the class BaseJavaTest method compile.
/**
* Wow! much faster than compiling outside of VM. Finicky though.
* Had rules called r and modulo. Wouldn't compile til I changed to 'a'.
*/
protected boolean compile(String... fileNames) {
List<File> files = new ArrayList<File>();
for (String fileName : fileNames) {
File f = new File(getTempTestDir(), fileName);
files.add(f);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// DiagnosticCollector<JavaFileObject> diagnostics =
// new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
Iterable<String> compileOptions = Arrays.asList("-g", "-source", "1.6", "-target", "1.6", "-implicit:class", "-Xlint:-options", "-d", getTempDirPath(), "-cp", getTempDirPath() + PATH_SEP + CLASSPATH);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, compileOptions, null, compilationUnits);
boolean ok = task.call();
try {
fileManager.close();
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
return ok;
}
use of javax.tools.StandardJavaFileManager in project flink by apache.
the class TestUserClassLoaderJar method createJarFile.
/**
* Pack the generated UDF class into a JAR and return the path of the JAR.
*/
public static File createJarFile(File tmpDir, String jarName, String className, String javaCode) throws IOException {
// write class source code to file
File javaFile = Paths.get(tmpDir.toString(), className + ".java").toFile();
// noinspection ResultOfMethodCallIgnored
javaFile.createNewFile();
FileUtils.writeFileUtf8(javaFile, javaCode);
// compile class source code
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(javaFile));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, Collections.emptyList(), null, compilationUnit);
task.call();
// pack class file to jar
File classFile = Paths.get(tmpDir.toString(), className + ".class").toFile();
File jarFile = Paths.get(tmpDir.toString(), jarName).toFile();
JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile));
JarEntry jarEntry = new JarEntry(className + ".class");
jos.putNextEntry(jarEntry);
byte[] classBytes = FileUtils.readAllBytes(classFile.toPath());
jos.write(classBytes);
jos.closeEntry();
jos.close();
return jarFile;
}
use of javax.tools.StandardJavaFileManager in project androidannotations by androidannotations.
the class ProcessorTestHelper method compileFiles.
public CompileResult compileFiles(Collection<File> compilationUnits) {
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null)) {
CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, compilerOptions, null, fileManager.getJavaFileObjectsFromFiles(compilationUnits));
List<Processor> processors = new ArrayList<>();
for (Class<? extends Processor> processorClass : processorsClasses) {
try {
processors.add(processorClass.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
task.setProcessors(processors);
task.call();
} catch (IOException e) {
// we should always be able to close the manager
}
return new CompileResult(diagnosticCollector.getDiagnostics());
}
Aggregations