use of javax.tools.StandardJavaFileManager in project ceylon-compiler by ceylon.
the class TestSuperclass method main.
public static void main(String... args) throws Exception {
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
int errors = 0;
for (ClassKind ck : ClassKind.values()) {
for (GenericKind gk : GenericKind.values()) {
for (SuperKind sk : SuperKind.values()) {
errors += new TestSuperclass(ck, gk, sk).run(comp, fm);
}
}
}
if (errors > 0)
throw new Exception(errors + " errors found");
}
use of javax.tools.StandardJavaFileManager in project zoj by licheng.
the class CustomJavaCompiler method main.
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Invalid arg length " + args.length);
System.exit(-1);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
JavaFileManager fileManager = new CustomJavaFileManager(stdFileManager);
System.out.println(new File(args[0]).getAbsolutePath());
System.out.println(compiler.getTask(null, fileManager, null, null, null, stdFileManager.getJavaFileObjects(args[0])).call());
}
use of javax.tools.StandardJavaFileManager in project gradle by gradle.
the class SimpleGeneratedJavaClassCompiler method compile.
/**
* Compiles generated Java source files.
*
* @param srcDir where the compiler will output the sources
* @param dstDir where the compiler will output the class files
* @param classes the classes to compile
* @param classPath the classpath to use for compilation
*/
public static void compile(File srcDir, File dstDir, List<ClassSource> classes, ClassPath classPath) throws GeneratedClassCompilationException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new GeneratedClassCompilationException("No Java compiler found, please ensure you are running Gradle with a JDK");
}
DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
try (StandardJavaFileManager mgr = compiler.getStandardFileManager(ds, null, null)) {
List<String> options = buildOptions(dstDir, classPath);
List<File> filesToCompile = outputSourceFilesToSourceDir(srcDir, classes);
if (dstDir.exists() || dstDir.mkdirs()) {
Iterable<? extends JavaFileObject> sources = mgr.getJavaFileObjectsFromFiles(filesToCompile);
JavaCompiler.CompilationTask task = compiler.getTask(null, mgr, ds, options, null, sources);
task.call();
} else {
throw new GeneratedClassCompilationException("Unable to create output classes directory");
}
} catch (IOException e) {
throw new GeneratedClassCompilationException("Unable to compile generated classes", e);
}
List<Diagnostic<? extends JavaFileObject>> diagnostics = ds.getDiagnostics().stream().filter(d -> d.getKind() == Diagnostic.Kind.ERROR).collect(Collectors.toList());
if (!diagnostics.isEmpty()) {
throwCompilationError(diagnostics);
}
}
use of javax.tools.StandardJavaFileManager in project gradle by gradle.
the class JdkJavaCompiler method createCompileTask.
private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec, ApiCompilerResult result) {
List<String> options = new JavaCompilerArgumentsBuilder(spec).build();
JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create();
MinimalJavaCompileOptions compileOptions = spec.getCompileOptions();
Charset charset = compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null;
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, charset);
Iterable<? extends JavaFileObject> compilationUnits = standardFileManager.getJavaFileObjectsFromFiles(spec.getSourceFiles());
boolean hasEmptySourcepaths = JavaVersion.current().isJava9Compatible() && emptySourcepathIn(options);
JavaFileManager fileManager = GradleStandardJavaFileManager.wrap(standardFileManager, DefaultClassPath.of(spec.getAnnotationProcessorPath()), hasEmptySourcepaths);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, spec.getClasses(), compilationUnits);
if (compiler instanceof IncrementalCompilationAwareJavaCompiler) {
task = ((IncrementalCompilationAwareJavaCompiler) compiler).makeIncremental(task, result.getSourceClassesMapping(), result.getConstantsAnalysisResult(), new CompilationSourceDirs(spec));
}
Set<AnnotationProcessorDeclaration> annotationProcessors = spec.getEffectiveAnnotationProcessors();
task = new AnnotationProcessingCompileTask(task, annotationProcessors, spec.getAnnotationProcessorPath(), result.getAnnotationProcessingResult());
task = new ResourceCleaningCompilationTask(task, fileManager);
return task;
}
use of javax.tools.StandardJavaFileManager in project bazel by bazelbuild.
the class VanillaJavaBuilder method run.
public VanillaJavaBuilderResult run(List<String> args) throws IOException {
OptionsParser optionsParser;
try {
optionsParser = new OptionsParser(args);
} catch (InvalidCommandLineException e) {
return new VanillaJavaBuilderResult(false, e.getMessage());
}
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
StringWriter output = new StringWriter();
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnosticCollector, ENGLISH, UTF_8);
setLocations(optionsParser, fileManager);
ImmutableList<JavaFileObject> sources = getSources(optionsParser, fileManager);
boolean ok;
if (sources.isEmpty()) {
ok = true;
} else {
CompilationTask task = javaCompiler.getTask(new PrintWriter(output, true), fileManager, diagnosticCollector, JavacOptions.removeBazelSpecificFlags(optionsParser.getJavacOpts()), ImmutableList.<String>of(), /*classes*/
sources);
setProcessors(optionsParser, fileManager, task);
ok = task.call();
}
if (ok) {
writeOutput(optionsParser);
}
writeGeneratedSourceOutput(optionsParser);
// the file to be created
if (optionsParser.getOutputDepsProtoFile() != null) {
try (OutputStream os = Files.newOutputStream(Paths.get(optionsParser.getOutputDepsProtoFile()))) {
Deps.Dependencies.newBuilder().setRuleLabel(optionsParser.getTargetLabel()).setSuccess(ok).build().writeTo(os);
}
}
// TODO(cushon): support manifest protos & genjar
if (optionsParser.getManifestProtoPath() != null) {
try (OutputStream os = Files.newOutputStream(Paths.get(optionsParser.getManifestProtoPath()))) {
Manifest.getDefaultInstance().writeTo(os);
}
}
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
StringBuilder message = new StringBuilder();
if (diagnostic.getSource() != null) {
message.append(diagnostic.getSource().getName());
if (diagnostic.getLineNumber() != -1) {
message.append(':').append(diagnostic.getLineNumber());
}
message.append(": ");
}
message.append(diagnostic.getKind().toString().toLowerCase(ENGLISH));
message.append(": ").append(diagnostic.getMessage(ENGLISH)).append(System.lineSeparator());
output.write(message.toString());
}
return new VanillaJavaBuilderResult(ok, output.toString());
}
Aggregations