Search in sources :

Example 31 with JavaCompiler

use of javax.tools.JavaCompiler in project compiler by boalang.

the class BoaCompiler method compileGeneratedSrc.

private static void compileGeneratedSrc(final CommandLine cl, final String jarName, final File outputRoot, final File outputFile) throws RuntimeException, IOException, FileNotFoundException {
    // compile the generated .java file
    final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null)
        throw new RuntimeException("Could not get javac - are you running the Boa compiler with a JDK or a JRE?");
    LOG.info("compiling: " + outputFile);
    LOG.info("classpath: " + System.getProperty("java.class.path"));
    if (compiler.run(null, null, null, "-source", "5", "-target", "5", "-cp", System.getProperty("java.class.path"), outputFile.toString()) != 0)
        throw new RuntimeException("compile failed");
    final List<File> libJars = new ArrayList<File>();
    if (cl.hasOption('j')) {
        libJars.add(new File(cl.getOptionValue('j')));
    } else {
        // find the location of the jar this class is in
        final String path = ClasspathUrlFinder.findClassBase(BoaCompiler.class).getPath();
        // find the location of the compiler distribution
        final File root = new File(path.substring(path.indexOf(':') + 1, path.indexOf('!'))).getParentFile();
        libJars.add(new File(root, "boa-runtime.jar"));
    }
    if (cl.hasOption('l'))
        for (final String s : Arrays.asList(cl.getOptionValues('l'))) libJars.add(new File(s));
    generateJar(jarName, outputRoot, libJars);
    if (DefaultProperties.localDataPath == null) {
        delete(outputRoot);
    }
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) File(java.io.File)

Example 32 with JavaCompiler

use of javax.tools.JavaCompiler in project compiler by boalang.

the class BaseTest method codegen.

protected StartContext codegen(final String input, final String error) throws IOException {
    final File outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
    final File outputSrcDir = new File(outputRoot, "boa");
    if (!outputSrcDir.mkdirs())
        throw new IOException("unable to mkdir " + outputSrcDir);
    final File outputFile = new File(outputSrcDir, "Test.java");
    CodeGeneratingVisitor.combineAggregatorStrings.clear();
    CodeGeneratingVisitor.reduceAggregatorStrings.clear();
    final List<String> jobnames = new ArrayList<String>();
    final List<String> jobs = new ArrayList<String>();
    final List<Integer> seeds = new ArrayList<Integer>();
    final StartContext ctx = typecheck(input);
    // use the whole input string to seed the RNG
    seeds.add(input.hashCode());
    final Start p = ctx.ast;
    try {
        new InheritedAttributeTransformer().start(p);
        new LocalAggregationTransformer().start(p);
        new VisitorOptimizingTransformer().start(p);
        final CodeGeneratingVisitor cg = new CodeGeneratingVisitor("1");
        cg.start(p);
        jobs.add(cg.getCode());
        jobnames.add("1");
        final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
        st.add("name", "Test");
        st.add("numreducers", 1);
        st.add("jobs", jobs);
        st.add("jobnames", jobnames);
        st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
        st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
        st.add("splitsize", 64 * 1024 * 1024);
        st.add("seeds", seeds);
        final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
        try {
            o.write(st.render().getBytes());
        } finally {
            o.close();
        }
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { outputFile }));
        if (!compiler.getTask(null, fileManager, diagnostics, Arrays.asList(new String[] { "-cp", System.getProperty("java.class.path") }), null, compilationUnits).call())
            for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) throw new RuntimeException("Error on line " + diagnostic.getLineNumber() + ": " + diagnostic.getMessage(null));
        if (error != null)
            fail("expected to see exception: " + error);
    } catch (final Exception e) {
        if (error == null) {
            if (e.getMessage() == null) {
                e.printStackTrace();
                fail("unexpected exception");
            } else
                fail("found unexpected exception: " + e.getMessage());
        } else
            assertEquals(error, e.getMessage());
    }
    delete(outputSrcDir);
    return ctx;
}
Also used : Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) VisitorOptimizingTransformer(boa.compiler.transforms.VisitorOptimizingTransformer) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) BufferedOutputStream(java.io.BufferedOutputStream) ST(org.stringtemplate.v4.ST) InheritedAttributeTransformer(boa.compiler.transforms.InheritedAttributeTransformer) JavaCompiler(javax.tools.JavaCompiler) LocalAggregationTransformer(boa.compiler.transforms.LocalAggregationTransformer) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) StartContext(boa.parser.BoaParser.StartContext) FileOutputStream(java.io.FileOutputStream) AbstractCodeGeneratingVisitor(boa.compiler.visitors.AbstractCodeGeneratingVisitor) CodeGeneratingVisitor(boa.compiler.visitors.CodeGeneratingVisitor) File(java.io.File)

Example 33 with JavaCompiler

use of javax.tools.JavaCompiler in project ceylon-compiler by ceylon.

the class T6598108 method main.

public static void main(String[] args) throws Exception {
    //NOI18N
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    final JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject()));
    CompilationUnitTree cut = ct.parse().iterator().next();
    TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
    Scope s = Trees.instance(ct).getScope(tp);
    TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
    if (Trees.instance(ct).isAccessible(s, type)) {
        //"false" would be expected here.
        throw new IllegalStateException("");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) TreePath(com.sun.source.util.TreePath) Scope(com.sun.source.tree.Scope) TypeElement(javax.lang.model.element.TypeElement) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 34 with JavaCompiler

use of javax.tools.JavaCompiler 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 35 with JavaCompiler

use of javax.tools.JavaCompiler 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

JavaCompiler (javax.tools.JavaCompiler)101 StandardJavaFileManager (javax.tools.StandardJavaFileManager)43 JavaFileObject (javax.tools.JavaFileObject)39 File (java.io.File)31 DiagnosticCollector (javax.tools.DiagnosticCollector)24 ArrayList (java.util.ArrayList)22 JavacTask (com.sun.source.util.JavacTask)20 IOException (java.io.IOException)17 CompilationTask (javax.tools.JavaCompiler.CompilationTask)16 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)15 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)11 Diagnostic (javax.tools.Diagnostic)11 PrintWriter (java.io.PrintWriter)9 Test (org.junit.Test)9 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)5 StringWriter (java.io.StringWriter)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStream (java.io.OutputStream)4