summaryrefslogtreecommitdiff
path: root/src/main/java/org/javacs/CompileBatch.java
blob: 88a728d0d24d45859dc345a9547d998a4f43af1e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.javacs;

import com.sun.source.tree.*;
import com.sun.source.util.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.lang.model.element.*;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import org.eclipse.lsp4j.Range;

public class CompileBatch {
    private final JavaCompilerService parent;
    private final JavacTask task;
    private final Trees trees;
    private final List<CompilationUnitTree> roots;

    CompileBatch(JavaCompilerService parent, Collection<URI> files) {
        this.parent = parent;
        this.task = batchTask(parent, files);
        this.trees = Trees.instance(task);
        this.roots = new ArrayList<CompilationUnitTree>();
        var profiler = new Profiler();
        task.addTaskListener(profiler);
        try {
            for (var t : task.parse()) roots.add(t);
            task.analyze();
            // The results of task.analyze() are unreliable when errors are present
            // You can get at `Element` values using `Trees`
            task.analyze();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        profiler.print();
    }

    static JavacTask batchTask(JavaCompilerService parent, Collection<URI> paths) {
        parent.diags.clear();
        var files = new ArrayList<File>();
        for (var p : paths) files.add(new File(p));
        return (JavacTask)
                parent.compiler.getTask(
                        null,
                        parent.fileManager,
                        parent.diags::add,
                        JavaCompilerService.options(parent.sourcePath, parent.classPath),
                        Collections.emptyList(),
                        parent.fileManager.getJavaFileObjectsFromFiles(files));
    }

    public List<Diagnostic<? extends JavaFileObject>> lint() {
        return Collections.unmodifiableList(new ArrayList<>(parent.diags));
    }

    public List<TreePath> references(Element to) {
        LOG.info(String.format("Search for references to `%s` in %d files...", to, roots.size()));

        var result = new ArrayList<TreePath>();
        for (var f : roots) {
            result.addAll(referencesToElement(f, to));
        }
        return result;
    }

    public Map<URI, Index> countReferences() {
        var index = new HashMap<URI, Index>();
        for (var f : roots) {
            var uri = f.getSourceFile().toUri();
            var path = Paths.get(uri);
            var refs = index(f);
            FileTime modified;
            try {
                modified = Files.getLastModifiedTime(path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            var i = new Index(refs, modified.toInstant());
            index.put(uri, i);
        }
        return index;
    }

    public Optional<Range> range(TreePath path) {
        var uri = path.getCompilationUnit().getSourceFile().toUri();
        var file = Paths.get(uri);
        String contents;
        try {
            contents = Files.readAllLines(file).stream().collect(Collectors.joining("\n"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ParseFile.range(task, contents, path);
    }

    private boolean toStringEquals(Object left, Object right) {
        return Objects.equals(Objects.toString(left, ""), Objects.toString(right, ""));
    }

    private boolean sameSymbol(Element from, Element to) {
        return to != null
                && from != null
                && toStringEquals(to.getEnclosingElement(), from.getEnclosingElement())
                && toStringEquals(to, from);
    }

    private boolean isField(Element to) {
        if (!(to instanceof VariableElement)) return false;
        var field = (VariableElement) to;
        return field.getEnclosingElement() instanceof TypeElement;
    }

    private Optional<TreePath> ref(TreePath from) {
        var root = from.getCompilationUnit();
        var lines = root.getLineMap();
        var to = trees.getElement(from);
        // Skip elements we can't find
        if (to == null) {
            // LOG.warning(String.format("No element for `%s`", from.getLeaf()));
            return Optional.empty();
        }
        // Skip non-methods
        if (!(to instanceof ExecutableElement || to instanceof TypeElement || isField(to))) {
            return Optional.empty();
        }
        // TODO skip anything not on source path
        var result = trees.getPath(to);
        if (result == null) {
            // LOG.warning(String.format("Element `%s` has no TreePath", to));
            return Optional.empty();
        }
        return Optional.of(result);
    }

    private List<TreePath> referencesToElement(CompilationUnitTree root, Element to) {
        var trees = Trees.instance(task);
        var results = new ArrayList<TreePath>();
        class FindReferencesElement extends TreePathScanner<Void, Void> {
            void check(TreePath from) {
                var found = trees.getElement(from);
                if (sameSymbol(found, to)) {
                    results.add(from);
                }
            }

            @Override
            public Void visitMemberReference(MemberReferenceTree t, Void __) {
                check(getCurrentPath());
                return super.visitMemberReference(t, null);
            }

            @Override
            public Void visitMemberSelect(MemberSelectTree t, Void __) {
                check(getCurrentPath());
                return super.visitMemberSelect(t, null);
            }

            @Override
            public Void visitIdentifier(IdentifierTree t, Void __) {
                check(getCurrentPath());
                return super.visitIdentifier(t, null);
            }

            @Override
            public Void visitNewClass(NewClassTree t, Void __) {
                check(getCurrentPath());
                return super.visitNewClass(t, null);
            }
        }
        new FindReferencesElement().scan(root, null);
        LOG.info(
                String.format(
                        "...found %d references in %s", results.size(), Parser.fileName(root.getSourceFile().toUri())));
        return results;
    }

    private List<Ptr> index(CompilationUnitTree root) {
        var refs = new ArrayList<Ptr>();
        class IndexFile extends TreePathScanner<Void, Void> {
            void check(TreePath from) {
                ref(from).map(Ptr::new).ifPresent(refs::add);
            }

            @Override
            public Void visitMemberReference(MemberReferenceTree t, Void __) {
                check(getCurrentPath());
                return super.visitMemberReference(t, null);
            }

            @Override
            public Void visitMemberSelect(MemberSelectTree t, Void __) {
                check(getCurrentPath());
                return super.visitMemberSelect(t, null);
            }

            @Override
            public Void visitIdentifier(IdentifierTree t, Void __) {
                check(getCurrentPath());
                return super.visitIdentifier(t, null);
            }

            @Override
            public Void visitNewClass(NewClassTree t, Void __) {
                check(getCurrentPath());
                return super.visitNewClass(t, null);
            }
        }
        new IndexFile().scan(root, null);
        if (refs.size() > 0)
            LOG.info(
                    String.format(
                            "Found %d refs in %s", refs.size(), Paths.get(root.getSourceFile().toUri()).getFileName()));
        return refs;
    }

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