Search in sources :

Example 16 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class TestSuperclass method run.

int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
    System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
    File testDir = new File(ck + "-" + gk + "-" + sk);
    testDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
    JavaSource js = new JavaSource();
    System.err.println(js.getCharContent(false));
    CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
    if (!t.call())
        throw new Error("compilation failed");
    File testClass = new File(testDir, "Test.class");
    String out = javap(testClass);
    // Extract class sig from first line of Java source
    String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
    // Extract class sig from line from javap output
    String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
    checkEqual("class signature", expect, found);
    return errors;
}
Also used : File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 17 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class TestGetResource2 method testCompositeSourcePath.

private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    System.err.println("testCompositeSearchPath");
    File tmpdir = new File("testCompositeSourcePath");
    File srcdir = new File(tmpdir, "src");
    File rsrcdir = new File(tmpdir, "rsrc");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(rsrcdir, "resources/file.txt", "hello");
    destdir.mkdirs();
    CompilationTask task = javac.getTask(null, null, null, Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()), Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with composite source path: " + result);
    expect(result, true);
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 18 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class TestGetResource2 method testSingleSourceDir.

private void testSingleSourceDir(JavaCompiler javac) throws Exception {
    System.err.println("testSingleSourceDir");
    File tmpdir = new File("testSingleSourceDir");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(srcdir, "resources/file.txt", "hello");
    destdir.mkdirs();
    CompilationTask task = javac.getTask(null, null, null, Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with single source dir: " + result);
    expect(result, true);
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 19 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project com.revolsys.open by revolsys.

the class ScriptRunner method run.

public void run() {
    if (this.scriptFile == null) {
    } else if (this.scriptFile.exists() && this.scriptFile.isFile()) {
        final String fileExtension = FileUtil.getFileNameExtension(this.scriptFile);
        try {
            if ("java".equals(fileExtension)) {
                final String scriptClassName = FileUtil.getBaseName(this.scriptFile);
                final URIJavaFileObject scriptJavaFile = new URIJavaFileObject(this.scriptFile, Kind.SOURCE);
                final JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
                final JavaFileManager fileManager = new InMemoryJavaFileManager(scriptJavaFile);
                final CompilationTask compilationTask = javaCompiler.getTask(null, fileManager, null, null, null, Collections.singleton(scriptJavaFile));
                compilationTask.call();
                final ClassLoader classLoader = fileManager.getClassLoader(null);
                final Class<?> scriptClass;
                try {
                    scriptClass = classLoader.loadClass(scriptClassName);
                } catch (final ClassNotFoundException e) {
                    showErrorDialog(// 
                    "must contain the class:<br />" + "<code>public class " + scriptClassName + // 
                    "</code><br />" + "in the default package.");
                    return;
                }
                final Method mainMethod;
                try {
                    mainMethod = scriptClass.getDeclaredMethod("main", String[].class);
                } catch (final NoSuchMethodException e) {
                    showErrorDialog("must contain the method:<br />" + // 
                    "<code>public static void main(String[] args)</code><br />" + // 
                    " in the class:<br />" + "<code>" + scriptClassName + "</code>");
                    return;
                }
                try {
                    mainMethod.invoke(null, (Object) NULL_MAIN_ARGS);
                } catch (final InvocationTargetException e) {
                    final Throwable targetException = e.getTargetException();
                    showErrorDialog(scriptClass, targetException);
                } catch (final Throwable e) {
                    showErrorDialog(scriptClass, e);
                    return;
                }
            } else {
                final Binding binding = new Binding();
                final GroovyShell shell = new GroovyShell(binding);
                shell.run(this.scriptFile, NULL_MAIN_ARGS);
            }
        } catch (final Throwable e) {
            showErrorDialog(getClass(), e);
        }
    } else {
        showErrorDialog("Does not exist");
    }
}
Also used : Binding(groovy.lang.Binding) JavaFileManager(javax.tools.JavaFileManager) JavaCompiler(javax.tools.JavaCompiler) Method(java.lang.reflect.Method) CompilationTask(javax.tools.JavaCompiler.CompilationTask) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyShell(groovy.lang.GroovyShell)

Example 20 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project mdw-designer by CenturyLinkCloud.

the class ProcessProxy method compile.

private void compile(IFile sourceFile) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // JavaFileObject file = new JavaSourceFromString(getClassName(), source);
    // 
    // Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
    // CompilationTask task = compiler.getTask(null, null, diagnostics, null, null,
    // compilationUnits);
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(sourceFile.getRawLocation().toFile());
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    boolean success = task.call();
    for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
        System.out.println(diagnostic.getCode());
        System.out.println(diagnostic.getKind());
        System.out.println(diagnostic.getPosition());
        System.out.println(diagnostic.getStartPosition());
        System.out.println(diagnostic.getEndPosition());
        System.out.println(diagnostic.getSource());
        System.out.println(diagnostic.getMessage(null));
    }
    System.out.println("\nSUCCESS: " + success);
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File) IFile(org.eclipse.core.resources.IFile) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

CompilationTask (javax.tools.JavaCompiler.CompilationTask)84 JavaCompiler (javax.tools.JavaCompiler)38 File (java.io.File)33 JavaFileObject (javax.tools.JavaFileObject)32 StandardJavaFileManager (javax.tools.StandardJavaFileManager)28 DiagnosticCollector (javax.tools.DiagnosticCollector)27 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)18 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)14 URLClassLoader (java.net.URLClassLoader)10 StringWriter (java.io.StringWriter)9 URL (java.net.URL)6 JavaFileManager (javax.tools.JavaFileManager)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 PrintWriter (java.io.PrintWriter)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3