use of javax.tools.SimpleJavaFileObject in project querydsl by querydsl.
the class CompileUtils method assertCompiles.
public static void assertCompiles(String name, String source) {
URLClassLoader parent = (URLClassLoader) CompileUtils.class.getClassLoader();
SimpleCompiler compiler = new SimpleCompiler();
MemFileManager fileManager = new MemFileManager(parent, compiler.getStandardFileManager(null, null, null));
String classpath = SimpleCompiler.getClassPath(parent);
List<String> compilationOptions = Arrays.asList("-classpath", classpath, "-g:none");
// compile
SimpleJavaFileObject javaFileObject = new MemSourceFileObject(name, source);
Writer out = new StringWriter();
JavaCompiler.CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, Collections.singletonList(javaFileObject));
if (!task.call()) {
Assert.fail("Compilation of " + source + " failed.\n" + out.toString());
}
}
use of javax.tools.SimpleJavaFileObject 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.SimpleJavaFileObject in project ceylon-compiler by ceylon.
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 javax.tools.SimpleJavaFileObject in project ceylon-compiler by ceylon.
the class T6733837 method exec.
public void exec() {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"), Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "\tclass ErroneousWithTab";
}
};
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
List<? extends JavaFileObject> files = Arrays.asList(sfo);
task = tool.getTask(sw, fm, null, null, null, files);
try {
((JavacTask) task).analyze();
} catch (Throwable t) {
throw new Error("Compiler threw an exception");
}
System.err.println(sw.toString());
if (!sw.toString().contains("/Test.java"))
throw new Error("Bad source name in diagnostic");
}
use of javax.tools.SimpleJavaFileObject in project ceylon-compiler by ceylon.
the class T6852595 method main.
public static void main(String[] args) throws IOException {
JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"), Kind.SOURCE) {
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return "class BadName { Object o = j; }";
}
};
List<? extends JavaFileObject> files = Arrays.asList(sfo);
JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTask ct = (JavacTask) tool.getTask(null, null, null, null, null, files);
Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
CompilationUnitTree cu = compUnits.iterator().next();
ClassTree cdef = (ClassTree) cu.getTypeDecls().get(0);
JCVariableDecl vdef = (JCVariableDecl) cdef.getMembers().get(0);
TreePath path = TreePath.getPath(cu, vdef.init);
Trees.instance(ct).getScope(path);
}
Aggregations