use of javax.tools.StandardJavaFileManager in project bazel by bazelbuild.
the class BazelJavaCompiler method newInstance.
private static JavaCompiler newInstance(final JavaCompiler delegate) {
// We forward most operations to the JavaCompiler implementation in langtools.jar.
return new JavaCompiler() {
@Override
public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
// We prepend bazel's default javacopts to user javacopts,
// so that the user can override them. javac supports this
// "last option wins" style of option override.
ImmutableList.Builder<String> fullOptions = ImmutableList.builder();
fullOptions.addAll(getDefaultJavacopts());
if (options != null) {
fullOptions.addAll(options);
}
return delegate.getTask(out, fileManager, diagnosticListener, fullOptions.build(), classes, compilationUnits);
}
@Override
public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
StandardJavaFileManager fileManager = delegate.getStandardFileManager(diagnosticListener, locale, charset);
try {
fileManager.setLocation(// bootclasspath
StandardLocation.PLATFORM_CLASS_PATH, JavacBootclasspath.asFiles());
} catch (IOException e) {
// Should never happen, according to javadocs for setLocation
throw new RuntimeException(e);
}
return fileManager;
}
@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
// prepend bazel's default javacopts to user arguments
List<String> args = ImmutableList.<String>builder().addAll(getDefaultJavacopts()).add(arguments).build();
return delegate.run(in, out, err, args.toArray(new String[0]));
}
@Override
public Set<SourceVersion> getSourceVersions() {
return delegate.getSourceVersions();
}
@Override
public int isSupportedOption(String option) {
return delegate.isSupportedOption(option);
}
};
}
use of javax.tools.StandardJavaFileManager in project bazel by bazelbuild.
the class BazelJavaCompilerTest method assertCompileSucceeds.
private void assertCompileSucceeds(final String uri, final String content) throws Exception {
JavaCompiler javac = BazelJavaCompiler.newInstance();
JavaFileObject source = new SimpleJavaFileObject(URI.create(uri), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return content;
}
};
StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null);
// setting the output path by passing a flag to getTask is not reliable
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(getTmpDir()));
DiagnosticCollector<JavaFileObject> messages = new DiagnosticCollector<>();
JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, messages, null, null, Collections.singletonList(source));
assertTrue(task.call());
assertTrue(messages.getDiagnostics().isEmpty());
}
use of javax.tools.StandardJavaFileManager in project error-prone by google.
the class CodeTransformerTestHelper method transform.
public JavaFileObject transform(JavaFileObject original) {
JavaCompiler compiler = JavacTool.create();
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, ImmutableList.<String>of(), null, ImmutableList.of(original));
try {
SourceFile sourceFile = SourceFile.create(original);
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(trees, JCCompilationUnit.class));
DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree);
transformer().apply(new TreePath(tree), task.getContext(), diff);
diff.applyDifferences(sourceFile);
return JavaFileObjects.forSourceString(Iterables.getOnlyElement(Iterables.filter(tree.getTypeDecls(), JCClassDecl.class)).sym.getQualifiedName().toString(), sourceFile.getSourceText());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of javax.tools.StandardJavaFileManager in project gradle by gradle.
the class JdkJavaCompiler method createCompileTask.
private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec) {
List<String> options = new JavaCompilerArgumentsBuilder(spec).build();
JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create();
CompileOptions compileOptions = spec.getCompileOptions();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(spec.getSource());
return compiler.getTask(null, null, null, options, null, compilationUnits);
}
use of javax.tools.StandardJavaFileManager in project mastering-java by Kingminghuang.
the class Compiler method main.
public static void main(String[] args) {
String source = "public class Main{" + "public static void main(String[] args){System.out.println(\"Hello World!\");}}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
StringSourceJavaObject sourceObject = new StringSourceJavaObject("Main", source);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, Arrays.asList(sourceObject));
boolean result = task.call();
if (result) {
System.out.println("Compiled successfully");
}
}
Aggregations