Skip to content

Commit

Permalink
Cleanup type check cache
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Feb 5, 2025
1 parent 366ee3b commit a3f67fc
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.ballerina.projects.repos.FileSystemCache;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.runtime.api.types.semtype.TypeCheckCache;
import io.ballerina.runtime.api.types.semtype.TypeCheckCacheFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.ballerinalang.compiler.bir.model.BIRNode;
Expand All @@ -41,6 +43,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Stream;

import static io.ballerina.projects.util.ProjectConstants.CACHES_DIR_NAME;
Expand Down Expand Up @@ -89,6 +92,7 @@ public static Project loadProject(String sourceFilePath, BuildOptions buildOptio
}

public static CompileResult compile(String sourceFilePath) {
cleanUpTypeCheckCaches();
Project project = loadProject(sourceFilePath);

Package currentPackage = project.currentPackage();
Expand All @@ -102,6 +106,31 @@ public static CompileResult compile(String sourceFilePath) {
return compileResult;
}

// FIXME: do we need to provide a cleaner way to clean up the caches?
private static void cleanUpTypeCheckCaches() {
var clazz = TypeCheckCacheFactory.class;
try {
var cacheField = clazz.getDeclaredField("cache");
cacheField.setAccessible(true);
var cache = (Map) cacheField.get(null);
cache.forEach((key, value) -> {
if (value instanceof TypeCheckCache cacheInstance) {
try {
var cachedResultsField = TypeCheckCache.class.getDeclaredField("cachedResults");
cachedResultsField.setAccessible(true);
var cachedResults = (Map) cachedResultsField.get(cacheInstance);
cachedResults.clear();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
});
cache.clear();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static CompileResult compileOffline(String sourceFilePath) {
BuildOptions.BuildOptionsBuilder buildOptionsBuilder = BuildOptions.builder();
BuildOptions buildOptions = buildOptionsBuilder.setOffline(Boolean.TRUE).build();
Expand Down

0 comments on commit a3f67fc

Please sign in to comment.