Skip to content

Commit

Permalink
GH-1068: added ability to run clean index builds on project initializ…
Browse files Browse the repository at this point in the history
…e + use this on config changes + publish empty diagnostics when nothing found
  • Loading branch information
martinlippert committed Jul 31, 2023
1 parent a692703 commit a93a42f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -121,13 +122,13 @@ public class SpringSymbolIndex implements InitializingBean, SpringIndex {
@Override
public void created(IJavaProject project) {
log.info("project created event: {}", project.getElementName());
initializeProject(project);
initializeProject(project, false);
}

@Override
public void changed(IJavaProject project) {
log.info("project changed event: {}", project.getElementName());
initializeProject(project);
initializeProject(project, false);
}

@Override
Expand Down Expand Up @@ -277,14 +278,14 @@ public void removeSymbols(IJavaProject project, String docURI) {

config.addListener(evt -> {
log.info("update settings of spring indexer - start");
server.getAsync().execute(() ->
configureIndexer(SymbolIndexConfig.builder()
.scanXml(config.isSpringXMLSupportEnabled())
.xmlScanFolders(config.xmlBeansFoldersToScan())
.scanTestJavaSources(config.isScanJavaTestSourcesEnabled())
.build()
));

CompletableFuture.runAsync(() ->
configureIndexer(SymbolIndexConfig.builder()
.scanXml(config.isSpringXMLSupportEnabled())
.xmlScanFolders(config.xmlBeansFoldersToScan())
.scanTestJavaSources(config.isScanJavaTestSourcesEnabled())
.build()
), this.updateQueue);

log.info("update settings of spring indexer - done");
});
Expand Down Expand Up @@ -326,6 +327,11 @@ public void configureIndexer(SymbolIndexConfig config) {
}
springIndexerJava.setScanTestJavaSources(config.isScanTestJavaSources());
}

Collection<? extends IJavaProject> projects = projectFinder().all();
for (IJavaProject project : projects) {
initializeProject(project, true);
}
}

private void addXmlFileListeners(List<String> globPattern) {
Expand Down Expand Up @@ -375,16 +381,16 @@ public void shutdown() {
}
}

