use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class T6472751 method main.
public static void main(String[] args) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
trees = Trees.instance(task);
positions = trees.getSourcePositions();
Iterable<? extends CompilationUnitTree> asts = task.parse();
for (CompilationUnitTree ast : asts) {
new MyVisitor().scan(ast, null);
}
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class ImplementationCacheTest method main.
public static void main(String[] args) throws IOException {
List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
Context ctx = new Context();
JavacFileManager.preRegister(ctx);
checkImplementationCache(ct.analyze(), Types.instance(ctx));
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class Main method main.
public static void main(String[] args) throws Exception {
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
JavacTask javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
Elements elements = javac.getElements();
final Set<String> packages = new LinkedHashSet<String>();
int nestedClasses = 0;
int classes = 0;
for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
if (type.endsWith("package-info"))
continue;
try {
TypeElement elem = elements.getTypeElement(type);
if (elem == null && type.indexOf('$') > 0) {
nestedClasses++;
type = null;
continue;
}
classes++;
packages.add(getPackage(elem).getQualifiedName().toString());
// force completion
elements.getTypeElement(type).getKind();
type = null;
} finally {
if (type != null)
System.err.println("Looking at " + type);
}
}
javac = null;
elements = null;
javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
elements = javac.getElements();
for (String name : packages) {
PackageElement pe = elements.getPackageElement(name);
for (Element e : pe.getEnclosedElements()) {
e.getSimpleName().getClass();
}
}
/*
* A few sanity checks based on current values:
*
* packages: 775, classes: 12429 + 5917
*
* As the platform evolves the numbers are likely to grow
* monotonically but in case somebody gets a clever idea for
* limiting the number of packages exposed, this number might
* drop. So we test low values.
*/
System.out.format("packages: %s, classes: %s + %s%n", packages.size(), classes, nestedClasses);
if (classes < 9000)
throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
if (packages.size() < 530)
throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
if (nestedClasses < 3000)
throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
}
use of com.sun.source.util.JavacTask in project bazel by bazelbuild.
the class BlazeJavacMain method compile.
public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
List<String> javacArguments = arguments.javacOptions();
try {
javacArguments = processPluginArgs(arguments.plugins(), javacArguments);
} catch (InvalidCommandLineException e) {
return BlazeJavacResult.error(e.getMessage());
}
Context context = new Context();
setupBlazeJavaCompiler(arguments.plugins(), context);
boolean ok = false;
StringWriter errOutput = new StringWriter();
// TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
// it and handling exceptions directly in callers.
PrintWriter errWriter = new PrintWriter(errOutput);
Listener diagnostics = new Listener(context);
BlazeJavaCompiler compiler;
try (JavacFileManager fileManager = new ClassloaderMaskingFileManager()) {
JavacTask task = JavacTool.create().getTask(errWriter, fileManager, diagnostics, javacArguments, ImmutableList.of(), /*classes*/
fileManager.getJavaFileObjectsFromPaths(arguments.sourceFiles()), context);
if (arguments.processors() != null) {
task.setProcessors(arguments.processors());
}
fileManager.setContext(context);
setLocations(fileManager, arguments);
try {
ok = task.call();
} catch (PropagatedException e) {
throw e.getCause();
}
} catch (Throwable t) {
t.printStackTrace(errWriter);
ok = false;
} finally {
compiler = (BlazeJavaCompiler) JavaCompiler.instance(context);
if (ok) {
// or empty source files.
if (compiler.skippedFlowEvents() > 0 && compiler.flowEvents() == 0) {
errWriter.println("Expected at least one FLOW event");
ok = false;
}
}
}
errWriter.flush();
return new BlazeJavacResult(ok, filterDiagnostics(diagnostics.build()), errOutput.toString(), compiler);
}
use of com.sun.source.util.JavacTask in project bazel by bazelbuild.
the class JavacTurbineCompiler method compile.
static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {
Map<String, OutputFileObject> files = new LinkedHashMap<>();
Status status;
StringWriter sw = new StringWriter();
Context context = new Context();
try (PrintWriter pw = new PrintWriter(sw)) {
setupContext(context, request.strictJavaDepsPlugin());
CacheFSInfo.preRegister(context);
try (ZipOutputFileManager fm = new ZipOutputFileManager(files)) {
JavacTask task = JavacTool.create().getTask(pw, fm, null, /*diagnostics*/
request.javacOptions(), ImmutableList.of(), /*classes*/
fm.getJavaFileObjectsFromPaths(request.sources()), context);
fm.setContext(context);
fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, Collections.<Path>emptyList());
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, request.classPath());
fm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, request.bootClassPath());
fm.setLocationFromPaths(StandardLocation.ANNOTATION_PROCESSOR_PATH, request.processorClassPath());
status = task.call() ? Status.OK : Status.ERROR;
} catch (Throwable t) {
t.printStackTrace(pw);
status = Status.ERROR;
}
}
return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context);
}
Aggregations