summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs/IncrementalCompileTest.java
blob: 6cd03fb72328f0337fa306d22807431c02ed3ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.javacs;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.util.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Test;

public class IncrementalCompileTest implements TaskListener, DiagnosticListener<JavaFileObject> {
    final JavaCompiler compiler = ServiceLoader.load(JavaCompiler.class).iterator().next();
    final Path src = Paths.get("src/test/incremental-compile/src").toAbsolutePath();
    final Path foo = src.resolve("foo/bar/Foo.java");
    final List<String> options = List.of("-sourcepath", src.toString(), "-verbose", "-proc:none");

    @Before
    public void setLogFormat() {
        Main.setRootFormat();
    }

    @Test
    public void freshTask() {
        var fileManager = compiler.getStandardFileManager(this, null, Charset.defaultCharset());
        for (var i = 0; i < 2; i++) {
            LOG.info(String.format("Compile %d...", i));
            var files = fileManager.getJavaFileObjects(foo);
            var task = (JavacTask) compiler.getTask(null, fileManager, this, options, null, files);
            checkInvokeType(task);
        }
    }

    @Test
    public void taskPool() {
        var fileManager = compiler.getStandardFileManager(this, null, Charset.defaultCharset());
        var pool = new TaskPool(1);
        for (var i = 0; i < 2; i++) {
            var files = fileManager.getJavaFileObjects(foo);
            LOG.info(String.format("Compile %d...", i));
            pool.getTask(
                    null,
                    fileManager,
                    this,
                    options,
                    null,
                    files,
                    task -> {
                        checkInvokeType(task);
                        return "Done!";
                    });
        }
    }

    private void checkInvokeType(JavacTask task) {
        task.addTaskListener(this);
        try {
            var root = task.parse().iterator().next();
            task.analyze();
            LOG.info("Scan " + root);
            new TreePathScanner<Void, Void>() {
                @Override
                public Void visitMethodInvocation(MethodInvocationTree t, Void __) {
                    if (t.getMethodSelect().toString().equals("Bar.test")) {
                        var type = Trees.instance(task).getTypeMirror(getCurrentPath());
                        LOG.info("Check " + t + ": " + type);
                        assertThat(type.toString(), equalTo("int"));
                    }
                    return null;
                }
            }.scan(root, null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void report(Diagnostic<? extends JavaFileObject> d) {
        LOG.warning(d.getMessage(null));
    }

    @Override
    public void started(TaskEvent e) {
        if (e.getSourceFile() == null) {
            LOG.info(String.format("...started %s", e.getKind()));
            return;
        }
        LOG.info(String.format("...started %s %s", e.getKind(), e.getSourceFile().getName()));
    }

    @Override
    public void finished(TaskEvent e) {
        if (e.getSourceFile() == null) {
            LOG.info(String.format("...finished %s", e.getKind()));
            return;
        }
        LOG.info(String.format("...finished %s %s", e.getKind(), e.getSourceFile().getName()));
    }

    private static final Logger LOG = Logger.getLogger("main");
}