public CompletableFuture<Void> initializeProject(IJavaProject project) {
CompletableFuture<Void> cf = _initializeProject(project);
public CompletableFuture<Void> initializeProject(IJavaProject project, boolean clean) {
CompletableFuture<Void> cf = _initializeProject(project, clean);
cf.thenAccept( f -> {
projectInitializedFuture(project).complete(null);
});

return cf;
}

private CompletableFuture<Void> _initializeProject(IJavaProject project) {
private CompletableFuture<Void> _initializeProject(IJavaProject project, boolean clean) {
try {
if (SpringProjectUtil.isBootProject(project) || SpringProjectUtil.isSpringProject(project)) {
if (project.getElementName() == null) {
Expand All @@ -399,7 +405,7 @@ private CompletableFuture<Void> _initializeProject(IJavaProject project) {
@SuppressWarnings("unchecked")
CompletableFuture<Void>[] futures = new CompletableFuture[this.indexers.length];
for (int i = 0; i < this.indexers.length; i++) {
InitializeProject initializeItem = new InitializeProject(project, this.indexers[i]);
InitializeProject initializeItem = new InitializeProject(project, this.indexers[i], clean);
futures[i] = CompletableFuture.runAsync(initializeItem, this.updateQueue);
}

Expand Down Expand Up @@ -843,18 +849,20 @@ private class InitializeProject implements Runnable {

private final IJavaProject project;
private final SpringIndexer indexer;
private final boolean clean;

public InitializeProject(IJavaProject project, SpringIndexer indexer) {
public InitializeProject(IJavaProject project, SpringIndexer indexer, boolean clean) {
this.project = project;
this.indexer = indexer;
this.clean = clean;
log.debug("{} created ", this);
}

@Override
public void run() {
log.debug("{} starting...", this);
try {
indexer.initializeProject(project);
indexer.initializeProject(project, this.clean);

log.debug("{} completed", this);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private IndexCacheKey getCacheKey(IJavaProject project) {


@Override
public void initializeProject(IJavaProject project) throws Exception {
public void initializeProject(IJavaProject project, boolean clean) throws Exception {
long startTime = System.currentTimeMillis();
List<Path> files = getFiles(project);
String[] filesStr = files.stream().map(f -> f.toAbsolutePath().toString()).toArray(String[]::new);
Expand All @@ -170,7 +170,7 @@ public void initializeProject(IJavaProject project) throws Exception {
IndexCacheKey cacheKey = getCacheKey(project);

CachedSymbol[] symbols = this.cache.retrieveSymbols(cacheKey, filesStr, CachedSymbol.class);
if (symbols == null) {
if (symbols == null || clean) {
List<CachedSymbol> generatedSymbols = new ArrayList<CachedSymbol>();

for (Path file : files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface SpringIndexer {

List<EnhancedSymbolInformation> computeSymbols(IJavaProject project, String docURI, String content) throws Exception;

void initializeProject(IJavaProject project) throws Exception;
void initializeProject(IJavaProject project, boolean clean) throws Exception;
void removeProject(IJavaProject project) throws Exception;

void updateFile(IJavaProject project, DocumentDescriptor updatedDoc, String content) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ public boolean isInterestedIn(String docURI) {
}

@Override
public void initializeProject(IJavaProject project) throws Exception {
public void initializeProject(IJavaProject project, boolean clean) throws Exception {
String[] files = this.getFiles(project);

log.info("scan java files for symbols for project: {} - no. of files: {}", project.getElementName(), files.length);

long startTime = System.currentTimeMillis();
scanFiles(project, files);
scanFiles(project, files, clean);
long endTime = System.currentTimeMillis();

log.info("scan java files for symbols for project: {} took ms: {}", project.getElementName(), endTime - startTime);
Expand Down Expand Up @@ -413,6 +413,7 @@ public void acceptAST(String sourceFilePath, CompilationUnit cu) {
EnhancedSymbolInformation[] symbols = generatedSymbols.stream().map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] beans = generatedBeans.stream().filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
Map<String, List<Diagnostic>> diagnosticsByDoc = generatedDiagnostics.stream().filter(cachedDiagnostic -> cachedDiagnostic.getDiagnostic() != null).collect(Collectors.groupingBy(CachedDiagnostics::getDocURI, Collectors.mapping(CachedDiagnostics::getDiagnostic, Collectors.toList())));
addEmptyDiagnostics(diagnosticsByDoc, javaFiles);
symbolHandler.addSymbols(project, symbols, beans, diagnosticsByDoc);

IndexCacheKey symbolsCacheKey = getCacheKey(project, SYMBOL_KEY);
Expand Down Expand Up @@ -457,7 +458,7 @@ private void scanAffectedFiles(IJavaProject project, Set<String> changedTypes, S
log.info("Finished scanning affected files {}", alreadyScannedFiles);
}

private void scanFiles(IJavaProject project, String[] javaFiles) throws Exception {
private void scanFiles(IJavaProject project, String[] javaFiles, boolean clean) throws Exception {
IndexCacheKey symbolsCacheKey = getCacheKey(project, SYMBOL_KEY);
IndexCacheKey beansCacheKey = getCacheKey(project, BEANS_KEY);
IndexCacheKey diagnosticsCacheKey = getCacheKey(project, DIAGNOSTICS_KEY);
Expand All @@ -470,7 +471,7 @@ private void scanFiles(IJavaProject project, String[] javaFiles) throws Exceptio
CachedBean[] beans;
CachedDiagnostics[] diagnostics;

if (cachedSymbols == null || cachedBeans == null || cachedDiagnostics == null ) {
if (clean || cachedSymbols == null || cachedBeans == null || cachedDiagnostics == null) {
List<CachedSymbol> generatedSymbols = new ArrayList<CachedSymbol>();
List<CachedBean> generatedBeans = new ArrayList<CachedBean>();
List<CachedDiagnostics> generatedDiagnostics = new ArrayList<CachedDiagnostics>();
Expand Down Expand Up @@ -517,6 +518,7 @@ public void accept(String docURI, Diagnostic diagnostic) {
EnhancedSymbolInformation[] enhancedSymbols = Arrays.stream(symbols).map(cachedSymbol -> cachedSymbol.getEnhancedSymbol()).toArray(EnhancedSymbolInformation[]::new);
Bean[] allBeans = Arrays.stream(beans).filter(cachedBean -> cachedBean.getBean() != null).map(cachedBean -> cachedBean.getBean()).toArray(Bean[]::new);
Map<String, List<Diagnostic>> diagnosticsByDoc = Arrays.stream(diagnostics).filter(cachedDiagnostic -> cachedDiagnostic.getDiagnostic() != null).collect(Collectors.groupingBy(CachedDiagnostics::getDocURI, Collectors.mapping(CachedDiagnostics::getDiagnostic, Collectors.toList())));
addEmptyDiagnostics(diagnosticsByDoc, javaFiles);
symbolHandler.addSymbols(project, enhancedSymbols, allBeans, diagnosticsByDoc);
}
}
Expand Down Expand Up @@ -559,6 +561,17 @@ public void acceptAST(String sourceFilePath, CompilationUnit cu) {
}
}

private void addEmptyDiagnostics(Map<String, List<Diagnostic>> diagnosticsByDoc, String[] javaFiles) {
for (int i = 0; i < javaFiles.length; i++) {
File file = new File(javaFiles[i]);
String docURI = UriUtil.toUri(file).toASCIIString();

if (!diagnosticsByDoc.containsKey(docURI)) {
diagnosticsByDoc.put(docURI, Collections.emptyList());
}
}
}

private void scanAST(final SpringIndexerJavaContext context) {
context.getCu().accept(new ASTVisitor() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public boolean isInterestedIn(String docURI) {
}

@Override
public void initializeProject(IJavaProject project) throws Exception {
public void initializeProject(IJavaProject project, boolean clean) throws Exception {
long startTime = System.currentTimeMillis();
String[] files = this.getFiles(project);

Expand All @@ -116,7 +116,7 @@ public void initializeProject(IJavaProject project) throws Exception {
CachedSymbol[] symbols = this.cache.retrieveSymbols(symbolsCacheKey, files, CachedSymbol.class);
CachedBean[] beans = this.cache.retrieveSymbols(beansCacheKey, files, CachedBean.class);

if (symbols == null || beans == null) {
if (symbols == null || beans == null || clean) {
List<CachedSymbol> generatedSymbols = new ArrayList<CachedSymbol>();
List<CachedBean> generatedBeans = new ArrayList<CachedBean>();

Expand Down Expand Up @@ -334,7 +334,7 @@ private void clearIndex() {
private void populateIndex() {
for (IJavaProject project : projectFinder.all()) {
try {
initializeProject(project);
initializeProject(project, true);
} catch (Exception e) {
log.error("{}", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.springframework.ide.vscode.boot.java.reconcilers.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
Expand Down Expand Up @@ -67,9 +68,8 @@ public void setup() throws Exception {
}

@Test
void testScanSimpleConfigurationClass() throws Exception {
void testFindPublicBeanMethodInConfigClass() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/BeanMethodNotPublic.java").toUri().toString();
assertTrue(true);

PublishDiagnosticsParams diagnosticsMessage = harness.getDiagnostics(docUri);
List<Diagnostic> diagnostics = diagnosticsMessage.getDiagnostics();
Expand All @@ -86,4 +86,15 @@ void testScanSimpleConfigurationClass() throws Exception {

assertEquals(1, diagnostics.size());
}

@Test
void testPublishEmptyDiagnosticsWhenNoProblemsAreFound() throws Exception {
String docUri = directory.toPath().resolve("src/main/java/org/test/BeanClass1.java").toUri().toString();

PublishDiagnosticsParams diagnosticsMessage = harness.getDiagnostics(docUri);
assertNotNull(diagnosticsMessage);

List<Diagnostic> diagnostics = diagnosticsMessage.getDiagnostics();
assertEquals(0, diagnostics.size());
}
}

0 comments on commit a93a42f

Please sign in to comment.