Search in sources :

Example 1 with SimpleJavaFileObject

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());
    }
}
Also used : SimpleCompiler(com.mysema.codegen.SimpleCompiler) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) StringWriter(java.io.StringWriter) MemFileManager(com.mysema.codegen.MemFileManager) URLClassLoader(java.net.URLClassLoader) MemSourceFileObject(com.mysema.codegen.MemSourceFileObject) JavaCompiler(javax.tools.JavaCompiler) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 2 with SimpleJavaFileObject

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());
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 3 with SimpleJavaFileObject

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();
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 4 with SimpleJavaFileObject

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");
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) PrintWriter(java.io.PrintWriter)

Example 5 with SimpleJavaFileObject

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);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Aggregations

SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)9 JavaCompiler (javax.tools.JavaCompiler)7 JavaFileObject (javax.tools.JavaFileObject)7 JavacTask (com.sun.source.util.JavacTask)6 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)3 Trees (com.sun.source.util.Trees)3 ClassTree (com.sun.source.tree.ClassTree)2 Tree (com.sun.source.tree.Tree)2 StringWriter (java.io.StringWriter)2 MemFileManager (com.mysema.codegen.MemFileManager)1 MemSourceFileObject (com.mysema.codegen.MemSourceFileObject)1 SimpleCompiler (com.mysema.codegen.SimpleCompiler)1 ErroneousTree (com.sun.source.tree.ErroneousTree)1 TreePath (com.sun.source.util.TreePath)1 TreeScanner (com.sun.source.util.TreeScanner)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1