PT #153100339 Don't cache CU unless there is a project for it
This commit is contained in:
@@ -15,7 +15,6 @@ import java.nio.file.Path;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
@@ -31,7 +30,6 @@ 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.SimpleTextDocumentService;
|
||||
import org.springframework.ide.vscode.commons.util.BadLocationException;
|
||||
import org.springframework.ide.vscode.commons.util.Log;
|
||||
import org.springframework.ide.vscode.commons.util.text.TextDocument;
|
||||
|
||||
@@ -87,18 +85,32 @@ public final class CompilationUnitCache {
|
||||
*/
|
||||
public <T> T withCompilationUnit(TextDocument document, Function<CompilationUnit, T> requestor) {
|
||||
URI uri = URI.create(document.getUri());
|
||||
readLock.lock();
|
||||
try {
|
||||
CompilationUnit cu = uriToCu.get(uri, () -> parse(document));
|
||||
if (cu!=null) {
|
||||
synchronized (cu.getAST()) {
|
||||
return requestor.apply(cu);
|
||||
IJavaProject project = projectFinder.find(document.getId()).orElse(null);
|
||||
if (project != null) {
|
||||
readLock.lock();
|
||||
try {
|
||||
CompilationUnit cu = uriToCu.get(uri, () -> {
|
||||
CompilationUnit cUnit = parse(document, project);
|
||||
projectToDocs.get(project, () -> new HashSet<>()).add(URI.create(document.getUri()));
|
||||
return cUnit;
|
||||
});
|
||||
if (cu!=null) {
|
||||
projectToDocs.get(project, () -> new HashSet<>()).add(URI.create(document.getUri()));
|
||||
synchronized (cu.getAST()) {
|
||||
return requestor.apply(cu);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
return requestor.apply(parse(document, null));
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
return requestor.apply(null);
|
||||
}
|
||||
@@ -137,16 +149,6 @@ public final class CompilationUnitCache {
|
||||
return cu;
|
||||
}
|
||||
|
||||
private CompilationUnit parse(TextDocument document)
|
||||
throws Exception, BadLocationException {
|
||||
IJavaProject project = projectFinder.find(document.getId()).orElse(null);
|
||||
CompilationUnit cu = parse(document, project);
|
||||
if (project != null) {
|
||||
projectToDocs.get(project, () -> new HashSet<>()).add(URI.create(document.getUri()));
|
||||
}
|
||||
return cu;
|
||||
}
|
||||
|
||||
private static String[] getClasspathEntries(TextDocument document, IJavaProject project) throws Exception {
|
||||
if (project == null) {
|
||||
return new String[0];
|
||||
|
||||
@@ -23,11 +23,12 @@ import java.nio.file.Paths;
|
||||
import org.eclipse.jdt.core.dom.CompilationUnit;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.ide.vscode.boot.java.BootJavaLanguageServer;
|
||||
import org.springframework.ide.vscode.commons.java.DelegatingCachedClasspath;
|
||||
import org.springframework.ide.vscode.commons.java.IClasspath;
|
||||
import org.springframework.ide.vscode.commons.java.IJavaProject;
|
||||
import org.springframework.ide.vscode.commons.maven.MavenCore;
|
||||
import org.springframework.ide.vscode.commons.util.text.LanguageId;
|
||||
import org.springframework.ide.vscode.commons.util.text.TextDocument;
|
||||
import org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness;
|
||||
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
|
||||
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
|
||||
|
||||
@@ -39,7 +40,7 @@ import org.springframework.ide.vscode.project.harness.ProjectsHarness;
|
||||
*/
|
||||
public class CompilationUnitCacheTest {
|
||||
|
||||
private LanguageServerHarness<BootJavaLanguageServer> harness;
|
||||
private BootLanguageServerHarness harness;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
@@ -48,6 +49,14 @@ public class CompilationUnitCacheTest {
|
||||
|
||||
@Test
|
||||
public void cu_cached() throws Exception {
|
||||
harness = BootLanguageServerHarness.builder()
|
||||
.mockDefaults().build();
|
||||
harness.useProject(new IJavaProject() {
|
||||
@Override
|
||||
public IClasspath getClasspath() {
|
||||
return new DelegatingCachedClasspath<>(() -> null, null);
|
||||
}
|
||||
});
|
||||
harness.intialize(null);
|
||||
|
||||
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
|
||||
@@ -62,12 +71,36 @@ public class CompilationUnitCacheTest {
|
||||
assertTrue(cu == cuAnother);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cu_not_cached_without_project() throws Exception {
|
||||
harness.intialize(null);
|
||||
|
||||
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
|
||||
"\n" +
|
||||
"public class SomeClass {\n" +
|
||||
"\n" +
|
||||
"}\n");
|
||||
CompilationUnit cu = getCompilationUnit(doc);
|
||||
assertNotNull(cu);
|
||||
|
||||
CompilationUnit cuAnother = getCompilationUnit(doc);
|
||||
assertFalse(cu == cuAnother);
|
||||
}
|
||||
|
||||
private CompilationUnit getCompilationUnit(TextDocument doc) {
|
||||
return harness.getServer().getCompilationUnitCache().withCompilationUnit(doc, cu -> cu);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cu_cache_invalidated_by_doc_change() throws Exception {
|
||||
harness = BootLanguageServerHarness.builder()
|
||||
.mockDefaults().build();
|
||||
harness.useProject(new IJavaProject() {
|
||||
@Override
|
||||
public IClasspath getClasspath() {
|
||||
return new DelegatingCachedClasspath<>(() -> null, null);
|
||||
}
|
||||
});
|
||||
harness.intialize(null);
|
||||
|
||||
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
|
||||
@@ -91,6 +124,14 @@ public class CompilationUnitCacheTest {
|
||||
|
||||
@Test
|
||||
public void cu_cache_invalidated_by_doc_close() throws Exception {
|
||||
harness = BootLanguageServerHarness.builder()
|
||||
.mockDefaults().build();
|
||||
harness.useProject(new IJavaProject() {
|
||||
@Override
|
||||
public IClasspath getClasspath() {
|
||||
return new DelegatingCachedClasspath<>(() -> null, null);
|
||||
}
|
||||
});
|
||||
harness.intialize(null);
|
||||
|
||||
TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" +
|
||||
|
||||
Reference in New Issue
Block a user