java: properly set language level on frontend:

* avoid memory leak when references to java files can't be GC-ed until project closed
* use global language level instead of per-project (the same as monolithic mode does)

GitOrigin-RevId: 4681d7f809661ee98bd7c2de8918dd4311f52977
This commit is contained in:
Dmitry Batkovich
2025-04-17 13:10:08 +00:00
committed by intellij-monorepo-bot
parent c018629f0a
commit 6cb820f47c
@@ -4,6 +4,7 @@ package com.intellij.core;
import com.intellij.lang.ASTNode;
import com.intellij.model.psi.PsiSymbolReference;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
@@ -14,6 +15,8 @@ import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
public class CoreJavaPsiImplementationHelper extends JavaPsiImplementationHelper {
private final Project myProject;
@@ -39,9 +42,24 @@ public class CoreJavaPsiImplementationHelper extends JavaPsiImplementationHelper
@Override
public @NotNull LanguageLevel getEffectiveLanguageLevel(@Nullable VirtualFile virtualFile) {
if (virtualFile != null) {
synchronized (virtualFile) {
LanguageLevel level = virtualFile.getUserData(FORCED_LANGUAGE_LEVEL_KEY);
if (level != null) return level;
}
}
return PsiUtil.getLanguageLevel(myProject);
}
public boolean setEffectiveLanguageLevel(@NotNull VirtualFile file, @Nullable LanguageLevel level) {
synchronized (file) {
LanguageLevel previousHolder = file.getUserData(FORCED_LANGUAGE_LEVEL_KEY);
if (Objects.equals(previousHolder, level)) return false;
file.putUserData(FORCED_LANGUAGE_LEVEL_KEY, level);
return true;
}
}
@Override
public ASTNode getDefaultImportAnchor(@NotNull PsiImportList list, @NotNull PsiImportStatementBase statement) {
throw new UnsupportedOperationException("TODO");
@@ -70,4 +88,6 @@ public class CoreJavaPsiImplementationHelper extends JavaPsiImplementationHelper
public @NotNull Project getProject() {
return myProject;
}
private static final Key<LanguageLevel> FORCED_LANGUAGE_LEVEL_KEY = Key.create("FORCED_LANGUAGE_LEVEL_KEY");
}