-
Notifications
You must be signed in to change notification settings - Fork 203
/
CompilationUnitCache.java
354 lines (311 loc) · 12.4 KB
/
CompilationUnitCache.java
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
* Copyright (c) 2017, 2020 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.utils;
import java.io.File;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
import org.eclipse.jdt.internal.core.BasicCompilationUnit;
import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
import org.eclipse.jdt.internal.core.INameEnvironmentWithProgress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
import org.springframework.ide.vscode.commons.util.AsyncRunner;
import org.springframework.ide.vscode.commons.util.text.TextDocument;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import reactor.core.scheduler.Schedulers;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
public final class CompilationUnitCache implements DocumentContentProvider {
private static final Logger logger = LoggerFactory.getLogger(CompilationUnitCache.class);
private static final long CU_ACCESS_EXPIRATION = 1;
private JavaProjectFinder projectFinder;
private ProjectObserver projectObserver;
private Cache<URI, CompilationUnit> uriToCu;
private Cache<IJavaProject, Set<URI>> projectToDocs;
private Cache<IJavaProject, Tuple2<List<Classpath>, INameEnvironmentWithProgress>> lookupEnvCache;
private ProjectObserver.Listener projectListener;
private SimpleTextDocumentService documents;
private AsyncRunner async;
private ReadLock readLock;
private WriteLock writeLock;
public CompilationUnitCache(JavaProjectFinder projectFinder, SimpleLanguageServer server, ProjectObserver projectObserver) {
this.projectFinder = projectFinder;
this.projectObserver = projectObserver;
this.lookupEnvCache = CacheBuilder.newBuilder().build();
// PT 154618835 - Avoid retaining the CU in the cache as it consumes memory if it hasn't been
// accessed after some time
uriToCu = CacheBuilder.newBuilder()
.expireAfterWrite(CU_ACCESS_EXPIRATION, TimeUnit.MINUTES)
.build();
projectToDocs = CacheBuilder.newBuilder().build();
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
this.documents = server == null ? null : server.getTextDocumentService();
this.async = server == null ? new AsyncRunner(Schedulers.single()) : server.getAsync();
if (documents != null) {
documents.onDidChangeContent(doc -> invalidateCuForJavaFile(doc.getDocument().getId().getUri()));
documents.onDidClose(doc -> invalidateCuForJavaFile(doc.getId().getUri()));
}
async.execute(() -> {
writeLock.lock();
try {
for (IJavaProject project : projectFinder.all()) {
loadLookupEnvTuple(project);
}
} finally {
writeLock.unlock();
}
});
projectListener = new ProjectObserver.Listener() {
@Override
public void deleted(IJavaProject project) {
logger.info("CU Cache: deleted project {}", project.getElementName());
async.execute(() -> {
writeLock.lock();
try {
invalidateProject(project);
} finally {
writeLock.unlock();
}
});
}
@Override
public void created(IJavaProject project) {
logger.info("CU Cache: created project {}", project.getElementName());
async.execute(() -> {
writeLock.lock();
try {
invalidateProject(project);
// Load the new cache the value right away
loadLookupEnvTuple(project);
} finally {
writeLock.unlock();
}
});
}
@Override
public void changed(IJavaProject project) {
logger.info("CU Cache: changed project {}", project.getElementName());
async.execute(() -> {
writeLock.lock();
try {
invalidateProject(project);
// Load the new cache the value right away
loadLookupEnvTuple(project);
} finally {
writeLock.unlock();
}
});
}
};
if (this.projectObserver != null) {
this.projectObserver.addListener(projectListener);
}
}
public void dispose() {
if (projectObserver != null) {
projectObserver.removeListener(projectListener);
}
}
/**
* Retrieves a CompiationUnitn AST from the cache and passes it to a requestor callback, applying
* proper thread synchronization around the requestor.
* <p>
* Warning: Callers should take care to do all AST processing inside of the requestor callback and
* not pass of AST nodes to helper functions that work aynchronously or store AST nodes or ITypeBindings
* for later use. The JDT ASTs are not thread safe!
*/
@Deprecated
public <T> T withCompilationUnit(TextDocument document, Function<CompilationUnit, T> requestor) {
IJavaProject project = projectFinder.find(document.getId()).orElse(null);
URI uri = URI.create(document.getUri());
return withCompilationUnit(project, uri, requestor);
}
public <T> T withCompilationUnit(IJavaProject project, URI uri, Function<CompilationUnit, T> requestor) {
if (project != null) {
readLock.lock();
CompilationUnit cu = null;
try {
cu = uriToCu.get(uri, () -> {
Tuple2<List<Classpath>, INameEnvironmentWithProgress> lookupEnvTuple = loadLookupEnvTuple(project);
String utiStr = uri.toString();
String unitName = utiStr.substring(utiStr.lastIndexOf("/"));
CompilationUnit cUnit = parse2(fetchContent(uri).toCharArray(), utiStr, unitName, lookupEnvTuple.getT1(), lookupEnvTuple.getT2());
projectToDocs.get(project, () -> new HashSet<>()).add(uri);
return cUnit;
});
if (cu != null) {
projectToDocs.get(project, () -> new HashSet<>()).add(uri);
}
} catch (Exception e) {
logger.error("", e);
} finally {
readLock.unlock();
}
if (cu != null) {
try {
synchronized (cu.getAST()) {
return requestor.apply(cu);
}
}
catch (Exception e) {
logger.error("", e);
}
}
}
return requestor.apply(null);
}
private void invalidateCuForJavaFile(String uriStr) {
URI uri = URI.create(uriStr);
writeLock.lock();
try {
uriToCu.invalidate(uri);
} finally {
writeLock.unlock();
}
}
// public static CompilationUnit parse(TextDocument document, IJavaProject project) throws Exception {
// String[] classpathEntries = getClasspathEntries(project);
// String docURI = document.getUri();
// String unitName = docURI.substring(docURI.lastIndexOf("/"));
// char[] source = document.get(0, document.getLength()).toCharArray();
// return parse(source, docURI, unitName, classpathEntries);
// }
//
// public CompilationUnit parse(String uri, char[] source, IJavaProject project) throws Exception {
// String[] classpathEntries = getClasspathEntries(project);
// String unitName = uri.substring(uri.lastIndexOf("/"));
// return parse(source, uri, unitName, classpathEntries);
// }
//
// public static CompilationUnit parse(char[] source, String docURI, String unitName, String[] classpathEntries) throws Exception {
// ASTParser parser = ASTParser.newParser(AST.JLS11);
// Map<String, String> options = JavaCore.getOptions();
// JavaCore.setComplianceOptions(JavaCore.VERSION_11, options);
// parser.setCompilerOptions(options);
// parser.setKind(ASTParser.K_COMPILATION_UNIT);
// parser.setStatementsRecovery(true);
// parser.setBindingsRecovery(true);
// parser.setResolveBindings(true);
//
// String[] sourceEntries = new String[] {};
// parser.setEnvironment(classpathEntries, sourceEntries, null, false);
//
// parser.setUnitName(unitName);
// parser.setSource(source);
//
// CompilationUnit cu = (CompilationUnit) parser.createAST(null);
//
// return cu;
// }
public static CompilationUnit parse2(char[] source, String docURI, String unitName, IJavaProject project) throws Exception {
List<Classpath> classpaths = createClasspath(getClasspathEntries(project));
return parse2(source, docURI, unitName, classpaths, null);
}
private static CompilationUnit parse2(char[] source, String docURI, String unitName, List<Classpath> classpaths, INameEnvironmentWithProgress environment) throws Exception {
Map<String, String> options = JavaCore.getOptions();
String apiLevel = JavaCore.VERSION_14;
JavaCore.setComplianceOptions(apiLevel, options);
if (environment == null) {
environment = CUResolver.createLookupEnvironment(classpaths.toArray(new Classpath[classpaths.size()]));
}
BasicCompilationUnit sourceUnit = new BasicCompilationUnit(source, null, unitName, (IJavaElement) null);
int flags = 0;
boolean needToResolveBindings = true;
flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
CompilationUnitDeclaration unit = null;
try {
unit = CUResolver.resolve(sourceUnit, classpaths, options, flags, environment);
} catch (Exception e) {
flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
unit = CUResolver.parse(sourceUnit, options, flags);
needToResolveBindings = false;
}
CompilationUnit cu = CUResolver.convert(unit, source, AST.JLS11, options, needToResolveBindings, DefaultWorkingCopyOwner.PRIMARY, flags);
return cu;
}
private static List<Classpath> createClasspath(String[] classpathEntries) {
ASTParser parser = ASTParser.newParser(AST.JLS14);
String[] sourceEntries = new String[] {};
parser.setEnvironment(classpathEntries, sourceEntries, null, false);
return CUResolver.getClasspath(parser);
}
private Tuple2<List<Classpath>, INameEnvironmentWithProgress> loadLookupEnvTuple(IJavaProject project) {
try {
return lookupEnvCache.get(project, () -> {
List<Classpath> classpaths = createClasspath(getClasspathEntries(project));
INameEnvironmentWithProgress environment = CUResolver.createLookupEnvironment(classpaths.toArray(new Classpath[classpaths.size()]));
return Tuples.of(classpaths, environment);
});
} catch (ExecutionException e) {
logger.error("{}", e);
return null;
}
}
private static String[] getClasspathEntries(IJavaProject project) throws Exception {
if (project == null) {
return new String[0];
} else {
IClasspath classpath = project.getClasspath();
Stream<File> classpathEntries = IClasspathUtil.getAllBinaryRoots(classpath).stream();
return classpathEntries
.filter(file -> file.exists())
.map(file -> file.getAbsolutePath()).toArray(String[]::new);
}
}
private void invalidateProject(IJavaProject project) {
Set<URI> docUris = projectToDocs.getIfPresent(project);
if (docUris != null) {
uriToCu.invalidateAll(docUris);
projectToDocs.invalidate(project);
}
lookupEnvCache.invalidate(project);
}
@Override
public String fetchContent(URI uri) throws Exception {
if (documents != null) {
TextDocument document = documents.get(uri.toString());
if (document != null) {
return document.get(0, document.getLength());
}
}
return IOUtils.toString(uri);
}
}