use of com.sun.source.util.JavacTask in project ceylon by eclipse.
the class Test method test.
void test(File test) throws Exception {
JavacTool tool1 = JavacTool.create();
StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
// parse test file into a tree, and write it out to a stringbuffer using Pretty
JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<? extends CompilationUnitTree> trees = t1.parse();
for (CompilationUnitTree tree : trees) {
new Pretty(pw, true).printExpr((JCTree) tree);
}
pw.close();
final String out = sw.toString();
System.err.println("generated code:\n" + out + "\n");
// verify the generated code is valid Java by compiling it
JavacTool tool2 = JavacTool.create();
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return out;
}
};
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
boolean ok = t2.call();
if (!ok)
throw new Exception("compilation of generated code failed");
File expectedClass = new File(test.getName().replace(".java", ".class"));
if (!expectedClass.exists())
throw new Exception(expectedClass + " not found");
}
use of com.sun.source.util.JavacTask in project ceylon by eclipse.
the class T6956638 method test.
void test(File... sourceFiles) throws Exception {
System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
File classesDir = new File("classes" + count);
classesDir.mkdirs();
StringWriter compilerOutputStream = new StringWriter();
List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
JavacTask javacTask = (JavacTask) task;
Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
Iterable<? extends Element> analyzedTrees = javacTask.analyze();
Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
System.err.print("3-");
for (JavaFileObject f : generatedFiles) System.err.print(" " + f);
System.err.println("");
System.err.print("5-");
for (File f : classesDir.listFiles()) System.err.print(" " + f);
System.err.println("");
System.err.println("----");
System.err.println(compilerOutputStream.toString());
if (size(generatedFiles) != size(parsedTrees)) {
throw new Exception("wrong number of files generated: " + size(generatedFiles) + " expected: " + size(parsedTrees));
}
}
use of com.sun.source.util.JavacTask in project ceylon by eclipse.
the class T6608214 method main.
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create(""), Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class Test<S> { <T extends S & Runnable> void test(){}}";
}
};
List<? extends JavaFileObject> files = Arrays.asList(sfo);
String bootPath = System.getProperty("sun.boot.class.path");
List<String> opts = Arrays.asList("-bootclasspath", bootPath, "-Xjcov");
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask) tool.getTask(null, null, null, opts, null, files);
ct.analyze();
}
use of com.sun.source.util.JavacTask in project ceylon by eclipse.
the class T6430241 method testTaskAPI.
void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception {
System.err.println("test task API: " + pcp);
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
if (pcp != null)
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
JavacTask task = tool.getTask(pw, fm, null, null, null, files);
boolean ok = task.call();
String out = showOutput(sw.toString());
checkCompilationOK(ok);
checkOutput(out, expectWarnings);
}
use of com.sun.source.util.JavacTask in project ceylon by eclipse.
the class T6345974 method main.
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter(System.out, true);
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
File testSrc = new File(System.getProperty("test.src"));
Iterable<? extends JavaFileObject> f = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, "T6345974.java")));
JavacTask task = tool.getTask(out, fm, null, null, null, f);
Iterable<? extends CompilationUnitTree> trees = task.parse();
out.flush();
Scanner s = new Scanner();
for (CompilationUnitTree t : trees) s.scan(t, null);
}
Aggregations